Search code examples
c#syncfusion

DocToPdfConverter omitting Page/NumPages fields


In our application we have a couple document exports which are generated by reading a Word document, replacing a number of placeholders and then either return the modified word document directly or converting it to PDF using Syncfusion DocIO and their DocToPdfConverter and return that. So far so good.

The problem is that in the generated PDF instead of page numbers there is only blank text.

e.g. if the word document contains

[Field:Page / Field:NumPages] (i.e. [5 / 7])

the generated PDF will instead contain

[ / ]

The conversion code looks like this (bytes is the previously generated word document, which still contains the Page/Numpages fields):

private string ReturnPdf(string fileName, byte[] bytes)
{
    using (var wordStream = new MemoryStream(bytes))
    {
        var wordDoc = new WordDocument(wordStream);

        var converter = new DocToPDFConverter();
        PdfDocument pdfDoc = converter.ConvertToPDF(wordDoc);

        // return directly in http response instead of uploading to blob storage and returning link for one-time usage
        pdfDoc.Save(Path.ChangeExtension(Path.GetFileName(fileName), ".pdf"), HttpContext.Current.Response, HttpReadType.Save);
    }

    return null;
}

Am I missing something obvious or is this actually a bug in the converter, and if it is indeed a bug, are there any known workarounds or good alternatives (preferrably free, but not necessarily so)?

I also tried adding the following before the conversion, to no avail:

wordDoc.UpdateDocumentFields();
wordDoc.UpdateWordCount();
wordDoc.UpdateWordCount(performlayout: true);
wordDoc.UpdateTableOfContents();

EDIT

By some playing around I found out that one possible workaround seems to be to edit the generated .docx file in Word before converting it. Unfortunately, that's not an option for various reasons.


Solution

  • We now use the Winnovative Word To PDF Converter for .NET, which does exactly what we want not only in this case but also in a couple other cases where the Syncfusion converter screwed up.