You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gabsaga Tata <ga...@simpaq.com> on 2011/06/30 16:52:49 UTC

Line Break in Page

I am trying to create a new PDF document and will like to add line breaks at certain points in the document. I've looked at the documentation and examples and not sure on how to proceed. Below is the code. I will like each text string the array to be on a new line in the same page. I'll greatly appreciate your help.

Thanks.

Gabsaga



String[] messageTokens = new String[3]; 
messageTokens[0] = "This is message 1 on line 1";
messageTokens[1] = "This is message 2 on line 2";
messageTokens[2] = "This is message 3 on line 3";

PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(font, 12);

contentStream.moveTextPositionByAmount(100, 700);
for (String message : messageTokens) {
 contentStream.drawString(message);
}

contentStream.endText();

contentStream.close();
document.save( "Test_Doc.pdf");
document.close();



Re: Line Break in Page

Posted by Andreas Lehmkuehler <an...@lehmi.de>.
Hi,

Am 30.06.2011 16:52, schrieb Gabsaga Tata:
> I am trying to create a new PDF document and will like to add line breaks at certain
> points in the document. I've looked at the documentation and examples and not sure
> on how to proceed. Below is the code. I will like each text string the array to be
 > on a new line in the same page. I'll greatly appreciate your help.
>
> Thanks.
>
> Gabsaga
>
>
>
> String[] messageTokens = new String[3];
> messageTokens[0] = "This is message 1 on line 1";
> messageTokens[1] = "This is message 2 on line 2";
> messageTokens[2] = "This is message 3 on line 3";
>
> PDDocument document = new PDDocument();
> PDPage page = new PDPage();
> document.addPage(page);
> PDFont font = PDType1Font.HELVETICA_BOLD;
> PDPageContentStream contentStream = new PDPageContentStream(document, page);
> contentStream.beginText();
> contentStream.setFont(font, 12);
>
> contentStream.moveTextPositionByAmount(100, 700);
> for (String message : messageTokens) {
>   contentStream.drawString(message);
> }
>
> contentStream.endText();
>
> contentStream.close();
> document.save( "Test_Doc.pdf");
> document.close();
The pdf format doesn't know line breaks. You have to move the text position to
the next line on your own using moveTextPositionByAmount.

BR
Andreas Lehmkühler