2017-10-31 11 views
0

Apache PDFBox Javaライブラリを使用してPDFを作成しています。しかし、私は複数行のテキスト(行の折り返し)をレンダリングする際に問題に直面しています:Apache PDFBox Javaライブラリ - テキストがレンダリングされない

//Creating PDF document object 
PDDocument doc = new PDDocument(); 

//Adding the blank page to the document 
doc.addPage(new PDPage()); 

PDPage page = doc.getPage(0); 
PDImageXObject pdImage = PDImageXObject.createFromFile("C:\\Users\\abc\\Desktop\\abc.png", doc); 

PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
contentStream.drawImage(pdImage, 10, 720); 

//Begin the Content stream 
contentStream.beginText(); 
contentStream.newLineAtOffset(50, 735); 

//Setting the font to the Content stream 
contentStream.setFont(PDType1Font.TIMES_ROMAN, 16); 
contentStream. showText("ABC Management System"); 

//Setting the leading 
//contentStream.setLeading(14.5f); 

//Setting the position for the line 
contentStream.newLineAtOffset(25, 600); 

String text1 = "This is an example of adding text to a page in the pdf document we can add as many lines"; 
String text2 = "as we want like this using the ShowText() method of the ContentStream class"; 

//Adding text in the form of string 
contentStream. showText(text1); 
contentStream.newLine(); 
contentStream. showText(text2); 

//Ending the content stream 
contentStream.endText(); 

System.out.println("Content added"); 

//Closing the content stream 
contentStream.close(); 

//Saving the document 
doc.save("my_doc.pdf"); 

System.out.println("PDF created"); 

//Closing the document 
doc.close(); 

私はfaciing午前問題は、テキスト(テキスト1、テキスト2)の後半は、PDFファイルにレンダリング取得されていないです。画像と最初の行ABC Management Systemのみがpdfに表示されます。

複数行のテキストを生成するために、私はPDFBox - Adding Multiple Linesを参照しました。

私はsetLeadingのことを理解していないので、コメントアウトして再試行しましたが、まだテキストがレンダリングされていませんでした。

+0

@TilmanHausherrそれは働いた。ありがとう! – HBK

+0

@TilmanHausherr背景色を変更する方法はありますか? – HBK

+0

適切な非ストロークカラーで四角形を描く必要があります。 –

答えて

1

newLineAtOffset()は、現在のテキスト位置に相対的です。 0から再起動するには、現在のテキストを終了して新しいテキストブロックを開始するのが最も簡単です。あなたの現在のコードはあなたをy 1335に置きます(または先行きに応じて少し下がります)。

+0

ラインラップを行うためのショートカット方法はありますか?今、私たちはそれを達成するために線を2つの文字列に分割する必要があります。 – HBK

+0

いいえ、PDFBoxは、itextとは異なり、低レベルです。あなたはラインラップ/ページラップを自分でやらなくてはなりません。 –

関連する問題