How to remove TOC from pdf document

Good day!


I use Aspose.Pdf for java (version 10.4.0) and Aspose.Words (version 15.5.0) libraries. I need to process some docs and if the table of contents in it is empty then remove it otherwise the excess message (“No table of contents entries found.”) is printed. So, at the beginning I create the .docx file and remove TOC by following code:

private Field fieldTOC = null; //is used for memorizing the TOC object of document
private Document doc; //the direct Document object

//some code before

protected void documentBodyEnd() {
updateFields();
boolean tocIsEmpty = true;
for (Object obj : doc.getChildNodes(NodeType.PARAGRAPH, true)) {
if ( !(obj instanceof Paragraph) ) continue;
Paragraph p = (Paragraph)obj;
if (p.getParagraphFormat().getStyleName().contains(“TOC”)) {
for (Run r : p.getRuns()) {
tocIsEmpty = false;
try {
r.getFont().setUnderline(Underline.SINGLE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
if (tocIsEmpty) {
removeTOC();
}
}
private void updateFields() {
try {
doc.updateFields();
} catch (Exception e) {
e.printStackTrace();
}
}

private void removeTOC() {
try {
fieldTOC.remove();
} catch (Exception e) {
e.printStackTrace();
}
}

And that’s OK. After I convert the obtained .docx file to .pdf with using SaveFormat.PDF and as a result receive the .pdf document with above mentioned excess phraze. So, please, show how to remove the TOC from PDF or how to avoid this situation because I can’t find anything in the docs or tutorials. Attaches: word document with removed TOC and .pdf obtained by converting the first one.

Thanks a lot!

Hi Dmitriy,


Thanks for your inquiry. I am afraid currently Aspose.Pdf does not support manipulation of TOC in existing PDF document. We have already logged a feature request PDFNEWJAVA-34354 for the purpose. We have linked your post to the issue id, so we will notify you as soon as it is resolved.

However, We will appreciate it if you please share some more details about your requirements, as per my understanding Aspose.Word is fulfilling your requirements.

We are sorry for the inconvenience caused.

Best Regards,

Well, there is a sufficient functionality for adding TOC in existing or just created document in Aspose.Words and Aspose.Pdf and for manipulating the formatting of TOC. But! There is no obvious way to edit TOC or just remove it from document. I understand, that it’s quite difficult, however this functionality will solve many problems in docflow process.
For example (about editing), I mean some changes when some sections (for example “Abstract” - summary) are not included into TOC. Or not many users want to see the “There were no table of contents entries found” phrase.

Well, there is a sufficient functionality for adding TOC in existing or just created document in Aspose.Words and Aspose.Pdf and for manipulating the formatting of TOC. But! There is no obvious way to edit TOC or just remove it from document. I understand, that it’s quite difficult, however this functionality will solve many problems in docflow process.
For example (about editing), I mean some changes when some sections (for example “Abstract” - summary) are not included into TOC. Or not many users want to see the “There were no table of contents entries found” phrase. May be just to not show empty TOC?

Hi Dmitriy,


Thanks for your feedback. Please note Aspose.Pdf does not support to manipulate(edit/delete) TOC, so we have already logged above ticket for the feature.

However, Aspose.Word supports to manipulate(edit/delte) TOC in DOC(X) documents. As a work around when you converting DOC(X) to PDF, first you can delete TOC entries in DOC(X) and later convert it to PDF. Please let us know if the solution acceptable for you then we can share the code for the purpose.

Best Regards,

Good day!

Yes please! That would be great because this problem was actual for us recently and no warranty it wouldn’t be actual again.

Ok, great, thank you!