New Field API in 15.1

Trying out the new 15.1 Aspose Word version, I got a NullPointerException on following code :


FieldPageRef fpr = new FieldPageRef();

fpr.setBookmarkName("test");

I get following error on the setbookmark line :

java.lang.NullPointerException

at com.aspose.words.Field.getStart(Unknown Source)

at com.aspose.words.Field.getType(Unknown Source)

at com.aspose.words.zz44.(Unknown Source)

at com.aspose.words.Field.zzZXn(Unknown Source)

at com.aspose.words.Field.zzZX6(Unknown Source)

at com.aspose.words.FieldPageRef.setBookmarkName(Unknown Source)


Also there is an old version of the java API packed with the 15.1 version. Nothing to find about the new Field API...

Hi,


Thanks for your inquiry. Please use the following code to be able to set name of bookmark and other switches for PAGEREF field:
DocumentBuilder builder = new DocumentBuilder();

builder.startBookmark(“Test1”);
builder.write(“Bookmarked text”);
builder.endBookmark(“Test1”);

builder.writeln();

FieldPageRef field = (FieldPageRef)builder.insertField(FieldType.FIELD_PAGE_REF, false);

// We will write the following field code
// PAGEREF Test1 \h \p
field.setBookmarkName(“Test1”);
field.setInsertHyperlink(true);
field.setInsertRelativePosition(true);

field.update();

builder.getDocument().save(getMyDir() + “out.docx”);

I hope, this helps.

Best regards,

I can’t use the DocumentBuilder in this case. We almost never do.

We start from a small template containing all the styles, etc… and build our report from that.


I now inserted a pageref field in my template. During report generation,I fetch it, deepclone it, and then update the value. So that works now.

So is it correct that, in 15.1 you can’t just create a field with a constructor, like my example, and add it to any paragraph, without the need of a builder ?

greetings,




Hi,


Thanks for your inquiry. Well, you can append field to a Paragraph using the following overload of AppendField method:

Paragraph.AppendField Method (FieldType, Boolean)

Here is the line you need to change:

FieldPageRef field = (FieldPageRef) somePara.appendField(FieldType.FIELD_PAGE_REF, false);

I hope, this helps.

Best regards,