Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

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

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);
PDDocumentCatalog dc = document.getDocumentCatalog();
PDAcroForm acroForm = dc.getAcroForm();
PDField naziv = acroForm.getField("naziv");
naziv.setValue("Наслов"); // this part right here
naziv.setValue("Naslov"); // it works like this

It works perfect when my input is in Latin Alphabet. But I need to handle Cyrillic inputs as well. How can I do it?

p.s. this is the exception I get: Caused by: java.lang.IllegalArgumentException: U+043D ('afii10079') is not available in this font Helvetica encoding: WinAnsiEncoding

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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<PDField> 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...