UpdatePageLayout() or UpdateFields() for Java

I have been trying to determine how to get the current page number that a Node is located on. In order to do this i’m searching for node im interested in, inserting a node near it, (trying to) update the doc, and then referenced the inserted nodes later to find their page number.

Reviewing a post made by the Aspose team for a similar question against C# the following snippet of code was posted:

if (isNewFieldEngine)
    currentDoc.UpdateFields();
else
    currentDoc.UpdatePageLayout();

Reading the API it looks like the Java doc has both of these methods removed BUT when i inspect the Aspose jar with Eclipse it shows the java version as having UpdatePageLayout().

I actually tried to use it and got an NPE after doing something similar to:

customDocument = new Document(file.getAbsolutePath());
customDocument.updatePageLayout();

Do java folks have a similar or equivalent way to run this method and update the document?

Hi there,
Thanks for your inquiry.
The .NET version needs this conditional as there were updates to the field engine starting in version 9.6. Before version 9.6 PAGE fields were updated using UpdatePageLayout(), but with the new field engine in 9.6 and later versions PAGE fields were instead updated using UpdateFields().
Right now in Java you only need to call UpdatePageLayout() for any version to update the PAGE fields as Java is not yet using the new field engine. However as soon as the first autoported version for Java is released (10.0), you will then need to change to using UpdateFields() to update PAGE fields as well.
Thanks,

Adam, thanks a lot for your response, it sounds like i was heading in the right direction but i am a little bit confused for a couple reasons:

  1. You mentioned here:
    https://forum.aspose.com/t/63996
    that the updatePageLayout() method is only available for.NET

  2. I do see it actually available in the jar but when i call upon it with the code i posted in my first post i get an NPE to the tune of this stacktrace:

2011-03-17 10:49:41 ERROR Could not build out custom document
java.lang.NullPointerException
at asposewobfuscated.jz.eV(Directory.java: 87)
at asposewobfuscated.qx.a(TTFontFiler.java: 788)
at asposewobfuscated.qx.iF(TTFontFiler.java: 763)
at asposewobfuscated.qx.Vp(TTFontFiler.java: 752)
at asposewobfuscated.qx.iz(TTFontFiler.java: 321)
at asposewobfuscated.qx.C(TTFontFiler.java: 117)
at asposewobfuscated.qx.B(TTFontFiler.java: 42)
at asposewobfuscated.oa.hc(PalFont.java: 24)
at com.aspose.words.xk.Ho(SpanPr.java: 231)
at com.aspose.words.sl.rs(Span.java: 730)
at com.aspose.words.sl.getWidth(Span.java: 593)
at com.aspose.words.zy.JJ(LineBuilder.java: 88)
at com.aspose.words.zy.JI(LineBuilder.java: 30)
at com.aspose.words.bx.V(LinePart.java: 281)
at com.aspose.words.k.c(ColumnBuilder.java: 481)
at com.aspose.words.k.a(ColumnBuilder.java: 163)
at com.aspose.words.k.a(ColumnBuilder.java: 97)
at com.aspose.words.mz.a(PageBuilder.java: 119)
at com.aspose.words.mz.a(PageBuilder.java: 60)
at com.aspose.words.ajt.b(SectionReflower.java: 40)
at com.aspose.words.io.nG(MainTextSectionLayout.java: 757)
at com.aspose.words.qr.nG(StoryLayoutBase.java: 88)
at com.aspose.words.mx.nG(PageLayoutModel.java: 124)
at com.aspose.words.zh.a(LayoutDocument.java: 33)
at com.aspose.words.Document.updatePageLayout(Document.java: 1637)
at com.customdoctools.CustomDocument.(CustomDocument.java: 59)

Your input would be greatly appreciated and thanks for the help working through this.

Hi there,
Thanks for this additional information.
You’re correct the Java version does actually include the UpdatePageLayout call. In the old thread you posted I was looking at the Java documentation which for some reason does not include this memeber. I will make sure this is fixed when the Java documentation is updated after autoporting.
Just to clarify that in Java you will need to call UpdatePageLayout to update PAGE fields for now as the old field evaluation engine is being used. After autoporting you will need to use UpdateFields to achieve the same functionality instead.
Regarding the exception you’re getting, could you please attach your test document and the code you are using here for testing.
The stacktrace most likely points to fonts that are required but cannot be found. Please test rendering this document without any extra code (e.g use simple open and save code) and see if this issue still occurs.
You can set the fonts folder used for rendering by using the code below

PdfOptions pdfOpts = new PdfOptions();
pdfOpts.setTrueTypeFontsFolder("usr/fonts"); 
doc = new Document("Document.doc");
doc.saveToPdf(0, doc.getPageCount(), " Document Out.pdf", pdfOpts);

Thanks,

Please find the source code attached.

I have another class i use to call this against the attached test files
(one created with office 2010 for mac, and the other 2003 with windows
xp)

thanks again!

Adam,

i tried your test code and after some tweaking was able to get it to work.

One problem i ran into (that might be relevant) is that i am using a Mac. Specifying the normal font directory that OSX provides – /Library/Fonts or /Library/Fonts/Microsoft – was resulting in errors like "java.lang.IllegalStateException: End of file reached. "

I ended up isolating the one font used in the doc to it’s own directory and that seemed to work. Here is the code i used:

PdfOptions pdfOpts = new PdfOptions();
try
{
    pdfOpts.setTrueTypeFontsFolder("/Users/mzigliotto/Desktop/fonts");
}
catch (Exception e)
{
}

Document doc = new Document("/Users/mzigliotto/Desktop/test.doc");
doc.saveToPdf(0, doc.getPageCount(), "Document Out.pdf", pdfOpts);

Hello

It is perfect, that you already found the way to resolve the problem. Please let us know in case of any issues. We will be glad to help you. And thank you for additional information.
Best regards,

We are happy to inform you that the first auto-ported version of Aspose.Words for Java is ready. This version supports updating fields. You can get it from here.

Best regards,
Aspose.Words team

(29)