PDFBox API: How to change font to handle Cyrillic values in an AcroForm field

后端 未结 1 569
闹比i
闹比i 2020-12-11 23:35

I need help with adding Cyrillic value to a field using the PDFBox API. Here is what I have so far:

PDDocument document = PDDocument.load(file);         


        
1条回答
  •  有刺的猬
    2020-12-12 00:09

    The code below adds an appropriate font in the acroform default resource dictionary, and replaces the name in the default appearances. PDFBox recreates the appearance stream of the fields using the new font when you call setValue().

    public static void main(String[] args) throws IOException
    {
        PDDocument doc = PDDocument.load(new File("ZPe.pdf"));
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
        PDResources dr = acroForm.getDefaultResources();
    
        // Important: the font is Type0 (allows more than 256 glyphs) and NOT SUBSETTED
        PDFont font = PDType0Font.load(doc, new FileInputStream("c:/windows/fonts/arial.ttf"), false);
    
        COSName fontName = dr.add(font);
        Iterator it = acroForm.getFieldIterator();
        while (it.hasNext())
        {
            PDField field = it.next();
            if (field instanceof PDTextField)
            {
                PDTextField textField = (PDTextField) field;
                String da = textField.getDefaultAppearance();
    
                // replace font name in default appearance string
                Pattern pattern = Pattern.compile("\\/(\\w+)\\s.*");
                Matcher matcher = pattern.matcher(da);
                if (!matcher.find() || matcher.groupCount() < 2)
                {
                    // oh-oh
                }
                String oldFontName = matcher.group(1);
                da = da.replaceFirst(oldFontName, fontName.getName());
    
                textField.setDefaultAppearance(da);
            }
        }
        acroForm.getField("name1").setValue("Наслов");
        doc.save("result.pdf");
        doc.close();
    }
    

    Update 4.4.2019: to save some space, it may be useful to remove the appearance before calling setValue:

    acroForm.getField("name1").getWidgets().get(0).setAppearance(null);
    

    to check whether there are unused fonts in the AcroForm default resources, see this answer.

    Update 7.4.2019: you may experience poor performance if the font is very large (e.g. ArialUni) and many fields are to be set (PDFBOX-4508). In that case, save and reload the file before calling setValue.

    To find out whether a font supports an intended text, call PDFont.encode() and check for IllegalArgumentException.

    0 讨论(0)
提交回复
热议问题