Well I've been working with PDFBox I still don't understand it at all, but I've read the Documentation, working with fonts, and some other places, but I've found how to get the text from the PDF and it's style, but I'm creating it, not reading it.
I am trying to make something
Like: this (Having bold and normal text on the same line).
I've been using streams:
Not sure if this is all the code needed to help me, 'cause I just joined this project but it had started when I joined.
I would thank you all if you could help me implementing this code or maybe helping me with an idea or source where I could read the solution.
I leave my code here:
public void rCovernoteFooter(PDPageContentStream stream , float x, float y) throws IOException {
y = y-20;
String currentDate = df1.format(new Date());
String[] issues = constants.issued().split("/");
String issued = issues[0] + currentDate + issues[1];
y = rText(stream, x, y, 90, 0, 10, issued, null);
stream.setFont(PDType1Font.TIMES_ROMAN, 8);
y = rText(stream, x, y - 50, 117, 0, 10, constants.terms(), null);
}
public float rText(PDPageContentStream cStream, float x, float y, int col1, int col2, int spc, String data1, String data2) throws IOException {
float y1 = 0f;
float y2 = 0f;
if (data2 == null) {
y = iterate(data1, col1, x, y, spc, cStream);
} else {
y1 = iterate(data1, col1, x, y, spc, cStream);
y2 = iterate(data2, col2, x + 125, y, spc, cStream);
if (y1 >= y2) {
return y2;
} else {
return y1;
}
}
return y;
}
private float iterate(String text, int len, float x, float y, int space, PDPageContentStream stream) throws IOException {
int rowHeight = 0;
List<String> wordList = Lists.newArrayList();
wordList = wordLines(text, len);
stream.setFont(PDType1Font.TIMES_BOLD, 10);
for (String word : wordList) {
stream.beginText();
stream.moveTextPositionByAmount(x, y - rowHeight);
stream.drawString(word);
stream.endText();
if (wordList.size() != 1)
rowHeight = rowHeight + 10;
}
if (rowHeight >= space) {
y = y - (rowHeight + 10);
} else {
y = y - space;
}
return y;
}
Thanks in advice
I am trying to make something
Like: this (Having bold and normal text on the same line).
I've been using streams:
The preferable way for creating bold (or otherwise styled) text is to use a font variant which has been created explicitly for that variant. If no such font is available, though, it is possible to artificially emulate those styles:
In code:
PDRectangle rec = new PDRectangle(220, 120);
PDDocument document = null;
document = new PDDocument();
PDPage page = new PDPage(rec);
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page, true, true);
content.beginText();
content.moveTextPositionByAmount(7, 105);
content.setFont(PDType1Font.HELVETICA, 12);
content.drawString("Normal text and ");
content.setFont(PDType1Font.HELVETICA_BOLD, 12);
content.drawString("bold text");
content.moveTextPositionByAmount(0, -25);
content.setFont(PDType1Font.HELVETICA_OBLIQUE, 12);
content.drawString("Italic text and ");
content.setFont(PDType1Font.HELVETICA_BOLD_OBLIQUE, 12);
content.drawString("bold italic text");
content.endText();
content.setLineWidth(.5f);
content.beginText();
content.moveTextPositionByAmount(7, 55);
content.setFont(PDType1Font.HELVETICA, 12);
content.drawString("Normal text and ");
content.appendRawCommands("2 Tr\n");
content.drawString("artificially bold text");
content.appendRawCommands("0 Tr\n");
content.moveTextPositionByAmount(0, -25);
content.appendRawCommands("1 Tr\n");
content.drawString("Artificially outlined text");
content.appendRawCommands("0 Tr\n");
content.setTextMatrix(1, 0, .2f, 1, 7, 5);
content.drawString("Artificially italic text and ");
content.appendRawCommands("2 Tr\n");
content.drawString("bold italic text");
content.appendRawCommands("0 Tr\n");
content.endText();
content.close();
document.save("StyledTexts.pdf");
document.close();
The result: