2017-10-31 10 views
1

Android 용 PDFBox 라이브러리를 사용하여 페이지에 텍스트를 그리는 알고리즘을 구현했습니다. 문제는 아래 그림과 같이 텍스트가 겹쳐진 새 페이지를 추가 할 때마다 발생합니다. PDPageContentStream.newLine() 메서드를 사용하고 있지만 결과가 예상과 다를 것이라고 확신합니다. 다른 것이 누락 되었습니까? enter image description herePDFBox 겹쳐진 텍스트

여기에 내 코드

PDPage page1 = new PDPage(); 
getInstance().getAnnexe().addPage(page1); 
PDPageContentStream contentStream1 = new 
PDPageContentStream(getInstance().getAnnexe(), page1, true, true); 
contentStream1.beginText(); 
contentStream1.newLineAtOffset(100F, 650F); 
contentStream1.setFont(font, fontSize); 
printMultipleLines(subSet, contentStream1); 
contentStream1.endText(); 
contentStream1.close(); 

입니다 그리고 이것은 printMultipleLines() 방법에게 TL 연산자와 함께 있던 문제를 @TilmanHausherr하는

private void printMultipleLines(ArrayList<String> lines, PDPageContentStream contentStream) { 
    try { 
     for (String line : 
       lines) { 
      if (line.length() > 110) { 
       // Print line as 2 lines 
       contentStream.showText(line.substring(0, 90)); 
       contentStream.newLine(); 
       contentStream.showText(line.substring(90, line.length())); 
      } else 
       // Print line as a whole 
       contentStream.showText(line); 
      // Print Carriage Return 
      contentStream.newLine(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+2

newLine을 사용하는 경우 텍스트 선행 (TL 연산자)을 설정해야합니다. 안드로이드에 대한 PDFBox에서 사용할 수 있는지 모르겠습니다. 대안 : 'newLineAtOffset'을 상대 y 값과 함께 호출하십시오. (0, -20)이다. –

+0

@TilmanHausherr이 줄'contentStream1.newLineAtOffset (100F, 650F);를 사용하고 있습니다. –

+1

예 (하지만 후속 줄은 try (0, -20)). 매개 변수는 텍스트 블록 내의 ** 상대적 **입니다. –

답변

1

감사입니다. 새로 생성 된 각 페이지는 TL이 사용자 기본 단위의 0과 같습니다. Text Leading 오프셋을 설정해야했습니다.

PDPage page1 = new PDPage(); 
getInstance().getAnnexe().addPage(page1); 
PDPageContentStream contentStream1 = new 
PDPageContentStream(getInstance().getAnnexe(), page1, true, true); 
// Set the Text Leading (TL operator) here!!!! 
contentStream1.setLeading(12); 
contentStream1.beginText(); 
contentStream1.newLineAtOffset(100F, 650F); 
contentStream1.setFont(font, fontSize); 
printMultipleLines(subSet, contentStream1); 
contentStream1.endText(); 
contentStream1.close(); 

모든 감사와 크레딧 자신의 신속하고 정확한 답변을 @ TilmanHausherr로 이동 : 다음은 업데이트 된 코드입니다.