PDFBOX相同流,带有粗体和普通文本

Fra*_*ool 2 java fonts pdfbox

好吧,我一直在使用PDFBox,我仍然完全不理解它,但我已经阅读了文档,使用字体和其他一些地方,但我已经找到了如何从PDF获取文本,它是风格,但我正在创造它,而不是阅读它.

我想做点什么

喜欢: this(在同一行上有粗体和普通文本).

我一直在使用流:

不确定这是否是帮助我所需的所有代码,因为我刚加入这个项目但是我加入时已经开始了.

如果你能帮助我实现这个代码,或者可以帮助我提供一个可以阅读解决方案的想法或来源,我会感谢大家.

我在这里留下我的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的建议

mkl*_*mkl 11

关于你的问题

我想做点什么

喜欢: this(在同一行上有粗体和普通文本).

我一直在使用流:

创建粗体(或其他样式)文本的首选方法是使用为该变体明确创建的字体变体.但是,如果没有这样的字体,则可以人为地模拟这些样式:

  • 人为粗体:使用文本渲染模式2不仅填充字母区域,还沿其轮廓绘制一条线
  • 人为概述:使用文本渲染模式1沿字母的轮廓绘制一条线而不是填充它
  • 人为斜体(实际上倾斜:改变文本矩阵以扭曲输出.

在代码中:

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();
Run Code Online (Sandbox Code Playgroud)

结果:

StyledTexts.pdf的屏幕截图

关于提供的代码

您拥有的代码不会创建100%有效的PDF:它设置字体

stream.setFont(PDType1Font.TIMES_BOLD, 10)
Run Code Online (Sandbox Code Playgroud)

在文本对象之外

stream.beginText();
...
stream.endText();
Run Code Online (Sandbox Code Playgroud)

这是无效的PDF; 但是,许多PDF观众都接受了它.

此外,它真的很难读(神秘的名字,奇怪的架构).