Hi Apache PDFBox Team,

I am currently working with PDFBox 3.0.1 for filling AcroForm fields in my
PDF files, with Chinese characters. In my attempts, I've loaded the
SimSun.ttf Chinese font into the file and set embed subset to false. While
this approach successfully fills the Chinese characters, the resultant PDF
file size is significantly large, which does not meet my requirements.

When I set embed subset to true, the file size is reduced, and the PDF
displays correctly on my Mac. However, on Windows, the embedded Chinese
characters in the same file appear as garbled text. Notably, the same
SimSun.ttf font is installed on both systems.

I am seeking advice on how to meet the following requirements:

1. Correctly embed Chinese characters into AcroForm fields, ensuring they
display accurately across most systems without any encoding issues.

2. Keep the final PDF file size under 500KB.


To illustrate my issue, I have attached the following items to this google
drive:

https://drive.google.com/drive/folders/1vUiKt_Z1z7CwgIaL73Jki_FmAZIWOw1c?usp=drive_link

1. A PDF file generated with embed subset set to true.

2. A PDF file generated with embed subset set to false.

3. The source code I am using for embedding the font and filling the form.

4. SimSun.ttf I am using

Could you please provide guidance or suggest alternative methods to achieve
these objectives? Any sample code would be greatly appreciated.


Thank you for your assistance.

Best regards,

Congwei
    public static void fillData(InputStream inputStream, OutputStream 
outputStream,
                                Map<String, String> replaceMap, byte[] 
fontFile) throws IOException {

        try (PDDocument document = Loader.loadPDF(new 
RandomAccessReadBuffer(inputStream))) {
            PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
            if (acroForm != null) {
                replaceMap.values().removeAll(Collections.singleton(null));
                final PDResources resources = new PDResources();
                PDType0Font formFont =
                        PDType0Font.load(document, new 
ByteArrayInputStream(fontFile), false);
                String fontName = resources.add(formFont).getName();
                acroForm.setDefaultResources(resources);
                for (PDField field : acroForm.getFields()) {
                    String fieldName = field.getFullyQualifiedName();

                        PDTextField textField = (PDTextField) field;
                        // 12 Tf 字号、0 g黑色
                        String defaultAppearanceString = String.format("/%s 12 
Tf 0 g", fontName);
                        textField.setDefaultAppearance(defaultAppearanceString);
                        textField.setValue(replaceMap.getOrDefault(fieldName, 
""));
                }
                acroForm.flatten();
            }
            document.save(outputStream);
        }
    }
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to