Convert Word to PDF in Console application
29 Nov 20245 minutes to read
Syncfusion® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert Word to PDF in Console application.
Convert Word to PDF using .NET Core and Latest
The below steps illustrates convert Word to PDF in console application using .NET Core.
Step 1: Create a new .NET Core console application project.
Step 2: Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your project from NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Step 3: Include the following namespaces in Program.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
Step 4: Include the below code snippet in Program.cs to convert Word to PDF.
//Open the file as Stream.
using (FileStream docStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
//Load file stream into Word document.
using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
{
//Instantiation of DocIORenderer for Word to PDF conversion.
using (DocIORenderer render = new DocIORenderer())
{
//Convert Word document into PDF document.
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
//Save the PDF document.
using(FileStream stream = new FileStream("Sample.pdf", FileMode.Create, FileAccess.Write))
{
pdfDocument.Save(stream);
}
}
}
}
You can download a complete working sample from GitHub.
By executing the program, you will get the PDF as follows.
Convert Word to PDF in .NET Framework
The below steps illustrates creating a simple Word document in console application using .NET Framework.
Step 1: Create a new .NET FrameWork console application project.
Step 2: Install Syncfusion.DocToPdfConverter.WinForms NuGet package as a reference to your Windows Forms application from the NuGet.org.
NOTE
- The Syncfusion.DocToPdfConverter.WinForms is a dependency for Syncfusion® Windows Forms GUI controls and is named with the suffix “WinForms”. It contains platform-independent .NET Framework assemblies (compatible with versions 4.0, 4.5, 4.5.1, and 4.6) for the Word library and does not include any Windows Forms-related references or code. Therefore, we recommend using this package for .NET Framework Console applications.
- Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Step 3: Include the following namespaces in Program.cs file.
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
Step 5: Include the below code snippet in Program.cs to convert Word to PDF.
//Load the existing Word document.
using (WordDocument document = new WordDocument("Data/Input.docx", FormatType.Docx))
{
//Instantiation of DocToPDFConverter for Word to PDF conversion.
using (DocToPDFConverter converter = new DocToPDFConverter())
{
//Convert Word document into PDF document.
using (PdfDocument pdfDocument = converter.ConvertToPDF(document))
{
//Save the PDF document.
pdfDocument.Save("Sample.pdf");
}
}
}
You can download a complete working sample from GitHub.
By executing the program, you will get the PDF as follows.
An online sample link to convert Word document to PDF in ASP.NET Core.