Removing empty TOCs/TOTs/TOFs

Hey support,

empty TOT/TOF/TOCs get saved to PDF as "No Table of Contents entries found. This is not necessarily incorrect but for PDF it’s annoying especially since you cannot edit the document. Is there a way ( some flag) to avoid this from happening ? If not, what is the right way to programmatically remove empty TOC/TOT/TOFs before saving the file?

Regards,
Dragos

Hi
Dragos,


Thanks for your inquiry. To remove ‘No table of contents entries found’, you should simply call Document.UpdateFields method before saving the document to PDF format.

Best Regards,

Hey,

I do that already but it doesn’t work. Note that I’m using Aspose.Words 11.3.

Regards,
Dragos

Hi Dragos,

Thanks for the additional information. I checked the scenario using Aspose.Words v11.7.0 on my side. The UpdateFields method serves the purpose. I would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link:

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi,

given the history we have with upgrading Aspose, lots of regressions, the upgrade path is not a solution. How can I do what I need with 11.3?

Regards,
Dragos

Hi Dragos,


Thanks for your inquiry and apologize for the inconvenience you have encountered so far. Unfortunately, we don’t provide support for older released versions of Aspose.Words. Also, please note that we do not provide any fixes or patches for old versions of Aspose products either. All fixes and new features are always added into new versions of our products. So you will have to upgrade if you need new features or fixes. However, could you please list here the threads you’ve reported those regressions in before?

Best Regards,

So you are telling me that the Aspose version release this May 2012 is no longer supported in September? Is that really the case?

Second, I have not asked for a patch but information on APIs that can do that or guidance on how to programatically determine if a TOC/TOF/TOT is empty. Since you do not seem willing to provide this can you or one of your colleagues at least confirm that the following approach is correct?

private static final String NO_TABLE_OF_CONTENTS_ENTRIES_FOUND = “No table of contents entries found.”; //$NON-NLS-1$
private static final String NO_TABLE_OF_FIGURES_ENTRIES_FOUND = “No table of figures entries found.”; //$NON-NLS-1$

/**
* Replaces the “no tables of xxx entries found” messages generated for empty TOCs/TOTs/TOFs with an empty string.
* Remove it once Aspose fixes it
*
*/
private void hideEmptyTocs(Document doc)
{
// Select all field start nodes so we can find the merge fields.
for (Object it: doc.getChildNodes( NodeType.FIELD_START, true, false))
{
FieldStart fieldStart = (FieldStart)it;
if (fieldStart.getFieldType() == FieldType.FIELD_TOC)
{
Node node = fieldStart.getNextSibling();
while ( node != null && (node.getNodeType() != NodeType.FIELD_END) && (node.getNodeType() != NodeType.FIELD_START))
{
if ( (node.getNodeType() == NodeType.RUN) && ( node.getText().equals( NO_TABLE_OF_CONTENTS_ENTRIES_FOUND) || node.getText().equals( NO_TABLE_OF_FIGURES_ENTRIES_FOUND)))
{
try
{
Run msgRun = (Run) node;
msgRun.setText(""); //$NON-NLS-1$
}
catch (Exception e)
{
}

break;
}

node = node.getNextSibling();
}
}
}
}

/Dragos

Hi Dragos,


Thanks for your inquiry.

Actually, what Awais meant is all bug fixes and new features are added on to the latest versions of our components. We do not patch or include features for older versions.

However that is a different topic as the issue here is not really a bug. If the document you are converting to PDF has a TOC but does not have any entries then the message “No table of content entries found” is correct, from your first post I believe you agree with that.

In order to avoid this appearing in your converted documents you need to remove the TOCs as you have correctly started working on. The code you have provided looks correct for its purpose so please let us know if there are any problems and we will assist.

Note that in a future version of Aspose.Words we will provide a public field API which will make this task much easier (one or two line to remove the TOC). This feature is currently being worked on, however there is not ETA yet.

Please let us know if we can help with anything else.

Thanks,

Hi Awais/Adam,

I still see “No table of contents entries found.” in the PDF generated by Aspose.Words.For.Java v13.3 despite calling updateFields().

Document doc = new Document();

DocumentBuilder docBuilder = new DocumentBuilder(doc);

docBuilder.insertTableOfContents("\o “1-3” \h \z \u");
doc.updateFields();
doc.save(“c:\test.pdf”);

Thanks,
Kumar

Hi Kumar,


Thanks for your inquiry. Well, the latest version of Aspose.Words (13.3.0) mimics the behavior of Microsoft Word when rendering document containing the empty TOC to PDF format. However, if you would like to remove such TOC fields, please try running the following code snippet:
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

Field toc = docBuilder.insertTableOfContents("\o “1-3<font color=“PURPLE”>” \h \z \u");
doc.updateFields();

if (toc.getResult().equals(“No table of contents entries found.”))
toc.remove();

doc.save(“c:\test.pdf”);

I hope, this helps.

Best regards,

Thanks Awais.

I’ll be able to perform toc.remove() just before saving the PDF and after doc.updateFields() is run. How do I get toc as com.aspose.words.Field type from doc.getChildNodes(NodeType.****).

Could you give a piece of code where I could traverse doc object and then remove if the Field is toc and it’s empty?

Thanks,
Kumar

Hi Kumar,


Thanks for your inquiry. Please try run the following code snippet to be able to access and remove TOC fields from document:
Document doc = new Document(“C:\Temp\in.docx”);

for (Field field : doc.getRange().getFields()){
if(field.getStart().getFieldType() == FieldType.FIELD_TOC)
field.remove();
}

doc.save(“C:\Temp\out.docx”);

I hope, this helps.

Best regards,

Hi Awais,

This worked with a minor change. Thanks.
The minor change is that doc.getRange().geFields() wasn’t getting either “table of tables” or “table of figures”. Hence had to use doc.getChildNodes(NodeType.FIELD_START, true).

for (Object it : doc.getChildNodes(NodeType.FIELD_START, true))
{
Field field = ((FieldStart) it).getField();
if (field != null && field.getStart().getFieldType() == FieldType.FIELD_TOC)
{
// For TOF and TOT, checking for endsWith because there is a
// newline at the beginning of the text in the field content.
if (field.getResult().equals(“No table of contents entries found.”) || field.getResult().endsWith(“No table of figures entries found.”))
{
field.remove();
}
}
}

Thanks,
Kumar

Hi Kumar,


Thanks for your feedback. It’s great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,