Creating new autofield

Hi,

I created a new autofield(author) in a document using the following code. When i open the file in MSWord, the autofield(author) shows empty.After right click and update the field, the author name generates. I need to show the author name when i opened the document.How should do this?

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Section section = new Section(doc);
doc.appendChild(section);
Body body = doc.getFirstSection().getBody();

Paragraph currentPara = new Paragraph(doc);
Run currentRun = new Run(doc, "");
currentPara.appendChild(currentRun);
builder.moveTo(currentRun);
builder.insertField("AUTHOR \* MERGEFORMAT");
body.appendChild(currentPara);
doc.save("src/Output.docx", SaveFormat.DOCX);

Hi
Thanks for your request. Unfortunately, currently, Aspose.Words cannot update AUTHOR field in the document. However, Aspose.Words can update DOCPROPERTY fields. So you can use it instead AUTHOR field. Here is field code:
{ DOCPROPERTY Author \* MERGEFORMAT }
You can set value of Author property using setAuthor.
https://reference.aspose.com/words/java/com.aspose.words/builtindocumentproperties/#getAuthor
And then update such field using UpdateField method.
https://docs.aspose.com/words/java/update-field/
Please try using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
doc.getBuiltInDocumentProperties().setAuthor("Andrey");
builder.insertField("DOCPROPERTY Author \\* MERGEFORMAT");
doc.updateFields();
doc.save("C:\\Temp\\Autofield.docx", SaveFormat.DOCX);

Best regards,

Hi,

Thanks for your reply. But the same code works for autofields like date, time,page number and page count. Its updating automatically when i open the document. Why the author name and document name is not working.

Hi
Thanks for your request. It is by MS Word design. MS Word automatically updated DATE/PAGE/NUMPAGES etc. fields inside the document Header/Footer during opening. But AUTHOR field is not updated upon opening with MS Word.
Best regards,

The issues you have found earlier (filed as WORDSNET-4680) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)