2017-10-31 10 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하고 문제가 PDF 파일로 렌더링지고 있지 않은 텍스트의 후반 (텍스트 1, 텍스트 2)입니다. pdf에는 이미지와 첫 번째 줄 ABC Management System 만 표시됩니다.

여러 줄 텍스트를 생성하려면 다음을 참조하십시오. PDFBox - Adding Multiple Lines.

setLeading이 무엇인지 이해하지 못 했으므로 주석을 달아서 다시 시도했지만 텍스트는 여전히 렌더링되지 않았습니다.

+0

@TilmanHausherr 일했습니다. 감사! – HBK

+0

@TilmanHausherr 배경색을 변경할 방법이 있습니까? – HBK

+0

직사각형을 비연 마성 색으로 그려야합니다. –

답변

1

newLineAtOffset()은 현재 텍스트 위치를 기준으로합니다. 0에서 다시 시작하려면 가장 쉬운 방법은 현재 텍스트를 끝내고 새 텍스트 블록을 시작하는 것입니다. 현재 코드는 y 1335에 있습니다 (또는 선행에 따라 약간 낮아짐).

+0

줄 바꾸기를 수행 할 수있는 바로 가기가 있습니까? 바로 지금, 우리는 그것을 달성하기 위해 한 줄을 두 줄로 나누어야합니다. – HBK

+0

아니요. PDFBox는 itext와 달리 낮은 레벨입니다. 줄 바꿈/페이지 줄 바꿈을 직접 수행해야합니다. –