2017-05-23 5 views
0

아랍어, 히브리어 및 이디시어 문자를 PDF로 작성하려고하지만 역순으로 작성됩니다. PDFBox 2.0.5를 사용하여 PDF 문서를 작성하고 작성합니다.히브리어, 아랍어, 이디시어 텍스트는 PDFBox 2.0.5의 역순으로 작성됩니다.

내 샘플 코드

String relativeWebPath = "/font/arial.ttf"; 
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); 
File file = new File(absoluteDiskPath); 

ByteArrayOutputStream output=new ByteArrayOutputStream(); 
PDDocument document=new PDDocument(); 
PDFont font = PDType0Font.load(document, new File(absoluteDiskPath)); 
PDPage test=new PDPage(); 
document.addPage(test); 
PDPageContentStream content=new PDPageContentStream(document, test); 
final String EXAMPLE = "النص العربي"; 
System.out.print(EXAMPLE); 

content.beginText(); 
content.newLineAtOffset(50, 680); 
content.setFont(font, 12); 
content.showText(EXAMPLE); 
System.out.print(EXAMPLE); 
content.endText(); 

content.close(); 

document.save(output); 
document.close(); 

나는이 프로젝트에 ICU4J 라이브러리를 추가하여 처리하지만 나를 위해없는 작품도 의존성을 ICU4J가 PDFBox 2.0 (PDFBox-2118)에서 제거되는 것을 발견 해결책을 연구하는 동안.

+0

RTL 언어 및 복잡한 스크립트는 슬프게도 PDF 작성에서 지원되지 않습니다. –

답변

2

PDFBox 자체에서 처리하지 않기 때문에 Google에서 처리해야합니다. 좋아, RTL 언어 문자의 역 문자열을 생성하고 PDFBox에 전달하면 PDFBox가 올바른 방향으로 작성됩니다.

이제 다음 질문은 RTL 언어의 텍스트를 감지하는 방법과이를 해결하는 방법입니다. 해결책은 여기에 있습니다. 이는 Java의 BiDi 클래스 객체를 사용하여 얻을 수 있습니다. 이건 내 문제를 해결

String word = EXAMPLE; // string from question 
    Bidi bidi = new Bidi(word, -2); 
    if (!bidi.isMixed() && bidi.getBaseLevel() == 0) { 
     return word; 
    } 
    else { 
     int runCount = bidi.getRunCount(); 
     byte[] levels = new byte[runCount]; 
     Integer[] runs = new Integer[runCount]; 

     for (int result = 0; result < runCount; ++result) { 
      levels[result] = (byte) bidi.getRunLevel(result); 
      runs[result] = Integer.valueOf(result); 
     } 

     Bidi.reorderVisually(levels, 0, runs, 0, runCount); 
     StringBuilder bidiText = new StringBuilder(); 

     for (int i = 0; i < runCount; ++i) { 
      int index = runs[i].intValue(); 
      int start = bidi.getRunStart(index); 
      int end = bidi.getRunLimit(index); 
      byte level = levels[index]; 
      if ((level & 1) != 0) { 
       while (true) { 
        --end; 
        if (end < start) { 
         break; 
        } 

        char character = word.charAt(end); 
        if (Character.isMirrored(word.codePointAt(end))) { 
         bidiText.append(character); 
        } 
        else { 
         bidiText.append(character); 
        } 
       } 
      } 
      else { 
       bidiText.append(word, start, end); 
      } 
     } 

     return bidiText.toString(); 
    } 

:

완벽한 솔루션이 솔루션은 문자열의 조합을 처리합니다. 희망은 다른 사람들을 도울 것입니다.