2017-10-31 14 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がユーザーのデフォルト単位のゼロ量に相当します。テキストの先頭オフセットを設定するだけでした。

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に行く: はここに更新されたコードです。