2017-12-27 28 views
2

PDFBox API을 사용하여 필드에 키릴 문자 값을 추가하는 데 도움이 필요합니다. 다음은 지금까지 내가 가지고있는 것입니다 :PDFBox API : 키릴 문자 값 처리 방법

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 

입력이 라틴 알파벳 인 경우 완벽하게 작동합니다. 하지만 키릴 문자 입력도 처리해야합니다. 어떻게해야합니까?

p.s. 에 의해 발생 : java.lang.IllegalArgumentException가 : 이것이 내가 얻을 예외 U는 + 043D ('afii10079')는이 글꼴 돋움, 인코딩을 사용할 수 없습니다 : WinAnsiEncoding

+0

CreateSimpleFormWithEmbeddedFont.java 예제에서는 특정 글꼴을 사용하는 방법을 보여줍니다. 즉, 코드를 부분적으로 사용할 수 있습니다. 모든 PDF에 대해 또는 특정 PDF의 특정 필드에 대해서만 필요합니까? PDF를 공유 할 수 있습니까? –

+0

예. 내 google.drive에서 PDF를 공개하겠습니다. 여기 링크 -> https://drive.google.com/open?id=1eI1iRQnrxMA2kEVJPLH9FhQMx2_2kMHj – Cronck

답변

1

코드는 아래의 acroform 기본에 적절한 글꼴을 추가 리소스 사전을 만들고 기본 apperances의 이름을 바꿉니다. PDFBox는 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(); 
}