Print field codes of a word document

I want to convert a Word document to a PDF. But instead of the field values the field codes shall be printed. Using Microsoft Word application this can be done with ALT-F9 before converting to PDF. How can I achieve this using Aspose.Word?

@Tim49

To print field codes instead of field values when converting a Word document to a PDF using Aspose.Words, you can utilize the Field class to access the field codes directly. Below is a sample code snippet that demonstrates how to achieve this in Java:

import com.aspose.words.Document;
import com.aspose.words.Field;
import com.aspose.words.PdfSaveOptions;

public class ConvertFieldCodesToPdf {
    public static void main(String[] args) throws Exception {
        // Load the document
        Document doc = new Document("path/to/your/document.docx");

        // Iterate through all fields in the document
        for (Field field : doc.getRange().getFields()) {
            // Replace the field result with the field code
            field.setResult(field.getGetFieldCode());
        }

        // Save the document as PDF
        PdfSaveOptions saveOptions = new PdfSaveOptions();
        doc.save("path/to/output/document.pdf", saveOptions);
    }
}

Explanation:

  1. Load the Document: The Document class is used to load your Word document.
  2. Iterate Through Fields: The code iterates through all fields in the document using getFields().
  3. Set Field Result: For each field, it sets the result to the field code using setResult(field.getGetFieldCode()).
  4. Save as PDF: Finally, the document is saved as a PDF using save() method with PdfSaveOptions.

This approach effectively replaces the field values with their corresponding field codes before saving the document as a PDF.

Make sure to adjust the file paths in the code to point to your actual document locations.

If you have any further questions or need additional assistance, feel free to ask!

Thanks for the quick answer.
So there it is necessary to iterate over all the fields and there is nothing like β€œAlt-F9”.

I tried your suggested solution and it works fine in most cases e.g.
FieldCode: PAGE , FieldResult: 1
FieldCode: PAGE , FieldResult: PAGE after setting FieldResult to FieldCode:

*FieldCode: FORMTEXT , FieldResult: *
FieldCode: FORMTEXT , FieldResult: FORMTEXT  after setting FieldResult to FieldCode:

*FieldCode: FORMTEXT , FieldResult: *
FieldCode: FORMTEXT , FieldResult: FORMTEXT  after setting FieldResult to FieldCode:

But there are cases I get an exception:
*FieldCode: FORMCHECKBOX , FieldResult: *
java.lang.IllegalStateException: Cannot set result of a field that does not have a separator.

*FieldCode: PRIVATE , FieldResult: *
java.lang.IllegalStateException: Cannot set result of a field that does not have a separator.

Do you have any hint how to handle these cases?

@Tim49 Please try using the following code:

Document doc = new Document("C:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

for (Field f : doc.getRange().getFields())
{
    String fieldCode = "{ " + f.getFieldCode() + " }";
    builder.moveToField(f, true);
    builder.write(fieldCode);
    f.remove();
}

doc.save("C:\\temp\\out.pdf");

Thanks alexey. This does the work :slight_smile: .

1 Like