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:

  • artificially bold: use text rendering mode 2 to not only fill the letter area but also draw a line along its outline
  • artificially outlined: use text rendering mode 1 to draw a line along the outline of the letter instead of filling it
  • artificially italic (actually slanted: change the text matrix to skew the output.

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:

Screenshot of StyledTexts.pdf

其他提示

As an edit of @mkl's answer

I'm using PDFBox 2.0.17, and on a PDPageContentStream, you have the setRenderingMode method.

To simulate bold text, here is a little sample :

try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true)) {
    contentStream.beginText();
    contentStream.setFont(font, fontSize);
    contentStream.setRenderingMode(RenderingMode.FILL_STROKE);
    contentStream.setNonStrokingColor(color);
    contentStream.setTextMatrix(Matrix.getTranslateInstance(positionX, positionY));
    contentStream.showText(text);
    contentStream.endText();        
}

setRenderingMode inner code:

public void setRenderingMode(RenderingMode rm) throws IOException
{
    writeOperand(rm.intValue());
    writeOperator("Tr");
}

which matches the previous content.appendRawCommands("2 Tr\n");

Hope this will help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top