如何使用 PDFBox 在 pdf 文件中写入文本、画一条线然后再次写入文本
How to write text, draw a line and then again write text in a pdf file using PDFBox
需求:附上截图。我必须在 pdf 中写 2 行文本,然后画一条线,然后再次开始写一些文本。
因此,我的算法是:
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");
名称显示在页面的左下角。我希望这条线在下图中显示的线之后。
任何帮助将不胜感激。
不幸的是,问题中的代码相当不完整,没有特别显示每个文本对象中文本矩阵的初始化,并且还有许多未定义的变量。
因此,这里以一段代码为例,结果是文本 - 行 - 文本输出:
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 -= ...
行。
需求:附上截图。我必须在 pdf 中写 2 行文本,然后画一条线,然后再次开始写一些文本。
因此,我的算法是:
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");
名称显示在页面的左下角。我希望这条线在下图中显示的线之后。
任何帮助将不胜感激。
不幸的是,问题中的代码相当不完整,没有特别显示每个文本对象中文本矩阵的初始化,并且还有许多未定义的变量。
因此,这里以一段代码为例,结果是文本 - 行 - 文本输出:
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 -= ...
行。