Reputation: 1
So basically I already achieved creating a text when generating a pdf on a specific position.
But now my problem is, how do I do it for text that are located on different positions
Expected Generated PDF:
contentStream.setLeading(15);
contentStream.newLineAtOffset(175, 670);
String text = "Text 1";
String text1 = "Text 2";
String text2 = "Text 3";
String text3 = "Text 4";
String text4 = "Text 5";
contentStream.showText(text);
contentStream.newLine();
contentStream.showText(text1);
contentStream.newLine();
contentStream.showText(text2);
contentStream.newLine();
contentStream.showText(text3);
contentStream.newLine();
contentStream.showText(text4);
contentStream.newLine();
contentStream.showText(text5);
Upvotes: 0
Views: 5688
Reputation: 95888
In your code you already choose the position for the first line start like this:
contentStream.newLineAtOffset(175, 670);
Concerning your question
how do I do it for text that are located on different positions
therefore: You simply use newLineAtOffset
again!
You have to be aware, though, that newLineAtOffset(x, y)
does not set the new line start to the absolute coordinates x, y
but instead adds these values to the coordinates of the previous line start, hence the AtOffset
in the method name.
As the previous line start coordinates are reset to 0, 0
at the start of a text object (contentStream.beginText()
), your first newLineAtOffset
in a text object appears to use absolute coordinates.
So if you prefer to use absolute coordinates, you can start a new text object each time you need to move the line start differently than contentStream.newLine()
does.
If you are ok with relative coordinates, though, you don't need to start new text objects that often but instead use offsets from line start to line start in newLineAtOffset
, e.g.:
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
PDFont font = PDType1Font.HELVETICA;
String text = "Text 1";
String text1 = "Text 2";
String text2 = "Text 3";
String text3 = "Text 4";
String text4 = "Text 5";
String text5 = "Text 6";
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.beginText();
contentStream.newLineAtOffset(175, 670);
contentStream.setFont(font, 12);
contentStream.setLeading(15);
contentStream.showText(text);
contentStream.newLine();
contentStream.showText(text1);
contentStream.newLineAtOffset(225, 10);
contentStream.setFont(font, 15);
contentStream.showText(text2);
contentStream.newLineAtOffset(-390, -175);
contentStream.setFont(font, 13.5f);
contentStream.setLeading(17);
contentStream.showText(text3);
contentStream.newLine();
contentStream.showText(text5);
contentStream.newLineAtOffset(300, 13.5f);
contentStream.showText(text4);
contentStream.endText();
contentStream.moveTo(0, 520);
contentStream.lineTo(612, 520);
contentStream.stroke();
}
document.save(TARGET_FILE);
}
(ArrangeText test testArrangeTextForTeamotea
)
which results in
which in turn approximates your image. (I have not counted pixels in your image, so this only is an approximation.)
Upvotes: 4