Editing a Table of Contents

I am building a document with ASPose.Words .NET into a PDF document and then with another tool, merging additional PDF documents to the end to form an Appendix. I would like for these appendix documents to be listed in the original documents Table of Contents.


Is there a way, after I have called UpdateFields() and UpdatePageLayout(), to programmatically append more entries onto the Table Of Contents?

Hi there,


Thanks for your inquiry. After saving document to Pdf, you can not update TOC field using Aspose.Words. Aspose.Words does not load the Pdf into Aspose.Words DOM. Could you please share which API you are using to merge the Pdf files? We will then provide you more information about your query.

I realize that ASPose.Words cannot edit the PDF document. I want to edit the TOC using ASPose.Words BEFORE I save to the PDF document. So I need some code that locates the TOC, finds the last entry, and then adds more TOC entries under it. Then I will SAVE to PDF. The tool I use to merge it afterward is irrelevant.

Hi there,


Thanks for your inquiry. Please note that table of contents is built by Microsoft Word when the field is updated.

Using Aspose.Words, you cannot modify the Pdf. However, you can work with Pdf files using Aspose.Pdf. We are moving this forum thread to Aspose.Pdf forum. Aspose.Pdf team will reply you about your query.

Hi Dennis,


Thanks for your inquriy. I am afraid Aspose.Pdf does not support to edit the TOC at the moment. We have already logged the feature request as PDFNEWNET-37291 in our issue tracking system for the implementation. We have linked your thread to the issue id and will keep you updated about the resolution progress.

Furthermore, as a workaround you can delete TOC in Word document, render it to the PDF, merge the PDF document with other PDF documents and create TOC in your final PDF document. Hopefully it will help you to accomplish the task.

Please feel free to contact us for any further assistance.

Best Regards,

I’m not even using ASPose.PDF. I don’t know why they moved the thread. I wanted to do this with ASPose.Words. Can you move the thread back?


After trying to think out of the box, here is what I came up with:

1. Move to end of document.
2. Add page breaks as needed to “make space” for where the appended PDF documents will go
3. Generate the TOC
4. Delete the blank pages at the end.

Hi Dennis,


Thanks for your feedback and we apologize for the misunderstanding. I have moved your thread back to Aspose.Words forum. My colleague from Aspose.Words will explain you the scenario in details.

Best Regards,

Hi Dennis,


Thanks for your inquiry.
DennisGDayII:
I want to edit the TOC using ASPose.Words BEFORE I save to the PDF document. So I need some code that locates the TOC, finds the last entry, and then adds more TOC entries under it.
Please note that Aspose.Words mimics the same behavior as MS Word does. The table of contents is built by Microsoft Word when the TOC field is updated. You cannot add extra TOC entries by using MS Word and Aspose.Words API.

A table of contents in a Word document can be built in a number of ways and formatted using a variety of options. The way the table is built and displayed by Microsoft Word is controlled by the field switches. For example, after creating a table of contents, the following field is inserted into the document: { TOC \o “1-3” \h \z \u }.
In Microsoft Word, fields are not automatically updated when a document is opened, but you can update fields in a document at any time by pressing F9.

We suggest you please read following article for your kind reference.
DennisGDayII:
1. Move to end of document.
2. Add page breaks as needed to “make space” for where the appended PDF documents will go
3. Generate the TOC
4. Delete the blank pages at the end.
As you want you update table of contents after merging two Pdf files 1) generated by Aspose.Words 2) generated by other third party tool. You cannot add extra TOC entries in Word document. This is because TOC is a field which contains field code like “TOC \o “1-3” \h \z \u”. When you press F9 in MS Word or call Document.UpdateFields using Aspose.Word, the TOC is generated/updated.

In your case, we suggest you following solution. Hope this helps you.

1) Remove TOC field using Aspose.Words
2) Save document to Pdf
3) Merge additional Pdf with Pdf file generated by Aspose.Words. You can merge Pdf files using Aspose.Pdf.
4) Add table of contents using Aspose.Pdf in final/merged Pdf file

Pleas let us know if you have any more queries.

I admire your insistence that this cannot be done in ASPose.Words! After many hours of trial and error, I did come up with a solution. Here it is, and attached is a picture of the TOC.


Dim builder As New DocumentBuilder(asposeDOC)
builder.MoveToDocumentEnd()
builder.InsertBreak(BreakType.SectionBreakNewPage)

’ // Add temporary pages for each attachment
For Each attachment in MyAttachments
For pageNbr As Integer = 1 To attachment.pages
If pageNbr=1 Then
builder.InsertHtml("

" & attachment.FileName & “

”)
End If

builder.InsertHtml("***ATTACHMENT PLACEHOLDER PAGE: PLEASE DELETE")
builder.InsertBreak(BreakType.PageBreak)
Next
End If

asposeDOC.UpdateFields()

’ // Now we need to remove all of those temporary pages.
asposeDOC.LastSection.Remove()

asposeDOC.UpdatePageLayout()

Hi Dennis,


Thanks for your inquiry. Yes, you can insert the contents with heading styles at the end of Word document using Aspose.Words. Wen Document.UpdateFields method is called, these contents with be available in TOC. However, as shared earlier, after merging Pdf file, Aspose.Words does not update the TOC field in Pdf file.

Please let us know if you have any more queries.