Word to PDF conversion is a common practice these days and it is preferred before sharing the documents. MS Word provides a builtin feature to save Word documents as PDF, however, there might be a case when you want to convert Word document to PDF programmatically such as in your web applications or you may require batch Word to PDF conversions without installing MS Office. So in this article, you will learn how to convert Word DOCX or DOC to PDF using C# in .NET or .NET Core framework.
Word to PDF C# Conversion Scenarios
In this article, you will learn how to:
- Convert Word DOCX/DOC to PDF in C#
- Convert desired pages of Word document to PDF in C#
- Word to PDF conversion with a particular PDF Standard such as PDF 1.7, PDF/A-1a, etc.
- Convert Word to PDF with image/text compression in C#
- DOCX/DOC to PDF with a digital signature in C#
- Word to PDF conversion with the desired JPEG quality for images
C# DOCX/DOC to PDF Converter
Aspose.Words for .NET is a powerful API for creating and manipulating popular Word document formats. The API also provides an easy way of converting Word documents to PDF with a high degree of fidelity. Therefore, for Word to PDF conversion in C#, we’ll first add a reference to Aspose.Words for .NET DLL in the project or install it using one of the following methods.
Using NuGet Package Manager
- Create/open the project in Visual Studio.
- Go to NuGet Package Manager and search Aspose.Words.
- Install the Aspose.Words for .NET.
Using the Package Manager Console
PM> Install-Package Aspose.Words
Simple Word (DOCX/DOC) to PDF C# Conversion
To convert a Word document to PDF, you’ll have to invoke the Document.Save() method and specify the output file name with the “.pdf” extension. The following code sample shows how to convert Word DOC to PDF in C#.
Convert Selected Pages of Word DOCX to PDF in C#
You can also customize the Word to PDF conversion by specifying the range of pages you want to get in the resultant PDF document. You can either convert first N pages or define a range of pages specifying the starting page’s index. The following code sample shows how to convert 3 pages starting from the second page of Word document to PDF in C#.
Convert Word DOCX/DOC to PDF with a Particular PDF Standard in C#
PDF documents may compliant with different PDF standards such as PDF 1.7, PDF 1.5, PDF/A-1a, etc. In accordance with this, Aspose.Words for .NET allows specifying the PDF compliance level in Word to PDF conversion. You can specify the compliance level using PdfCompliance enum. The following code sample shows how to set the compliance level in Word DOCX to PDF conversion in C#.
Convert Word to PDF with Text/Image Compression in C#
The PDF format supports text and image compression to reduce the size of the document. Since a PDF document could be bulky in size, the compression of its content makes it easy to upload, share, print, etc. The text and image compression can also be applied in Word to PDF conversion using Aspose.Words for .NET.
Text Compression Options
The following text compression options are available in PdfTextCompression enum.
- None: Saving PDF without text compression.
- Flate: Saving PDF with flate (ZIP) compression.
The following image compression options are available in PdfImageCompression enum.
- Auto: The API automatically selects the most appropriate compression for every image in the document.
- Jpeg: Compression to JPEG images (does not support transparency).
The following code sample shows how to convert Word DOCX to PDF with text and image compression in C#.
C# Word to PDF Conversion with a Digital Signature
You can also apply a digital signature to the output PDF while performing Word to PDF conversion. The CertificateHolder class allows you to specify the digital certificate and the password. The PdfDigitalSignatureDetails class allows providing the details of the digital signature such as certificate holder, reason, location and date/time. The following code sample shows how to convert Word DOC/DOCX to PDF with a digital signature in C#.
Convert Word to PDF with Desired JPEG Quality in C#
You can also specify the desired JPEG quality you want to get in the converted PDF document using PdfSaveOptions.JpegQuality property. The value of JpegQuality may vary from 0 to 100 where 0 means the worst quality but maximum compression and 100 means the best quality but minimum compression.
The following code sample shows how to set JPEG quality when converting Word DOCX to PDF in C#.
In this article, you have learned how to convert MS Word DOCX/DOC documents using C# with various options. Visit documentation of Aspose.Words for .NET to see the complete set of features offered by the API. For any questions or queries, contact us via our forum.