PDF input field encoding problem

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)

@kerner It looks like MS Edge can use only glyphs of the fonts embedded into the PDF document. So it displays only characters, which glyphs are there in the font subset embedded into the document. The problem does not occur if embed full fonts:

Document doc = new Document("C:\\Temp\\in.docx");
    
PdfSaveOptions opt = new PdfSaveOptions();
opt.setPreserveFormFields(true);
opt.setEmbedFullFonts(true); // Embed full fonts
    
doc.save("C:\\Temp\\out.pdf", opt);

Looks like a glitch or limitation in MS Edge. The problem does not occur in Acrobat Reader.

1 Like

Hi Alexey,
thanks so much for your promt response! Your workaround works!

… One more question: The resulting file is of course much larger if we embed fonts. I dont know too much about glyphs, but could we use another font to avoid embedding it into the document?

@kerner Yes, it is expected that document becomes much larger when full fonts are embedded. Aspose.Words embeds the fonts used in your original document. So what fonts will be embedded depends on your input document.