I have the following input docx, from which I generate a PDF using the following code:
@Override
public byte[] writeFile(Document document, String fileExtension) throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.fromName(fileExtension.toUpperCase()));
if(saveOptions instanceof PdfSaveOptions pdfSaveOptions){
pdfSaveOptions.setPreserveFormFields(true);
pdfSaveOptions.setUpdateFields(true);
pdfSaveOptions.setRenderChoiceFormFieldBorder(false);
}
document.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);
document.save(outputStream, saveOptions);
return outputStream.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
The resulting PDF is attached. When I open the PDF in Acrobat, it looks like this:
When I open the same file in Chrome, it looks like this:
Is there a way to fix this?
Input template:
encoding test.docx (18.2 KB)
output file:
encoding test.pdf (22.1 KB)