PDF format is the best choice in many cases, but Word is more flexible when editing or modification is needed. PDF files are typically used for online sharing, printing and archiving, while Word documents are used for creating, editing and formatting documents. Converting a PDF to Word is a good option if you want to re-edit the PDF document. In this article, you will learn how to programmatically convert PDF to Word in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
Spire.PDF for .NET provides two modes of conversion. The advantages and disadvantages of these two modes are as follows:
- Fixed Layout Mode: The 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.
- Flow Recognition Mode: The flow recognition mode 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.
Convert PDF to Fixed-Layout Doc/Docx in C#, VB.NET
By default, the PdfDcoument.SaveToFile() method will convert PDF to Word with fixed layout. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.SaveToFile(String fileName, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Pdf; namespace ConvertPdfToFixedLayoutWord { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Convert PDF to Doc and save it to a specified path doc.SaveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx and save it to a specified path doc.SaveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.Close(); } } }
Convert PDF to Flexible-Structured Doc/Docx in C#, VB.NET
In addition to the default conversion engine, Spire.PDF for .NET provides another engine called Ps mode, which works better with the flow recognition mode. To enable Ps conversion engine and flow recognition mode, pass (true, true) as the parameters of the PdfDocument.ConvertOptions.SetPdfToDocOptions(bool usePsMode, bool useFlowRecognitionMode) method. The entire steps are as follows.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Enable Ps conversion engine and flow recognition mode using PdfDocument.ConvertOptions.SetPdfToDocOptions(true, true) method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace ConvertPdfToFlexibleLayoutWord { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Specify the PDF to Word conversion options doc.ConvertOptions.SetPdfToDocOptions(true, true); //Convert PDF to Doc doc.SaveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx doc.SaveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.Close(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.