How to convert DOC to PDF without updating fields

Hello,

Is it possible not to update fields during DOC to PDF conversion?

I have a Word document that has a “FileName” field with full path included. When I convert it to PDF Aspose updates the field instead of using the value stored in the document. Please see the attached sample document and the test code.

The sample file has a stored field value “R:\tmp\FileName field test.doc”, while the result PDF contains a text with the current location.

String testFile = "r:\datasets\docs\Filename field\FileName field test.doc";
Document document = new Document(testFile);

Iterable fields = (Iterable)document.getChildNodes(NodeType.FIELD_START, true);
for (FieldStart fs : fields)
{
    System.out.println("Field: " + fs.getField().getResult());
}

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setSaveFormat(SaveFormat.PDF);

String resultFile = testFile + ".pdf";
document.save(resultFile, saveOptions);

Hi Igor,

Thanks for your inquiry. Please use following code example to achieve your requirements. I suggest you please read about ‘How to Replace Fields with Static Text’ from here:
https://docs.aspose.com/words/java/insert-and-remove-field/

Hope this helps you. Please let us know if you have any more queries.

Document doc = **new** Document(*MyDir* + "FileName+field+test.doc");
System.*out*.println(doc.getRange().getFields().get(0).getResult());
FieldsHelper.*convertFieldsToStaticText*(doc, FieldType.*FIELD_FILE_NAME*);
doc.save(*MyDir* + "Out.pdf");

Hi Tahir,

Thanks, it worked fine.

However I think the following article is not fully accurate then:
https://docs.aspose.com/words/java/update-field/

It says that:

When you open/save a document the fields remain intact.
When you print/render to PDF or XPS the fields related to page-numbering in headers/footers are updated.

But, AFAIU, FileName field is not related to page numbering. That’s why I thought it should not be updated when it’s exported to PDF. It still would be great to have an option not to update FileName fields automatically.

Thanks!

Hi Igor,

Thanks for your inquiry. The shared documentation points are correct. If you open and save the Word document using Aspose.Words, the fields are not updated in output Word document. However, saving Word to Pdf document update the page-numbering.

Document.UpdateFields method updates the values of fields in the whole document. This method does not update fields that are related to the page layout algorithms (e.g. PAGE, PAGES, PAGEREF). The page layout-related fields are updated when you render a document or call UpdatePageLayout.

Please note that Aspose.Words mimics the same behavior as MS Word does. If you convert your document to Pdf using MS Word, you will get the same output. MS Word updates FileName field.

Hope this answers your query. Please let us know if you have any more queries.

I was misled by all the data in this thread. I had a date field that was damaged when using a FieldsHelper.
In my version (Aspose Words for Java 16.4.0) there is a straightforward way to prevent updates when saving.

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setSaveFormat(SaveFormat.PDF);
saveOptions.setUpdateFields(false);

String resultFile = testFile + ".pdf";
document.save(resultFile, saveOptions);

In detail the change is:

saveOptions.setUpdateFields(false);

In my version com.aspose.words.FieldsHelper does not exist and i did an exhaustive search on this subject and found this simple method that is stable and documented.
My thread:
Static Date fields rendered incorrectly in PDF Aspose.Words Product Family
I have a current date field in a Word document. To prevent the field updating, I am using the FieldsHelper from the examples. When using FieldsHelper to create static Date fields, the date is cut off. Maybe I am not using the right FieldsHelper because another example in PHP refers to a class com.aspose.words.FieldsHelper, but i cannot find that. I am using Aspose Words for Java 16.4.0. I checked 18.6 but it does not seem to contain the class. Maybe i am missing a dependency? I checked fo…

@benzoid,
Thanks for your inquiry. You are following very old thread. The PdfSaveOptions.UpdateFields property was introduced later. Please do not check older thread. Moreover, the FieldsHelper class is not part of Aspose.Words API. Instead of this class, please use Field.Unlink method. This method replaces the field with its most recent result.
Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hello Tahir,
@tahir.manzoor
I was searching online and I got misled by all these threads.
I am simply trying to help by providing a modern answer. This will prevent any problems for other users in the future.
Also the thread is from 2015, not so old. “Very old” would be like 2005.
Thanks for telling me about the Unlink method. Can you show an example of creating static versions of different fields like Date, File and other fields, what the FieldsHelper was set up to accomplish? You refer to the Field class and it’s Unlink method but I was using Run instances with a DocumentVisitor. Can You show me how to access these Fields?
Also, i referred to my example in:
Static Date fields rendered incorrectly in PDF Aspose.Words Product Family
I have a current date field in a Word document. To prevent the field updating, I am using the FieldsHelper from the examples. When using FieldsHelper to create static Date fields, the date is cut off. Maybe I am not using the right FieldsHelper because another example in PHP refers to a class com.aspose.words.FieldsHelper, but i cannot find that. I am using Aspose Words for Java 16.4.0. I checked 18.6 but it does not seem to contain the class. Maybe i am missing a dependency? I checked fo…
And that shows that the FieldsHelper is a real thing and present in current examples.
Also in present documentation, the FieldsHelper class is used.

$helper = new Java("com.aspose.words.FieldsHelper", $targetFieldType);

https://docs.aspose.com/words/java/replace-fields-with-static-text/
But this class does not exist.

@benzoid,
Thanks for your inquiry. You can replace field with it’s result using Field.Unlink and FieldsHelper.convertFieldsToStaticText. Please read following article for more detail:
How to Replace or Modify Hyperlinks and Replace Fields with Static Text
We will update this article according to latest API of Aspose.Words. Following code example shows how to unlink the date field.

Document doc = new Document(MyDir + "in.docx");
for(Field field : doc.getRange().getFields())
{
    if(field.getType() == FieldType.FIELD_DATE)
        field.unlink();
}
doc.save(MyDir + "out.docx");

If you are facing any issue while using Aspose.Words, please share complete detail of your use case along with input and expected output documents. We will then provide you more information about your query along with code.

@tahir.manzoor
Thank you for your examples.
The FieldsHelper in your example is what damaged my document. Do not use it.
I understand that the DocumentVisitor is useful and can be used to modify documents.
My examples are present here, i have provided screenshots:
Static Date fields rendered incorrectly in PDF Aspose.Words Product Family
I have a current date field in a Word document. To prevent the field updating, I am using the FieldsHelper from the examples. When using FieldsHelper to create static Date fields, the date is cut off. Maybe I am not using the right FieldsHelper because another example in PHP refers to a class com.aspose.words.FieldsHelper, but i cannot find that. I am using Aspose Words for Java 16.4.0. I checked 18.6 but it does not seem to contain the class. Maybe i am missing a dependency? I checked fo…

@benzoid,
Thanks for your inquiry. Please ZIP and attach your input Word document here for testing. We will investigate the issue on our side and provide you more information.

3 posts were split to a new topic: Replace field result with static text