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,
Hey,
I do that already but it doesn’t work. Note that I’m using Aspose.Words 11.3.
Regards,
Dragos
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,
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,
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,
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”);
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,
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”);
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,