无法在PDF中保存阿拉伯语单词-PDFBox Java

Dan*_*elo 6 java pdfbox

尝试将阿拉伯语单词保存在可编辑的PDF中。它对英语语言来说都可以正常工作,但是当我使用阿拉伯语单词时,出现了这个异常:

java.lang.IllegalArgumentException:U + 0627在此字体Helvetica编码中不可用:WinAnsiEncoding

这是我生成PDF的方式:

public static void main(String[] args) throws IOException
{
  String formTemplate = "myFormPdf.pdf";
  try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate)))
  {
    PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
    if (acroForm != null)
    {
        PDTextField field = (PDTextField) acroForm.getField( "sampleField" );
        field.setValue("????");
    }
    pdfDocument.save("updatedPdf.pdf"); 
  }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*elo 3

这就是我的工作方式,希望对其他人有帮助。只需使用您想要在 PDF 中使用的语言支持的字体即可。

\n\n
public static void main(String[] args) throws IOException\n{\n  String formTemplate = "myFormPdf.pdf";\n\n  try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate)))\n  {\n    PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();\n    // you can read ttf from resources as well, this is just for testing \n    PDFont font = PDType0Font.load(pdfDocument,new File("/path/to/font.ttf"));\n    String fontName = acroForm.getDefaultResources().add(pdfont).getName();\n    if (acroForm != null)\n    {\n        PDTextField field = (PDTextField) acroForm.getField( "sampleField" );\n        field.setDefaultAppearance("/"+fontName +" 0 Tf 0 g");\n        field.setValue("\xd8\xac\xd9\x85\xd9\x84\xd8\xa9");\n    }\n\n    pdfDocument.save("updatedPdf.pdf"); \n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑:添加mkl的注释\n字体名称和字体大小是Tf指令的参数,黑色的灰度值0是g指令的参数。参数和指令名称必须适当分开。

\n