Index format

We generate long documents and we need to add an index.
The only way we found we can do that in Words for Java is to use:

builder.insertField(FieldType.FIELD_INDEX, false);

The result is awful: the items are followed by a comma, and the whole list of pages separated with comma.
We’d like to have it formatted like in word if we insert (there’s a tab between the quotes):

{INDEX \e " " \c "1"}

So we tried creating the index with:

builder.insertField("INDEX \\e \"\t\" \\c \"1\"");

but that actually makes aspose crash with a null pointer exception as soon as we do:

doc.updateFields();

Is there any way we can add these formatting specifiers for the otherwise very ugly default index?

We also couldn’t find a way to have another default in a word template that we’d load.

Thanks for any help you could give us

Yves

@yvesb,

Thanks for your inquiry. Please manually create your expected Word document using Microsoft Word, Zip and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

out.zip (10.4 KB)
Here it is, thanks for investigating.
Yves

@yvesb,

Thanks for sharing the document.

We have tested the scenario using latest version of Aspose.Words for Java 17.8 with following code example and have not found the shared issue. Please use Aspose.Words for Java 17.8.

Document doc = new Document(MyDir + "out.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.writeln();
builder.insertField("INDEX \\e \"\t\" \\c \"1\"");
doc.updateFields();
doc.save(MyDir + "output.docx", SaveFormat.DOCX );

@yvesb,

Unfortunately, Aspose.Words does not support the requested feature at the moment. However, we have logged this feature request as WORDSNET-15773 in our issue tracking system to right align the page numbers in index field. You will be notified via this forum thread once this feature is available.We apologize for your inconvenience.

Hello Tahir,

Thanks for getting back to us.
I think your code doesn’t crash because there’s nothing in the index.

My full test is this:

Document doc = new Document();
DocumentBuilder builder = new com.aspose.words.DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.writeln();
builder.insertField("INDEX \\e \"\t\" \\c \"1\"");
builder.insertBreak(BreakType.PAGE_BREAK);

String ref = "REQ-1";
builder.insertField("XE \"" + ref + "\"");        
builder.write("Here is the first " + ref);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertField("XE \"" + ref + "\"");        
builder.write("Here is the 2nd " + ref);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertField("XE \"" + ref + "\"");        
builder.write("Here is the 3rd " + ref);
doc.updateFields();

doc.save("out.docx");

It does crash on the updateFields() call with a java.lang.RuntimeException: java.lang.NullPointerException

Yves
PS: I am using 17.8

@yvesb,

Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-15778 . You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@yvesb,

The issues you have found earlier (filed as WORDSNET-15778) have been fixed in this Aspose.Words for .NET 17.9 update and this Aspose.Words for Java 17.9 update.

Thanks Awais,
Now aspose 17.11 doesn’t crash on that code anymore, but the index doesn’t have any pointer to the actual markers. The only line in the index says the item appears in page 0 (zero).
Yves

@yvesb,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16225. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

Could you please share the Java version that you are using at your end? Thanks for your cooperation.

Hello Tahir,
Thanks for the follow up.
I’m using Java 1.8.0.141 on a Windows 10 system

Yves

@yvesb,

Thanks for sharing the detail. We will inform you via this forum thread once there is any update available on this issue.

@yvesb,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-16225) as ‘Not a Bug’. Please use following solution to get the desired output.

  1. Use the DocumentBuilder.InsertField(fieldCode, fieldValue) overload.

builder.InsertField("INDEX \\e \"\t\" \\c \"1\"", null);

  1. Call the Document.UpdatePageLayout method before the Document.UpdateFields method.

    doc.UpdatePageLayout();
    doc.UpdateFields();

Please check the following complete code example.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();
builder.Writeln();
builder.InsertField("INDEX \\e \"\t\" \\c \"1\"", null);
builder.InsertBreak(BreakType.PageBreak);

String Ref = "REQ-1";
builder.InsertField("XE \"" + Ref + "\"");
builder.Write("Here is the first " + Ref);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertField("XE \"" + Ref + "\"");
builder.Write("Here is the 2nd " + Ref);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertField("XE \"" + Ref + "\"");
builder.Write("Here is the 3rd " + Ref);

doc.UpdatePageLayout();
doc.UpdateFields();

doc.Save(MyDir + "output.docx");