Convert document to PDF

Is there more than one way to convert a word doc (DOCX) to PDF?

I tried it using DocToPDFConverter, here is the code:

            document.MailMerge.RemoveEmptyParagraphs = true;
            document.MailMerge.ClearFields = true;

            document.MailMerge.Execute(main_table);
            main_table.Dispose();

            document.UpdateDocumentFields();


                DocToPDFConverter converter = new DocToPDFConverter();

                PdfDocument pdfDocument = converter.ConvertToPDF(document);

                MemoryStream ret_file = new MemoryStream();
                pdfDocument.Save(ret_file);

                pdfDocument.Close();
                document.Close();
                pqp_template_stream.Close();

                byte[] bin_file = ret_file.ToArray();
                ret_file.Close();



The word document has 16 pages, but after its converted to PDF it cuts off a lot of pages in the middle of the document, end up to only 7 pages

If you need to word document for you to test, then I need a way to send you the document without posting it here for everyone to see. It's a form that my company uses for projects.

2 Replies 1 reply marked as answer

LB Lokesh Baskar Syncfusion Team June 17, 2021 10:34 AM UTC

Hi Michael,

Thank you for contacting Syncfusion support.

While converting Word to PDF can you please save the document as docx intermediately, then check whether there is an issue in the intermediately saved Word document. Please refer the code snippet given below.  

//Opens an existing Word document. 
WordDocument document = new WordDocument(@"InputTemplateDocument.docx", Syncfusion.DocIO.FormatType.Docx); 
//Sets a value indicating whether to remove paragraphs which contain empty merge fields. 
document.MailMerge.RemoveEmptyParagraphs = true; 
//Sets “ClearFields” to true to remove empty mail merge fields from document. 
document.MailMerge.ClearFields = true; 
//Execute mail merge. 
document.MailMerge.Execute(main_table); 
main_table.Dispose(); 
//Updates the fields present in a document. 
document.UpdateDocumentFields(); 

//Saves the document intermediately. 
document.Save(@"IntermediateDocument.docx",FormatType.Docx); 
//Open the intermediate saved document. 
WordDocument intermediateDocument = new WordDocument(@"IntermediateDocument.docx", Syncfusion.DocIO.FormatType.Docx); 

//Creates an instance of the DocToPDFConverter. 
DocToPDFConverter converter = new DocToPDFConverter(); 
//Converts Word document into PDF document 
PdfDocument pdfDocument = converter.ConvertToPDF(intermediateDocument); 
//Creates an instance of memory stream 
MemoryStream ret_file = new MemoryStream(); 
//Saves the document to stream 
pdfDocument.Save(ret_file); 
pdfDocument.Close(); 
document.Close(); 
intermediateDocument.Close(); 

If there is issue in intermediate Word document itself (“IntermediateDocument.docx”), then it might be due to information in input template and datasource used for mail merge. So, could you please share us the input template and datasource(dummy data). 
If there is no issue in intermediate Word document (“IntermediateDocument.docx”) but issue in output PDF alone means, then we suspect it might be due to problem in Word to PDF conversion process only. So, could you please share us the intermediate Word document, which is enough to investigate. We will try to replicate the same by converting the document to PDF at our end.

Also, could you please share us the Syncfusion product version used at your end. 

Note: 
If the sample or Input template document is confidential and unable to share in the public forum then please share the sample or input document to [email protected]  with the forum id as subject line. We will check internally and update you further.

Please let us know if you have any other questions.

Regards 
Lokesh B 


Marked as answer

MI Michael June 17, 2021 09:15 PM UTC

You provided me the answer :)

When I tried your code, it converted the document to PDF correctly. Then I realized that the solution is to save the document into a memory stream as DOCX, and read it back.

My code now is:

                MemoryStream inter_ret_file = new MemoryStream();
                document.Save(inter_ret_file, FormatType.Docx);

                WordDocument intermediateDocument = new WordDocument(inter_ret_file, Syncfusion.DocIO.FormatType.Docx);
                document.Close();
                inter_ret_file.Close();


                //Converts Word document into PDF document
                DocToPDFConverter converter = new DocToPDFConverter();
                PdfDocument pdfDocument = converter.ConvertToPDF(intermediateDocument);

                MemoryStream ret_file = new MemoryStream();
                pdfDocument.Save(ret_file);

                pdfDocument.Close();
                pqp_template_stream.Close();


Thank You!

Loader.
Up arrow icon