We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Chapter Number

Hello,
I’m trying to add Chapter number in the beginning of my template. I created a mergefield for this so that the mergefield is replaced with the Chapter number, but it didn’t work (or is there a way to replace the mergefield with chapter information?) So, I wrote the following code (in Java):

Document ch1Doc = new Document(aBlobStream); //The document stored as a blob data in the database.
ch1Doc.getMailMerge().execute(
new String[] {“STPNumber”, “STPDate”},
new Object[] {stp.getStpnumber(), pubDate}); //There are two mergefields, so I update these fields before inserting the chapter.
DocumentBuilder ch1Builder = new DocumentBuilder(ch1Doc);
Style style = ch1Doc.getStyles().add(StyleType.PARAGRAPH, “chapterStyle”);
ch1Builder.getParagraphFormat().setStyle(ch1Doc.getStyles().get(“Heading 1”));
style.getListFormat().setList(ch1Doc.getLists().add(ListTemplate.OUTLINE_HEADINGS_CHAPTER));
style.getListFormat().setListLevelNumber(0);

Instead of hard coding the chapter number (such as docbuilder.write (“Chapter 1”)), I 'm using Style so that my page number in the footer can be updated as “chapter number -1” format (like 1-1, 1-2, 1-3, etc). When I use the code above, the chapter number is printed out in the document correctely, but the message “Evauluation Only, Created with Aspose.Words…” follows after the chpater number.

Questions:
1. Why the Evaluation message is appeared after the chapter number? Isn’t this message supposed to be printed out in the very beginning of the document?
2. Is there a way to replace a mergefield with this style so that the chapter number appears at a designated location? I don’t want to, again, hard-code the chapter number. I need to use OUTLINE_HEADINGS_CHAPTER so that the page number is updated accordinlgy based on the chapter number.

Please help.
Thanks.
michelle

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. This message appears because you are using Aspose.Words in evaluation mode. I think you should request a 30-day Temporary License to test Aspose.Words without the evaluation version limitations. Please refer to

http://www.aspose.com/corporate/purchase/faqs/temporary-license.aspx

Also please attach your template for testing.

Best regards.

Thanks, Alexey, for your quick response. Here’s my template. This is fairly simple template. Before I request for 30 day temporary license, I’m trying to create a complete document (it is about 200-300 pages of document). My client is already working on the purchase order, but we really need to test the product thoroughly before making the purchase including the performance. So, after I complete all the necessary code, we will probably request for 30 day trial to test its performance.

Anyway, here is my attachment. Please let me know if my code is correct. Thanks!

Michelle

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks you for additional information. Your code is correct.

Evaluation message appears after “Chapter 1” text because this text is a label of list item.

In your code you are using the following line

ch1Builder.getParagraphFormat().setStyle(ch1Doc.getStyles().get("Heading 1"));

This applies Heading 1 style to first paragraph in the document (this first paragraph is evaluation message). An evaluation watermark is inserted at the top of the document on open and save.

Best regards.

Again, thank you so much for your quick response, Alexey. My last question: is there a way to have the chapter number displayed at the place of mergefield, ChapterNumber? If you see my template, there is a mergefield, ChapterNumber, and have the page number in the footer displayed in correct format (such as 1-1, 1-2, 1-3, etc. The pager nuber format is “Chapter Number - Page Number” Can we do that? If I use the code that I posted earlier, everything works out. However, if I try to replace the mergefield with the Chapter Number, the page number just shows “1, 2, 3, etc” w/o the chapter information.
Any idea?

Thanks,
Michelle

Another quick question: When you used a non-demo version, how the Chapter information gets displayed? Is it right before “Introduction” text?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You should use the following code:

Document ch1Doc = new Document("C:\\Temp\\ChapterTemplate.doc");

ch1Doc.getMailMerge().execute(

new String[] { "STPNumber", "STPDate" },

new Object[] { "13", new java.util.Date() });

DocumentBuilder ch1Builder = new DocumentBuilder(ch1Doc);

ch1Builder.moveToMergeField("ChapterNumber");

ch1Builder.getParagraphFormat().clearFormatting();

ch1Builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

ch1Doc.save("C:\\Temp\\out.doc");

This should help.

Best regards.

Great! Thank you so much for your quick response!!

Hi Alexey,
Now, I’m facing different issues. I have several documents that I am going to merge. For example, I have a cover page, table of content page, preface and chapter 1, so and so. When I merge these documents, for some reason, the chapter information doesn’t appear. It is just blank. Please see my attachment with the code below:

SimpleDateFormat formatter = new SimpleDateFormat(“dd MMMM yyyy”);
String pubDate = formatter.format(new java.util.Date());

java.io.InputStream prefaceIS = document.getDocumentContent().getDocumentBlob().getBinaryStream();
Document completeDoc = new Document(prefaceIS);

completeDoc.getMailMerge().execute(
new String[] {“STPNumber”, “STPDate”},
new Object[] {stp.getStpnumber(), pubDate});

java.io.InputStream ch1IS = document.getDocumentContent().getDocumentBlob().getBinaryStream();
Document ch1Doc = new Document(ch1IS );

ch1Doc.getMailMerge().execute(
new String[] {“STPNumber”, “STPDate”},
new Object[] {stp.getStpnumber(), pubDate});

DocumentBuilder ch1Builder = new DocumentBuilder(ch1Doc);
ch1Builder.moveToMergeField(“CHAPTERNUMBER”);
ch1Builder.getParagraphFormat().clearFormatting();
ch1Builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

completeDoc.appendDocument(ch1Doc, ImportFormatMode.USE_DESTINATION_STYLES);

return completeDoc;

The complete doc is now a document ch1Doc appended to preface Document. If you scroll down to view the chapter 1 of the document, the “Chapter 1” does not appear. Why is this? Do you have any idea?

Can you please help?

Thank you in advance.
Michelle


Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. This occurs because you are using ImportFormatMode.USE_DESTINATION_STYLES. Try using the following code:

completeDoc.appendDocument(ch1Doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

This will solve the problem.

Best regards.

Alexey,
Actually, it won’t resolve the issue. If I use KEEP_SOURCE_FORMATTING, then, the style for Chapter is renamed to Heading 1_0 since the preface template uses Heading 1 for its title. Because of this (Heading 1_0), the page number doesn’t recognize this style so the page number doesn’t reflect the chapter information (such 1-1, 1-2, etc). You may ask me to use different style for preface title…but it won’t make the table of content section work… I think this is the dilemma that I’m facing now… …

Would it be there any good solution?
Thanks,
Michelle


Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. The destination document should have the same Heading 1 style as source document has. I modified your “PrefaceTemplate.doc” document. Please see the attachment.

Best regards.

Alexey,
What’s the difference b/w your template and mine? They are the same to me. The template that I attached earlier has Heading 1 style as well. What changes did you make to the template? Could you please explain? The reason that the heading is renamed to Heading 1_0 when I use “KEEP_SOURCE_FORMATTING” is because there are identical styles in both templates…

Thanks,
Michelle

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request and sorry for my bad explanation. I changed Heading 1 style in your template. Click modify style, on the form click “Format” and select “Numbering” from the menu. Here you will see that I applied numbering for “Heading 1” style.

Best regards.

Hi Alexey,
Thanks a lot for your great response. After I modified my template as you explained, it works great!