Insert TOC and orientation change

Hi All,

I am using Aspose Words 13.4.0 to try to insert a TOC into an existing document and trying to modify the orientation of the page containing the TOC, rest of the pages should be unaffected.

I am attaching the code I have used to achieve this and also the source and target documents. I see the following problems :

  1. When I set the orientation to landscape, not only the first page but 4 other pages ( Page 1- 5) show in landscape.

  2. The original document does contain a TOC at the very beginning, but after I insert another TOC into the doc, the existing TOC appears in Page 4 instead of Page 2. The newly added TOC seems to have replaced the existing one instead of being added before it.

Can you please take a look and advise?

Thanks,
Smitha

Hi Smitha,

Thanks for your inquiry. I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-8217 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

Please use the following code snippet to achieve your requirement.

Document doc = new Document(MyDir + "dev_test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
if (doc.getFirstSection().getBody().getFirstChild().getNodeType() == NodeType.STRUCTURED_DOCUMENT_TAG)
{

    Paragraph para = new Paragraph(doc);
    doc.getFirstSection().getBody().insertBefore(para, doc.getFirstSection().getBody().getFirstChild());
}

builder.moveToDocumentStart();
builder.write("TOC");
builder.getCurrentSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.insertTableOfContents("\\o \"1-7\" \\h \\z \\u");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.getPageSetup().clearFormatting();
doc.updateFields();
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setSaveFormat(SaveFormat.PDF);
doc.save(MyDir + "out.pdf", saveOptions);

Hi Tahir,

Thanks for analyzing the problem. I will try that and update you. I have a couple of other questions :

  1. How do we change the tab leader style for the TOC ? The default is “DottedLine”, I need to change it to “DashedLine”, “Underline” or “None”. I have tried the \p “___” option with builder.insertTableOfContents but it added a single _ between the text and page number.

  2. I was also looking at creating bookmarks based on heading styles used for generating TOC using the attached code. For documents having multi-byte characters in the headings, I see that the TOC is generated fine, but the bookmarks show rectangles instead of these characters. I could not find any way to set encoding options.
    Can you please take a look at the attached sample docs and code and advise?

Thanks,
Smitha

Hi Smitha,

Thanks for your inquiry. Please use the following code snippet to set the TOC TabLeader.

Document doc = new Document(MyDir + "double-byte.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// To demonstate the technique let's configure the first 4 levels of TOC.
Style toc1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1);
Style toc2 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_2);
Style toc3 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_3);
Style toc4 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_4);
Style[] tocStyles = { toc1, toc2, toc3, toc4 };
for (Style style : tocStyles)
{
    style.getParagraphFormat().getTabStops().clear();
    style.getParagraphFormat().getTabStops().add(500, TabAlignment.RIGHT, TabLeader.LINE);
}
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setSaveFormat(SaveFormat.PDF);
OutlineOptions outline = pdfSaveOptions.getOutlineOptions();
outline.setHeadingsOutlineLevels(5);
doc.updateFields();
doc.save(MyDir + "Out.pdf", pdfSaveOptions);

I like to share some more detail about issue WORDSNET-8217. Your document contains the content control (STRUCTURED_DOCUMENT_TAG) at the start of document. The DocumentBuilder.moveToDocumentStart moves the cursor inside the content control. When you insert section break inside contents control, this moves the TOC field contents to page number 4. This is a bug (WORDSNET-8217).

You can use the code shared in my last post to achieve your requirements. Regarding your second query about bookmarks, it would be great if you please share some more detail about this query. I will investigate the issue on my side and provide you more information.

Hi Tahir,

Thanks for the update. I have used the code from your last post to solve problems i had when the start of the document contained controls like Tables as well. It has worked for the documents that I have used till now.

I am using the following code to get Bookmarks when converting a Word doc into PDF ( attached double-byte.docx in my last post) -

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
OutlineOptions outline = pdfSaveOptions.getOutlineOptions();
outline.setHeadingsOutlineLevels(5);
doc.save(filePath +".pdf" , pdfSaveOptions);

I have the text “役割と権限” with H1 style and “ユーザ登録” with H2 Style. But in the PDF output generated this text shows as rectanges (no text visible) in the “Bookmarks” tab (attached double-byte.docx.pdf from my last post).

I am also attaching a screen shot. Please advise.

Thanks,
Smitha

Hi Smitha,

Thanks for sharing the details. Aspose.Words requires TrueType fonts when rendering documents to fixed-page formats. Most likely this problem occurs when you use Aspose.Words on Linux or Mac OS or your MS Word template document uses fonts that are not present on your computer or Aspose.Words cannot locate fonts on your computer. I would suggest you please read the following article:
https://docs.aspose.com/words/java/using-truetype-fonts/

I have attached the output Pdf file generated at my end with this post. If the problem still remains, please share following details for investigation purposes.

What environment are you running on?

  • OS (Windows Version or Linux Version)
  • Architecture (32 / 64 bit)
  • Java version
  • Provide information about your specific culture, such as the name of the culture, language and country/region.

Hi Smitha,

I have received response from our development team about issue WORDSNET-8217. We don’t support section breaks inside content control by design, this is a known limitation. You can achieve your requirement by using code shared in my first post.

Please let us know if you have any more queries.

Hi Tahir,

Thanks for the update.I will use the code from your first post to get it working.

Thanks,
Smitha

Hi Smitha,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi,

This is regarding the TabLeader issue. I coded as per your suggestion and things look good. Thank you for that.
The issue now is, that if there is TOC already present in the word document, then the TabLeader there changes as well.
i.e
If a word document (PFA) has a TOC with dotted TabLeader, then applying your code will change the TabLeader style to Dashed or Underlined (whichever specified).

1 - Can you please let me know how to overcome this?
2 - Can you please let me know how can we determine whether a TOC already exists in the source word document? If TOC exists, then we need to skip TOC creation in output PDF.

Thanks,
Satyendra Acharya

Hi Satyendra,

Thanks for your inquiry. Yes, the code shared in this post change the TabLeader for all table of contents in the document with TOC level 1, 2, 3 and 4.

SmithaBhat:
2 - Can you please let me know how can we determine whether a TOC already exists in the source word document? If TOC exists, then we need to skip TOC creation in output PDF.

You can find the TOC fields in the document by using following code snippet.

Document doc = new Document(MyDir + "table-of-contents-test.doc");
for (FieldStart fStart : (Iterable<FieldStart>)doc.getChildNodes(NodeType.FIELD_START, true))
{
    if (fStart.getFieldType() == FieldType.FIELD_TOC)
    {
        // Toc field exists
        System.out.println(fStart.getField().getFieldCode());
    }
}

SmithaBhat:
1 - Can you please let me know how to overcome this?

Do you want to change the TabLeader for a specific TOC field? Please let us know. We will then provide you more information on this along with code.