Convert PDF to Word in C#

PDF to DOCX

Nutrient .NET SDK (formerly GdPicture.NET) includes the ability to convert any supported file type into Word.

To save a PDF to a Word document (DOCX), use the SaveAsDOCX method method of the GdPictureDocumentConverter class. It uses the following parameter:

  • Stream, or the overload FilePath — A stream object where the current document is saved as a DOCX file. This stream object must be initialized before it can be sent into this method, and it should stay open for subsequent use. If the output stream isn’t open for both reading and writing, the method will fail, returning the GdPictureStatus.InvalidParameter status, which is the file path where the converted file will be saved. If the specified file already exists, it’ll be overwritten. You have to specify a full file path, including the file extension.

Note that the output stream should be open for both reading and writing and closed/disposed of by the user once processing is complete using the CloseDocument method.

How to convert PDF to DOCX

  1. Create a GdPictureDocumentConverter object.
  2. Load the source document by passing its path to the LoadFromFile method. This method accepts all supported file formats. However, only PDF will return a high-quality DOCX. If the source document isn’t a PDF, saveAsDOCX will return a DOCX, with each page containing a bitmap image representing the input document. If the source document isn’t a PDF, files can be converted to PDF with GdPictureDocumentConverter.SaveAsPDF and then passed to the saveAsDOCX method. Recommended: Specify the source document format with a member of the DocumentFormat enumeration.
  3. Save the PDF file as a DOCX using SaveAsDOCX.

If you use SaveAsDOCX after loading a file that isn’t a PDF, the method will create a DOCX containing the original document as an image. Instead, for the best results, ensure the input document is a PDF.

The following example converts and saves a PDF document to a DOCX file (it can also be saved as a stream):

using GdPictureDocumentConverter converter = new();
GdPictureStatus status = converter.LoadFromFile("input.pdf");
if (status != GdPictureStatus.OK)
{
throw new Exception(status.ToString());
}
status = converter.SaveAsDOCX("output.docx");
if (status != GdPictureStatus.OK)
{
throw new Exception(status.ToString());
}
Console.WriteLine("The input document has been converted to a docx file");