How to generate PDF report starting from Word document?
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.
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.
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.
The following simple sample with complete code snippets demonstrates on how to generate PDF report starting from Word document using Essential® DocIO and PDF.