MS Word is an excellent choice for creating and editing text documents. However, there are times when you need to share your documents with others, and you want to ensure that the formatting and content remain consistent, regardless of the recipient’s software or operating system. PDF files are widely used for their compatibility and ease of sharing, and they ensure that the document’s formatting remains consistent across different devices and platforms. Therefore, Word documents are converted to PDF before they are shared. In this blog, we’ll explore how to convert Word documents to PDF in C# effortlessly.
- Convert Word 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#
- Word to PDF with a digital signature in C#
- Word to PDF conversion with the desired JPEG quality for images
C# Library to Convert Word DOC to PDF
Aspose.Words for .NET is a powerful library that makes it easy to convert Word documents to PDF, along with various other document processing tasks. 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 the Package Manager Console
PM> Install-Package Aspose.Words
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.
Convert a Word DOC to PDF in C#
To convert a Word document to PDF, just follow the below simple steps.
- Load the Word DOC/DOCX document using Document class,
- Invoke the Document.Save() method and specify the output file name with the “.pdf” extension.
The following code sample shows how to convert Word document to PDF in C#.
Word Document
Converted PDF
Convert Selected Pages of Word Document to PDF
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 to PDF in C#.
Convert Word to PDF with Compliance 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 to PDF conversion in C#.
Apply Text/Image Compression in Word to PDF Conversion
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.
Image 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 to PDF with text and image compression in C#.
Use Digital Signature in Word to PDF Conversion
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 to PDF with a digital signature in C#.
C# Convert Word to PDF with JPEG Quality
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 to PDF in C#.
Get a Free License
Get a free temporary license and convert Word documents to PDF without evaluation limitations.
Convert Word to PDF Online
Try our free online app to convert Word documents to PDF with high fidelity and accuracy. No need to create any account or buy the subscription.
Conclusion
Converting Word documents to PDF is a common requirement in many applications, and Aspose.Words for .NET simplifies the process, allowing you to achieve consistent and reliable results. In this blog post, we’ve demonstrated how to convert Word documents to PDF in a C# application. We also covered the advanced features of the library to customize Word to PDF conversion.
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.