Convert Word to PDF in C#
Word to PDF
Nutrient .NET SDK (formerly GdPicture.NET) offers Office-to-PDF conversion that relies entirely on its own technology. The conversion doesn’t rely on third-party tools such as LibreOffice.
This article explains how to convert Word files such as DOC or DOCX to PDF.
Creating a PDF from a Word file
To create a PDF from a Word file, follow the steps below:
- Create a
GdPictureDocumentConverter
object. - Load the source document by passing its path to the
LoadFromFile
method. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. - Save the output in a new PDF document with the
SaveAsPDF
method.
The example below creates a PDF document from a Word file:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();// Load the source document.gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.docx", GdPicture14.DocumentFormat.DocumentFormatDOCX);// Save the output in a new PDF document.gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. gdpictureDocumentConverter.LoadFromFile("C:\temp\source.docx", GdPicture14.DocumentFormat.DocumentFormatDOCX) ' Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf")End Using
Used methods
Related topics
Optional configuration properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter
object:
PageRange
specifies the pages included in the conversion. Use commas and hyphens to define the pattern. For example,"1,5,9-12"
specifies pages 1, 5, 9, 10, 11, and 12.TrackOfficeDocumentRevisions
is a Boolean value that specifies whether to display Track Changes revisions in the output PDF file.
The example below creates a PDF document from a Word file with a custom configuration:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();// Load the source document.gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.docx", GdPicture14.DocumentFormat.DocumentFormatDOCX);// Configure the conversion.gdpictureDocumentConverter.PageRange = "1,5,9-12";// Save the output in a new PDF document.gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. gdpictureDocumentConverter.LoadFromFile("C:\temp\source.docx", GdPicture14.DocumentFormat.DocumentFormatDOCX) ' Configure the conversion. gdpictureDocumentConverter.PageRange = "1,5,9-12" ' Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf")End Using
Used methods and properties
Related topics