Convert PDF to Word in C#
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 overloadFilePath
— 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 theGdPictureStatus.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
- Create a
GdPictureDocumentConverter
object. - 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 withGdPictureDocumentConverter.SaveAsPDF
and then passed to thesaveAsDOCX
method. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. - 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");
Using gdpictureDocumentConverter As New GdPictureDocumentConverter() Dim status As GdPictureStatus = gdpictureDocumentConverter.LoadFromFile("input.pdf", GdPicture14.DocumentFormat.DocumentFormatPDF) If status = GdPictureStatus.OK Then gdpictureDocumentConverter.DocxImageQuality = 80 status = gdpictureDocumentConverter.SaveAsDOCX("output.docx") If status = GdPictureStatus.OK Then MessageBox.Show("The file has been saved successfully.", "GdPicture") Else MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "GdPicture") End If Else MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "GdPicture") End IfEnd Using
See also
Related topics