我正在使用Pdfbox使用Java生成PDF文件.问题是当我在文档中添加长文本内容时,它无法正确显示.只显示其中的一部分.这也是一条线.
我希望文本有多行.
我的代码如下:
PDPageContentStream pdfContent=new PDPageContentStream(pdfDocument, pdfPage, true, true);
pdfContent.beginText();
pdfContent.setFont(pdfFont, 11);
pdfContent.moveTextPositionByAmount(30,750);
pdfContent.drawString("I am trying to create a PDF file with a lot of text contents in the document. I am using PDFBox");
pdfContent.endText();
Run Code Online (Sandbox Code Playgroud)
我的输出:

我正在使用 Java 将输出写入 a PDDocument,然后将该文档附加到现有文档,然后再将其提供给客户端。
其中大部分运行良好。我只有一个小问题试图在写入时处理内容溢出PDDocument。我想跟踪文本插入文档的位置,以便当“光标”可以说超过某个点时,我将创建一个新页面,将其添加到文档中,创建一个新的内容流,并照常继续。
这是一些代码,显示了我想做的事情:
// big try block
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
int fontSize = 12;
content.beginText();
content.setFont(...);
content.moveTextPositionByAmount(margin, pageHeight-margin);
for ( each element in a collection of values ) {
content.moveTextPositionByAmount(0, -fontSize); // only moves down in document
// at this point, check if past the end of page, if so add a new page
if (content.getTextYPosition(...) < margin) { …Run Code Online (Sandbox Code Playgroud)