Numeric header


#1

Dear Support,
I am testing the aspose library now and all seems to work.
Right now there is one challenge where I have on idea if this can be solved by the library.

Following situation:
I am merging documents with headers.
This works fine and TOC looks good as well.
But I would like to change the TOC and header layout now from a normal list to a numeric TOC and numeric header.

So instead of (+ just indicates the layout of the TOC)
+Chapter Book
++Book Action
++Book Science Fiction
+Chapter Movie
++Action Movies
++Science Fiction Movie

I would like to have:

  1. Chapter Book
    1.1 Book Action
    1.2 Book Science Fiction
  2. Chapter Movie
    2.1 Action Movies
    2.2 Science Fiction Movie

Is it possible to update the Headers and therefore the TOC like this?

Any sample source would be appriciated.

Thank you


#2

@kai20511,

Thanks for your inquiry. Please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document (DOCX file) showing the undesired behavior
  • Your expected document showing the correct output such that when you Update Fields by using MS Word the layout/formatting of said TOC field remain preserved. You can create this document by using MS Word.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.


#3

Thank you @awais.hafeez
Attached you can find the root sample document and the src.
You can find the result.docx, thats how it looks like.
There is a good.docx, thats what is the minimum requirement (only the TOC is numbered).
Or the perfect once, which has a numbered TOC and also the headers on each page are numbered.

Any help is appriciated.

Test2.zip (115.5 KB)


#4

@kai20511,

We are checking this scenario and will get back to you soon.


#5

@kai20511,

You can build on the following code to get the results as shown in your “Good.docx”.

Document doc = new Document("D:\\temp\\Test2\\Result.docx");

List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);

list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001");

for (Field field : doc.getRange().getFields()) {
    if (field.getType() == (FieldType.FIELD_HYPERLINK)) {
        FieldHyperlink hyperlink = (FieldHyperlink) field;
        if (hyperlink.getSubAddress() != null && hyperlink.getSubAddress().startsWith("_Toc")) {
            Paragraph tocItem = (Paragraph) field.getStart().getAncestor(NodeType.PARAGRAPH);
            if (tocItem != null) {
                if (Double.compare(tocItem.getParagraphFormat().getLeftIndent(), 0) == 0) {
                    tocItem.getListFormat().setList(list);
                    tocItem.getListFormat().setListLevelNumber(0);
                }
                if (Double.compare(tocItem.getParagraphFormat().getLeftIndent(), 11.0) == 0) {
                    tocItem.getListFormat().setList(list);
                    tocItem.getListFormat().setListLevelNumber(1);
                }
            }
        }
    }
}

doc.save("D:\\temp\\Test2\\18.6.docx");

#6

@awais.hafeez
Your code is working fine, many thanks.
Just the question, would it be possible to achive numeric header as well (as in perfect.docx)?

I could also change my templates and already add a number in there, but the “update” methods do not update the numbers, they just update the TOC,


#7

@kai20511,

Sure, please adjust the second list/paragraph properties to get the desired positioning and indent:

Document doc = new Document("D:\\temp\\Test2\\Result.docx");

List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001");

for (Field field : doc.getRange().getFields()) {
    if (field.getType() == (FieldType.FIELD_HYPERLINK)) {
        FieldHyperlink hyperlink = (FieldHyperlink) field;
        if (hyperlink.getSubAddress() != null && hyperlink.getSubAddress().startsWith("_Toc")) {
            Paragraph tocItem = (Paragraph) field.getStart().getAncestor(NodeType.PARAGRAPH);
            if (tocItem != null) {
                if (Double.compare(tocItem.getParagraphFormat().getLeftIndent(), 0) == 0) {
                    tocItem.getListFormat().setList(list);
                    tocItem.getListFormat().setListLevelNumber(0);
                }
                if (Double.compare(tocItem.getParagraphFormat().getLeftIndent(), 11.0) == 0) {
                    tocItem.getListFormat().setList(list);
                    tocItem.getListFormat().setListLevelNumber(1);
                }
            }
        }
    }
}

List list2 = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
list2.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list2.getListLevels().get(1).setNumberFormat("\u0000.\u0001");

for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    if (para.getParagraphFormat().getStyleName().equals("Heading 1_0")) {
        para.getListFormat().setList(list2);
        para.getListFormat().setListLevelNumber(0);
    }
    if (para.getParagraphFormat().getStyleName().equals("Heading 2_0")) {
        para.getListFormat().setList(list2);
        para.getListFormat().setListLevelNumber(1);
    }
}

doc.save("D:\\temp\\Test2\\18.6.docx");

#8

@awais.hafeez

It is runing really good, and I am on the last bit to fix all problems now.
I am als using the trial now and layout and all I needed is running fine.
Can I still ask you a little more please?

Your code works well with Header1 nd Header 2, but now after I am getting closer to the final document, I figured out I need up to Header 5.

Can I ask you to help me on that as well?

  • 1
  • 1.1.
  • 1.2
  • 2
  • 2.1
  • 2.2
  • 2.3
  • 3
  • 3.1
  • 3.1.1
  • 3.2
  • 4
  • 4.1
  • 4.1.1
  • 4.1.1.1
  • 4.1.1.1.1
  • 4.1.1.1.2
  • 4.1.2

Many thanks for your help so far.
Really appriciated.


#9

@awais.hafeez,

The above code configures only the first List Level. In that case, you just need to configure more List Levels for the List. You can define up to 9 List Levels for a List. After that, just set the correct value of List Level Number for a particular heading paragraph. Hope, this helps.


#10

@awais.hafeez
Unfortunatly I was unsuccessfull.
May I ask if you could share the final source to achive 5 level for TOC and headers?
That would be really helpfull please.

Many Thanks


#11

@awais.hafeez
Another option.
In case I would have a numeric header in my template, is there a method, like updateListLabels which could update the numbers in its List?


#12

@kai20511,

Thanks for your inquiry. Please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document (DOCX file) showing the undesired behavior
  • Your expected document showing the correct output such that when you Update Fields by using MS Word the layout/formatting of said TOC field remain preserved. You can create this document by using MS Word.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.


#13

@awais.hafeez
You can close this ticket.
I found an “easier way” to solve it now by using the MS internal function.
Aspose is great as it supports to take over the layout from the cover page…

Please see: Bug with Office 2016 MailMerge and USE_DESTINATION_STYLES

Many thanks.


#14

@kai20511,

It is great that you were able to resolve this issue on your end. Please let us know anytime you have any further queries.