簡體   English   中英

使用 PDFBox 將西里爾字符寫入 PDF 表單字段

[英]Write cyrillic chars into PDF form fields with PDFBox

我正在使用 pdfbox 2.0.5 使用以下代碼填寫 PDF 文檔的表單字段:

        doc = PDDocument.load(inputStream);
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDAcroForm form = catalog.getAcroForm();
        for (PDField field : form.getFieldTree()){
            field.setValue("должен");
        }

我收到此錯誤: U+0434 ('afii10069') 在此字體中不可用 Times-Roman (generic: TimesNewRomanPSMT) encoding: StandardEncoding with Difference

PDF 文檔本身包含顯示正常的西里爾文文本。 我試過使用不同的字體。 對於“Arial Unicode MS”,它需要下載一個 50MB 的“Adobe Acrobat Reader DC 字體包”。 這是對西里爾字符的要求嗎?

我必須在文本字段中指定哪種字體來處理西里爾文(或亞洲)字符?

謝謝,羅波

Adobe 通過重用 {/Ubuntu} 字體中的嵌入字體文件來處理這個問題,並從中創建一個新的字體資源。 這是一個快速技巧,可以作為如何實現類似目標的指南。 該代碼特定於我擁有的示例。

PDDocument doc = PDDocument.load(new File(...));
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
PDResources formResources = acroForm.getDefaultResources();
PDTrueTypeFont font = (PDTrueTypeFont) formResources.getFont(COSName.getPDFName("Ubuntu"));

// here is the 'magic' to reuse the font as a new font resource
TrueTypeFont ttFont = font.getTrueTypeFont();

PDFont font2 = PDType0Font.load(doc, ttFont, true);
ttFont.close();

formResources.put(COSName.getPDFName("F0"), font2);

PDTextField formField = (PDTextField) acroForm.getField("Text2");
formField.setDefaultAppearance("/F0 0 Tf 0 g");
formField.setValue("öäüинформацию");

doc.save(...);
doc.close();

解決方案很簡單: form.setNeedAppearances(true);

然后我刪除了字段的藍色框: field.setReadOnly(true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM