How to Convert PDF to Word with C#?

 It is well known that PDF documents support extra-long files, are highly integrated, safe, and reliable, and can effectively prevent others from changing PDF content, which is why they are very popular in the workplace. But at work, we will inevitably modify or re-edit PDF documents. At this time, we can convert PDF to Word document format for modification. To introduce how to convert PDF to Word programmatically, this article will be divided into two parts. Here we go.

  •     Convert PDF to Doc/Docx with Fixed Layout
  •     Convert PDF to Doc/Docx with Flowable Structure

Fixed Layout mode has fast conversion speed and is conducive to maintaining the original appearance of PDF files to the greatest extent. However, the editability of the resulting document will be limited since each line of text in PDF will be presented in a separate frame in the generated Word document.

Flowable Structure is a full recognition mode. The converted content will not be presented in frames, and the structure of the resulting document is flowable. The generated Word document is easy to re-edit but may look different from the original PDF file.

Programming Environment  

In this test, Free Spire.PDF for .NET is introduced into the program. The Spire.PDF.dll file can be referenced by:

Method 1: Download Free Spire.PDF for .NET locally, unzip it, and install it. After the installation is complete, find Spire.PDF.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.PDF", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.PDF -Version 8.6

Convert PDF to Doc/Docx with Fixed Layout

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Convert the document to a .doc/.docx format file using PdfDocument.SaveToFile() method.

Full Code

C#

using Spire.Pdf;

 

namespace ConvertPdfToDoc

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a PdfDocument object

            PdfDocument doc = new PdfDocument();

 

            //Load a sample PDF document

            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

 

            //Convert PDF to Doc and save it to a specified path

            doc.SaveToFile("ToDoc.doc", FileFormat.DOC);

 

            //Convert PDF to Docx and save it to a specified path

            doc.SaveToFile("ToDocx.docx", FileFormat.DOCX);

 

        }

    }

}

 

VB.NET

Imports Spire.Pdf

 

Namespace ConvertPdfToDoc

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())

            'Create a PdfDocument object

            Dim doc As PdfDocument = New PdfDocument()

 

            'Load a sample PDF document

            doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

 

            'Convert PDF to Doc and save it to a specified path

            doc.SaveToFile("ToDoc.doc", FileFormat.DOC)

 

            'Convert PDF to Docx and save it to a specified path

            doc.SaveToFile("ToDocx.docx", FileFormat.DOCX)

 

        End Sub

    End Class

End Namespace

Convert PDF to Docx Using PS Mode

  • Create a PdfDocument object. 
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Set the conversion engine to PS mode, and set the recognition mode to flow using PdfConvertOptions.SetPdfToDocOptions(bool usePsMode, bool useFlowRecognitionMode) method.
  • Convert the document to a .doc/.docx format file using PdfDocument.SaveToFile() method.

Full Code

C#

using Spire.Pdf;

 

namespace ConvertPdfToDocxUsingPsMode

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a PdfDocument object

            PdfDocument doc = new PdfDocument();

 

            //Load a sample PDF document

            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

 

            //Use PS mode for converting PDF to Word, and set the recognition mode to flow

            doc.ConvertOptions.SetPdfToDocOptions(true, true);

 

            //Convert PDF to Doc and save it to a specified path

            doc.SaveToFile("ToDoc1.docx", FileFormat.DOCX);

 

            //Convert PDF to Docx and save it to a specified path

            doc.SaveToFile("ToDoc1.docx", FileFormat.DOCX);

 

        }

    }

}


VB.NET

Imports Spire.Pdf

 

Namespace ConvertPdfToDocxUsingPsMode

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())

            'Create a PdfDocument object

            Dim doc As PdfDocument = New PdfDocument()

 

            'Load a sample PDF document

            doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

 

            'Use PS mode for converting PDF to Word, and set the recognition mode to flow

            doc.ConvertOptions.SetPdfToDocOptions(True, True)

 

            'Convert PDF to Doc and save it to a specified path

            doc.SaveToFile("ToDoc1.docx", FileFormat.DOCX)

 

            'Convert PDF to Docx and save it to a specified path

            doc.SaveToFile("ToDoc1.docx", FileFormat.DOCX)

 

        End Sub

    End Class

End Namespace

Effective Shot


Attention:
The code path is for reference only, and you can customize the path as you want.

 Conclusion:
   In this post, you have learned how toconvert PDF to Word in C#/VB.NET. Not only that, we also have other functions, such as, C#/VB.NET: Convert HTML to PDF, C#/VB.NET: Convert Text Files to PDF and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for .NET.

Comments

Popular posts from this blog

Java: How to encrypt or decrypt PDF documents?

Replace the existing image on the PDF file in C#/VB.NET

How to Convert PDF to Images in Java