Java 如何使用PDFBOX生成动态页码
我必须根据某些输入生成pdf文件。每次代码运行时,输入长度可能会有所不同,因此如何根据输入内容动态向文档添加页面Java 如何使用PDFBOX生成动态页码,java,pdf,pdf-generation,pdfbox,Java,Pdf,Pdf Generation,Pdfbox,我必须根据某些输入生成pdf文件。每次代码运行时,输入长度可能会有所不同,因此如何根据输入内容动态向文档添加页面 public class pdfproject { static int lineno=768; public static void main (String[] args) throws Exception { PDDocument doc= new PDDocument(); PDPage pag
public class pdfproject
{
static int lineno=768;
public static void main (String[] args) throws Exception
{
PDDocument doc= new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream cos = new PDPageContentStream(doc, page);
for(int i=0;i<2000;i++)
{
renderText("hello"+i,cos,60);
}
cos.close();
doc.save("test.pdf");
doc.close();
}
static void renderText(String Info,PDPageContentStream cos,int marginwidth) throws Exception
{
lineno-=12;
System.out.print("lineno="+lineno);
PDFont fontPlain = PDType1Font.HELVETICA;
cos.beginText();
cos.setFont(fontPlain, 10);
cos.moveTextPositionByAmount(marginwidth,lineno);
cos.drawString(Info);
cos.endText();
}
}
公共类PDF项目
{
静态int lineno=768;
公共静态void main(字符串[]args)引发异常
{
PDDocument doc=新的PDDocument();
PDPage page=新PDPage();
文件添加页(第页);
PDPageContentStream cos=新的PDPageContentStream(文档,页面);
for(int i=0;iPdfbox不包括任何自动布局支持。因此,您必须跟踪页面的完整程度,并且必须关闭当前页面、创建新页面、重置填充指示器等
这显然不应该在某些项目类的静态成员中完成,而应该在某些专用类及其实例成员中完成
public class PdfRenderingSimple implements AutoCloseable
{
//
// rendering
//
public void renderText(String Info, int marginwidth) throws IOException
{
if (content == null || textRenderingLineY < 12)
newPage();
textRenderingLineY-=12;
System.out.print("lineno=" + textRenderingLineY);
PDFont fontPlain = PDType1Font.HELVETICA;
content.beginText();
content.setFont(fontPlain, 10);
content.moveTextPositionByAmount(marginwidth, textRenderingLineY);
content.drawString(Info);
content.endText();
}
//
// constructor
//
public PdfRenderingSimple(PDDocument doc)
{
this.doc = doc;
}
//
// AutoCloseable implementation
//
/**
* Closes the current page
*/
@Override
public void close() throws IOException
{
if (content != null)
{
content.close();
content = null;
}
}
//
// helper methods
//
void newPage() throws IOException
{
close();
PDPage page = new PDPage();
doc.addPage(page);
content = new PDPageContentStream(doc, page);
content.setNonStrokingColor(Color.BLACK);
textRenderingLineY = 768;
}
//
// members
//
final PDDocument doc;
private PDPageContentStream content = null;
private int textRenderingLineY = 0;
}
公共类PdfRenderingSimple实现自动关闭
{
//
//渲染
//
公共void renderText(字符串信息,int marginwidth)引发IOException
{
if(content==null | | textrendingliney<12)
newPage();
textrendingliney-=12;
系统输出打印(“lineno=“+textrendingliney”);
PDFont fontPlain=PDType1Font.HELVETICA;
content.beginText();
content.setFont(fontPlain,10);
content.moveTextPositionByAmount(边距宽度、textRenderingLineY);
内容.抽绳(信息);
content.endText();
}
//
//建造师
//
公共PdfRenderingSimple(PDDocument文档)
{
this.doc=doc;
}
//
//自动关闭实现
//
/**
*关闭当前页面
*/
@凌驾
public void close()引发IOException
{
如果(内容!=null)
{
content.close();
内容=空;
}
}
//
//辅助方法
//
void newPage()引发IOException
{
close();
PDPage page=新PDPage();
文件添加页(第页);
内容=新PDPageContentStream(文档,页面);
content.setNonSrokingColor(颜色为黑色);
textRenderingLineY=768;
}
//
//成员
//
最终文件;
私有PDPageContentStream内容=null;
私有int textRenderingLineY=0;
}
()
你可以这样使用它
PDDocument doc = new PDDocument();
PdfRenderingSimple renderer = new PdfRenderingSimple(doc);
for (int i = 0; i < 2000; i++)
{
renderer.renderText("hello" + i, 60);
}
renderer.close();
doc.save(new File("renderSimple.pdf"));
doc.close();
PDDocument doc=新的PDDocument();
PdfRenderingSimple渲染器=新的PdfRenderingSimple(doc);
对于(int i=0;i<2000;i++)
{
renderText(“hello”+i,60);
}
close();
保存(新文件(“renderSimple.pdf”);
doc.close();
()
对于更专业的呈现支持,您将实现改进的呈现类,例如from。Pdfbox不包括任何自动布局支持。因此,您必须跟踪页面的完整程度(例如通过测试lineno
),您必须关闭当前页面、创建新页面、重置填充指示器等。是的,新页面应在(lineno>0&&linenoThanks mkl.)时生成。这很有帮助。