【问题标题】:How to write text, draw a line and then again write text in a pdf file using PDFBox如何写文本,画一条线,然后再次使用 PDFBox 在 pdf 文件中写文本
【发布时间】:2017-11-14 03:41:51
【问题描述】:

要求:附上截图。我必须在 pdf 中写两行文本,然后画一条线,然后再次开始写一些文本。

因此,我的算法是这样的:

contentStream = new PDPageContentStream(doc, page);
contentStream.setFont(font, fontSize);
contentStream.beginText();

创建了一个新的 PDPageContentStream 并触发了函数 beginText()。我可以写出所附图像中显示的上部文本部分。

下面是上面的文本和行的以下代码行:

        contentStream.showText("Entry Form – Header");
        yCordinate -= fontHeight;  //This line is to track the yCordinate
        contentStream.newLineAtOffset(0, -leading);
        yCordinate -= leading;
        contentStream.showText("Date Generated: " + dateFormat.format(date));
        yCordinate -= fontHeight;
        contentStream.newLineAtOffset(0, -leading);
        yCordinate -= leading;
        contentStream.endText(); // End of text mode

我不得不结束这个文本模式,因为下面的 3 行代码(画线)不会在文本模式下执行:

            contentStream.moveTo(startX, yCordinate);
            contentStream.lineTo(endX, yCordinate);
            contentStream.stroke();        

现在在这行代码之后,如果我写:

contentStream.beginText();
contentStream.showText("Name: XXXXX");

名称显示在页面的左下角。我希望这条线在下图中显示的线之后。

任何帮助将不胜感激。

【问题讨论】:

  • beginText() ... 显示前两行文本 ... endText() ... 描边图形线 ... beginText() ... 为下一个文本行定位文本矩阵 ...显示剩余的文本行。
  • 在绘制线条后放置下一个文本行的文本矩阵时遇到问题。我试过contentStream.newLineAtOffset(0, yCordinate);,但没用。

标签: java pdfbox


【解决方案1】:

不幸的是,问题中的代码相当不完整,没有特别显示每个文本对象中文本矩阵的初始化,并且还有许多未定义的变量。

因此,这里以一段代码为例,导致文本-行-文本输出:

PDFont font = PDType1Font.HELVETICA;
float fontSize = 14;
float fontHeight = fontSize;
float leading = 20;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
Date date = new Date();

PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.setFont(font, fontSize);

float yCordinate = page.getCropBox().getUpperRightY() - 30;
float startX = page.getCropBox().getLowerLeftX() + 30;
float endX = page.getCropBox().getUpperRightX() - 30;

contentStream.beginText();
contentStream.newLineAtOffset(startX, yCordinate);
contentStream.showText("Entry Form – Header");
yCordinate -= fontHeight;  //This line is to track the yCordinate
contentStream.newLineAtOffset(0, -leading);
yCordinate -= leading;
contentStream.showText("Date Generated: " + dateFormat.format(date));
yCordinate -= fontHeight;
contentStream.endText(); // End of text mode

contentStream.moveTo(startX, yCordinate);
contentStream.lineTo(endX, yCordinate);
contentStream.stroke();
yCordinate -= leading;

contentStream.beginText();
contentStream.newLineAtOffset(startX, yCordinate);
contentStream.showText("Name: XXXXX");
contentStream.endText();

contentStream.close();
doc.save("textLineText.pdf");

(TextAndGraphics.java 测试testDrawTextLineText)

这段代码导致:

如果您想要不同的距离,则必须在绘制图形线之前和之后调整 yCordinate -= ... 线。

【讨论】:

  • 感谢@mkl 的解决方案。它对我有用。您在答案部分添加的代码块与我的代码相似。我错过了在画线后放置yCordinate -= leading; (contentStream.stroke();)。添加后,对我来说效果很好,
猜你喜欢
  • 2014-10-20
  • 2016-01-05
  • 1970-01-01
  • 2019-01-15
  • 2013-12-02
  • 1970-01-01
  • 2012-12-30
  • 2014-07-11
  • 2019-09-02
相关资源
最近更新 更多