BoldSign®Effortlessly integrate e-signatures into your app with the BoldSign® API. Create a sandbox account!
Hello,
I have created the attached document using Syncfusion.WordDocument and saved the file. As you can see, the page numbers in the table of contents do not correspond with the correct pages in the report. When I display this document as a pdf using the following code, the page numbers are correct. How can I update the TOC so that the values in the word document are correct?
using (DocToPDFConverter converter = new())
{
converter.Settings = new DocToPDFConverterSettings()
{
ExportBookmarks = ExportBookmarkType.Bookmarks,
EmbedFonts = true,
EmbedCompleteFonts = true
};
//Converts Word document into PDF document
using (PdfDocument pdfDocument = converter.ConvertToPDF(document))
{
//Saves the PDF document
pdfDocument.Save(fileName);
}
};
Hi Natalie,
Regarding your query: "How can I update the TOC so that the values in
the Word document are correct?"
To update the Table of Contents (TOC) page numbers in your Word document using
DocIO, you can use the UpdateTableOfContents
method. This will ensure the page numbers in the TOC reflect the correct pages
in the document.
Refer to the below code snippet and for more details refer to the documentation.
using (FileStream inputDocStream = new FileStream("Report.docx", FileMode.Open, FileAccess.Read)) { using (WordDocument wordDocument = new WordDocument(inputDocStream, FormatType.Docx)) { // Update the Table of Contents wordDocument.UpdateTableOfContents();
// Save the updated Word document using (FileStream outputDocStream = new FileStream("Result.docx", FileMode.Create, FileAccess.Write)) { wordDocument.Save(outputDocStream, FormatType.Docx); }
// Convert the Word document to PDF using (DocToPDFConverter pdfConverter = new DocToPDFConverter()) { using (PdfDocument pdfDocument = pdfConverter.ConvertToPDF(wordDocument)) { using (FileStream outputPdfStream = new FileStream("Result.pdf", FileMode.Create, FileAccess.Write)) { pdfDocument.Save(outputPdfStream); } } } } } |
For your reference, we have attached the DOCX and PDF document which generated at
our end in the below attachment.
Regards,
Dharanya.
Hi Dharanya,
This code doesn't seem to work because it does not close the word document before re-saving it so I get an error each time.
I am able to get it to work by adjusting the file name here:
using (FileStream outputDocStream = new FileStream("Result1.docx", FileMode.Create, FileAccess.Write))
However, this method requires me to create the wordDocument, save it, read it, update the TOC, then resave which are all methods that take a bit of time. Is there another way to simply update the TOC before saving it? Calling document.UpdateTableOfContents does not work in this context:
var document = BuildSyncfusionReport(serviceProvider, documentInfo);
wordDocument.UpdateTableOfContents();
document.Save(fileName);
but it does work when i save, re-open, update TOC, and then save again with another file name.
Natalie,
Regarding ‘Is there another way to simply update the TOC before saving it?’:
You can create the Word document programmatically with TOC and then update the table of contents before saving it. There is no need to reopen the document to update the Table of Contents.
Regarding ‘Calling document.UpdateTableOfContents does not work in this context:’:
To investigate this problem, we need to replicate the same document creation process. Could you please share the code used in the method ‘BuildSyncfusionReport(serviceProvider, documentInfo)’ as runnable sample?
If you are using any data from a database or another resources, kindly replace it with dummy values (like a list or dictionary) and share a runnable sample with us.
Regards,
Sivakumar. A
Hello,
I responded to this email via forum-support@syncfusion.com on Friday because it included a zip file of a sample project and have not yet received a reply. Can you confirm that it was received?
Natalie,
Yes, we received the sample project that you sent. Upon reviewing the sample,
we were able to reproduce the reported issue: “TOC page number not preserved
properly after UpdateTOC” on our end. We will validate this issue and
provide you with more details within two days.
Regards,
Sindhu Ramesh.
Natalie,
While calling the UpdateTableOfContents() API, we are internally processing our
Word-to-PDF layout engine and updating the page numbers in the table of
contents (TOC) based on this engine. Upon initial investigation, we discovered
that during the Word-to-PDF layout process, a specific paragraph is placed on
the fourth page instead of the third page. As a result, we are updating the
incorrect page number in the Word document. We are currently validating this
issue and will share further details within two days.
Regards,
Sindhu Ramesh.
Natalie,
Upon further validation, we found that while processing the document you
created using our Word to PDF layout engine, invalid padding was applied to the
table cell. As a result, the row height increased, causing the paragraph to
move to the fourth page, which led to the issue.
We have confirmed that the reported issue “Table cell padding is
not applied properly while converting a Word document to PDF” is a
defect and we have logged a defect report. We will include the fix for this
defect in our Weekly NuGet release which is estimated to be available on second
week of February 2025.
The status of this bug can be tracked through the below link:
https://www.syncfusion.com/feedback/65023/table-cell-padding-is-not-applied-properly-while-converting-a-word-document-to-pdf
Note: Once this issue is fixed, the page number will be preserved
properly after updating the TOC.
Disclaimer: “Inclusion of this solution in the weekly release may change due to
other factors including but not limited to QA checks and works
reprioritization.”
Natalie, we were unable to include the fix for the issue "Reference field result not preserved properly after calling the UpdateDocumentFields API" as promised in the weekly release on second week of February 2025. During testing, we identified issues that require additional time to resolve. This fix will be included in the next weekly release on third week of February 2025.
We apologize for any inconvenience this may have caused and appreciate your understanding.
Natalie,
As promised earlier, we have included the fix for the reported issue with “Table
cell padding is not applied properly while converting a Word document to PDF"
in our latest weekly NuGet release (v28.2.6).
Root cause of the issue:
Padding in row is not serialized while saving as DOCX document. So the padding
not preserved
Please use the below link to download our latest weekly NuGet:
Syncfusion.DocToPDFConverter.Wpf
The status of this bug can be tracked through the below link:
Table
cell padding is not applied properly while converting a Word document to PDF in
WPF | Feedback Portal
Note: We will include this fix in our 2025 Volume 1 Main release, which will be
available at the end of March 2025.