DEV Community

Gia
Gia

Posted on

How to Convert PDF to Word with Fixed Layout on .NET Platforms

In today's digital world, PDF files have become a standard format for document sharing and distribution. However, there are times when we need to convert a PDF file into a Word document with a fixed layout, preserving the original formatting, images, and design. This article will guide you through the process of converting PDF to Word with a fixed layout, allowing you to maintain the integrity of your documents while gaining the flexibility and editing capabilities of Microsoft Word.

Step 1 Download

Download Free Spire.PDF for .NET from this link and install it to local.
This free library not only supports converting PDF to Word, but also compressing and converting PDF, including images and Excel.

Step 2 Import the DLL File into Project

Take Visual Studio 2022 for example.

  • Create a new C# project and open it.
  • Right-click " References " in the " Solution Explorer ", and then select " Add Reference " > " Browse ".
  • Find the DLL file in the BIN folder under the installation path and click " OK " to add it as a reference to the program.

Step 3 Write code

C#:

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("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();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description
The following explanation can help you understand this code easily.
Firstly, create a “PdfDocument” object. And then load your PDF file using “PdfDocument.LoadFromFile()” method. Finally, Convert the PDF document to a Doc or Docx format file using “PdfDocument.SaveToFile(String fileName, FileFormat fileFormat)”method.

Top comments (0)