我正在使用Java将输出写入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) { // wishful thinking, doesn't exist
content.endText();
content.close();
page = new PDPage();
doc.addPage(page);
content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(...);
content.moveTextPositionByAmount(margin, pageHeight-(margin+fontSize));
}
content.drawString(...);
}
content.endText();
content.close();
重要的是content.getTextYPosition()
。它实际上并不存在,但我确信PDPageContentStream
必须跟踪类似的值。有没有办法访问这个值?
感谢。
答案 0 :(得分:2)
创建一个heightCounter
变量,用于跟踪您移动文本位置的距离。它的初始值可以是您的起始Y位置。
PDRectangle mediabox = page.findMediaBox();
float margin = 72;
float width = mediabox.getWidth() - 2 * margin;
float startX = mediabox.getLowerLeftX() + margin;
float startY = mediabox.getUpperRightY() - margin;
float heightCounter = startY;
每次移动文字位置时,都会从heightCounter
中减去该位置。如果heightCounter
小于您移动文字位置的位置,则创建新页面。
答案 1 :(得分:0)
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(startX, startY);
for (String line : lines) {
if(height>705){ //this is the height of my bottom line where I want cutoff. you can check yours by sysoout the content.
line = line.trim();
float charSpacing = 0;
if (line.length() > 1) {
float size = fontSize * pdfFont.getStringWidth(line) / 1000;
float free = width - size;
if (free > 0) {
charSpacing = free / (line.length() - 1);
}
}
contentStream.drawString(line);
contentStream.moveTextPositionByAmount(0, -leading);
System.out.println("content Stream line :" + line);
height--;
System.out.println("value of height:"+ height);
}
else{
contentStream.endText();
contentStream.close();
page = new PDPage(PDPage.PAGE_SIZE_A4);
doc.addPage(page);
contentStream = new PDPageContentStream(doc, page,false, true, true);
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(startX, startY);
System.out.println("Height counter value :"+ height);
System.out.println("line insde the second page:" + line);
contentStream.drawString(line);
System.out.println("Output on second page:"+contentStream.toString());
contentStream.moveTextPositionByAmount(0, -leading);
height=mediabox.getHeight() - 2 * margin; //
}
}
contentStream.endText();
contentStream.close();
}
答案 2 :(得分:0)
这是我在顶部的配置,供您参考如何使用。 快乐的编码..
PDPageContentStream contentStream = new PDPageContentStream(doc, page,true, true, true);
//PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
PDPageContentStream contentStream1 = new PDPageContentStream(doc, page,true, true, true);
PDFont pdfFont = PDType1Font.COURIER;
PDFont fontBold = PDType1Font.TIMES_BOLD;
float leading = 1.5f * fontSize;
PDRectangle mediabox = page.getMediaBox();
float margin = 45;
float width = mediabox.getWidth() - 2 * margin;
float height = mediabox.getHeight() - 2 * margin;
float startX = mediabox.getLowerLeftX() + margin - statVarX;
float startY = mediabox.getUpperRightY() - margin - statVarY;