Unable to save Arabic words in a PDF - PDFBox Java

那年仲夏 提交于 2019-12-01 17:00:47

问题


Trying to save Arabic words in an editable PDF. It works all fine with English ones but when I use Arabic words, I am getting this exception:

java.lang.IllegalArgumentException: U+0627 is not available in this font Helvetica encoding: WinAnsiEncoding

Here is how I generated 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"); 
  }
}

回答1:


That's how I made it work, I hope it would help others. Just use the font that is supported by the language that you want to use in the 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();
    // you can read ttf from resources as well, this is just for testing 
    PDFont font = PDType0Font.load(pdfDocument,new File("/path/to/font.ttf"));
    String fontName = acroForm.getDefaultResources().add(pdfont).getName();
    if (acroForm != null)
    {
        PDTextField field = (PDTextField) acroForm.getField( "sampleField" );
        field.setDefaultAppearance("/"+fontName +" 0 Tf 0 g");
        field.setValue("جملة");
    }

    pdfDocument.save("updatedPdf.pdf"); 
  }
}

Edited: Adding the comment of mkl The font name and the font size are parameters of the Tf instruction, and the gray value 0 for black is the parameter for the g instruction. Parameters and instruction names must be appropriately separated.




回答2:


You need a font which supports those Arabic symbols.
Once you've got a compatible font, you can load it using PDType0Font

final PDFont font = PDType0Font.load(...);

A Type 0 font is a font which references multiple other fonts' formats, and can, potentially, load all available symbols.

See also the Cookbook - working with fonts (no examples with Type 0, but still useful).



来源:https://stackoverflow.com/questions/55451551/unable-to-save-arabic-words-in-a-pdf-pdfbox-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!