)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to generate PDF report starting from Word document?

Platform: WPF |
Control: DocIO |
Published Date: January 19, 2017 |
Last Revised Date: February 5, 2020

You can generate PDF report starting from Word document by using Essential DocIO and Essential PDF. Below steps need to be followed,

  • Create a Word document from scratch
  • Perform Mail merge
  • Convert the Word document to PDF

For more information about the required assemblies, please refer our UG documentation page.

Step 1: Create a Word document from scratch

Create template Word document with merge fields using Essential DocIO.

The following code example shows how to create a Word document template with merge fields.

C#

// Creates a new instance of Word document
WordDocument document = new WordDocument();
// Adds one section and one paragraph to the document
document.EnsureMinimal();
// Sets page margins to the last section of the document
document.LastSection.PageSetup.Margins.All = 72;
// Appends text to the last paragraph
document.LastParagraph.AppendText("Emp_Id: ");
// Appends merge field to the last paragraph
document.LastParagraph.AppendField("Emp_Id", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nName: ");
document.LastParagraph.AppendField("Name", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nPhone: ");
document.LastParagraph.AppendField("Phone", FieldType.FieldMergeField);
document.LastParagraph.AppendText("\nCity: ");
document.LastParagraph.AppendField("City", FieldType.FieldMergeField);
// Saves and closes the WordDocument instance.
document.Save("Template.docx");
document.Close();

 

VB

' Creates an instance of a WordDocument
Dim document As New WordDocument()
' Adds one section and one paragraph to the document
document.EnsureMinimal()
' Sets page margins to the last section of the document
document.LastSection.PageSetup.Margins.All = 72
' Appends text to the last paragraph.
document.LastParagraph.AppendText("Emp_Id: ")
' Appends merge field to the last paragraph
document.LastParagraph.AppendField("Emp_Id", FieldType.FieldMergeField)
document.LastParagraph.AppendText(vbLf & "Name: ")
document.LastParagraph.AppendField("Name", FieldType.FieldMergeField)
document.LastParagraph.AppendText(vbLf & "Phone: ")
document.LastParagraph.AppendField("Phone", FieldType.FieldMergeField)
document.LastParagraph.AppendText(vbLf & "City: ")
document.LastParagraph.AppendField("City", FieldType.FieldMergeField)
' Saves and closes the WordDocument instance.
document.Save("Template.docx")
document.Close()

 

The generated template document looks as follows.

Template Word document

Step 2: Perform Mail merge

The Mail merge operation replaces the matching merge fields with the respective data.

The following code example shows how to perform a simple Mail merge in the generated template document with string array as data source.

C#

// Opens the template document
WordDocument document = new WordDocument("Template.docx");
// Gets the data for Mail merge
string[] fieldNames = new string[] { "Emp_Id", "Name", "Phone", "City" };
string[] fieldValues = new string[] { "1001", "Peter", "+122-2222222", "London" };
// Performs the Mail merge
document.MailMerge.Execute(fieldNames, fieldValues);
// Saves and closes the WordDocument instance.
document.Save("WordReport.docx");
document.Close();

 

VB

' Opens the template document
Dim document As New WordDocument("Template.docx")
' Gets the data for Mail merge
Dim fieldNames As String() = New String() {"Emp_Id", "Name", "Phone", "City"}
Dim fieldValues As String() = New String() {"1001", "Peter", "+122-2222222", "London"}
' Performs the Mail merge
document.MailMerge.Execute(fieldNames, fieldValues)
' Saves and closes the WordDocument instance.
document.Save("WordReport.docx")
document.Close()

 

The resultant document looks as follows.

DocIO generated Word document

Step 3: Convert the Word document to PDF

The following code example illustrates how to convert a Word document into PDF document.

C#

// Opens the template document
WordDocument document = new WordDocument("WordReport.docx");
// Creates an instance of the DocToPDFConverter
DocToPDFConverter converter = new DocToPDFConverter();
// Converts Word document to PDF document
PdfDocument pdfDocument = converter.ConvertToPDF(document);
// Saves the report as PDF document
pdfDocument.Save("PDFReport.pdf");
// Closes the instance of PDF document object
pdfDocument.Close(true);
// Closes the instance of Word document object
document.Close();

 

VB

' Opens the template document
Dim document As New WordDocument("WordReport.docx")
' Creates an instance of the DocToPDFConverter
Dim converter As New DocToPDFConverter()
' Converts Word document into PDF document
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(document)
' Saves the report as PDF document
pdfDocument.Save("PDFReport.pdf")
' Closes the instance of PDF document object
pdfDocument.Close(True)
' Closes the instance of Word document object
document.Close()

 

The generated PDF report looks as follows.

DocIO generated PDF report

The following simple sample with complete code snippets demonstrates on how to generate PDF report starting from Word document using Essential DocIO and PDF.

Sample

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Christopher Joshua
Feb 17, 2018

I will suggest to using ZetPDF to Create, Edit, Convert, Protect or Print PDF documents, without requiring Adobe Acrobat. It's the fastest pdf SKD for .NET applications.  

Reply
Jordan Capa
Apr 23, 2019

this is very helpful! for beginners and want to save time, I found zetpdf.com i heard a lot of this from my co workers. i tried using it nad it's really convenient and easy to use!

Reply
Jordan Capa
Apr 23, 2019

thanks for the information! i recommend using zetpdf.com if you're looking for a very fast and helpful pdf sdk.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon