Convert Word document to PDF in Windows Forms
29 Nov 20244 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 a Word document to PDF in Windows Forms.
Steps to convert Word document to PDF in Windows Forms
Step 1: Create a new Windows Forms application project.
Step 2: Install Syncfusion.DocToPdfConverter.WinForms NuGet package as a reference to your Windows Forms application from the 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 the Form1.Designer.cs file.
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
Step 4: Add a new button in Form1.Designer.cs to create Word file as follows.
private Button btnCreate;
private Label label;
private void InitializeComponent()
{
label = new Label();
btnCreate = new Button();
//Label
label.Location = new System.Drawing.Point(0, 40);
label.Size = new System.Drawing.Size(426, 35);
label.Text = "Click the button to Convert Word document to PDF generated by Essential DocIO. Please note that Microsoft Word Viewer or Microsoft Word is required to view the resultant Word document";
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//Button
btnCreate.Location = new System.Drawing.Point(180, 110);
btnCreate.Size = new System.Drawing.Size(85, 36);
btnCreate.Text = "Convert Word document to PDF";
btnCreate.Click += new EventHandler(btnConvert_Click);
//Create Word
ClientSize = new System.Drawing.Size(450, 150);
Controls.Add(label);
Controls.Add(btnCreate);
Text = "Convert Word document to PDF";
}
Step 5: Add the following code in btnConvert_Click to convert Word document to PDF with simple text.
//Load the existing Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Data/Input.docx"), FormatType.Docx))
{
//Instantiation of DocToPDFConverter for Word to PDF conversion
using (DocToPDFConverter converter = new DocToPDFConverter())
{
//Converts Word document into PDF document
using (PdfDocument pdfDocument = converter.ConvertToPDF(document))
{
//Saves the PDF document
pdfDocument.Save(Path.GetFullPath(@"../../Sample.pdf"));
}
};
}
You can download a complete working sample from GitHub.
By executing the program, you will get the PDF document as follows.
Click here to explore the rich set of Syncfusion® Word library (DocIO) features.
An online sample link to convert Word document to PDF in ASP.NET Core.