如何使用 PDFBOX 生成动态页数 [英] How to generate Dyanamic no of pages using PDFBOX

查看:74
本文介绍了如何使用 PDFBOX 生成动态页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须根据某些输入生成一个 pdf 文件.每次运行代码时,输​​入的长度可能会有所不同,因此如何根据我的输入内容动态地将页面添加到文档中.

公共类pdfproject{静态 int lineno=768;public static void main (String[] args) 抛出异常{PDDocument 文档 = 新的 PDDocument();PDPage 页面 = 新的 PDPage();doc.addPage(页面);PDPageContentStream cos = new PDPageContentStream(doc, page);for(int i=0;i<2000;i++){renderText("你好"+i,cos,60);}cos.close();doc.save("test.pdf");doc.close();}static void renderText(String Info,PDPageContentStream cos,int marginwidth) 抛出异常{线号-=12;System.out.print("lineno="+lineno);PDFont fontPlain = PDType1Font.HELVETICA;cos.beginText();cos.setFont(fontPlain, 10);cos.moveTextPositionByAmount(marginwidth,lineno);cos.drawString(信息);cos.endText();}}

当当前页面没有空间时,如何通过动态添加新页面来确保内容呈现在下一页上?

解决方案

Pdfbox 不包括任何自动布局支持.因此,您必须跟踪页面的填充程度,并且必须关闭当前页面、创建一个新页面、重置填充指示器等

这显然不应该在某些项目类的静态成员中完成,而应该在某些专用类及其实例成员中完成.例如

公共类 PdfRenderingSimple 实现 AutoCloseable{////渲染//public void renderText(String Info, int marginwidth) 抛出 IOException{if (content == null || textRenderingLineY <12)新页面();textRenderingLineY-=12;System.out.print("lineno=" + textRenderingLineY);PDFont fontPlain = PDType1Font.HELVETICA;content.beginText();content.setFont(fontPlain, 10);content.moveTextPositionByAmount(marginwidth, textRenderingLineY);content.drawString(信息);content.endText();}////构造函数//公共 PdfRenderingSimple(PDDocument 文档){this.doc = doc;}////自动关闭实现///*** 关闭当前页面*/@覆盖public void close() 抛出 IOException{如果(内容!= null){content.close();内容=空;}}////辅助方法//void newPage() 抛出 IOException{关闭();PDPage 页面 = 新的 PDPage();doc.addPage(页面);content = new PDPageContentStream(doc, page);content.setNonStrokingColor(Color.BLACK);textRenderingLineY = 768;}////成员//最终的 PDDocument 文档;私有 PDPageContentStream 内容 = null;私人 int textRenderingLineY = 0;}

<块引用>

(PdfRenderingSimple.java)

你可以这样使用

PDDocument doc = new PDDocument();PdfRenderingSimple 渲染器 = 新 PdfRenderingSimple(doc);for (int i = 0; i <2000; i++){renderer.renderText("你好" + i, 60);}渲染器.close();doc.save(new File("renderSimple.pdf"));doc.close();

<块引用>

(RenderSimple.java)

为了获得更专业的渲染支持,您将实现改进的渲染类,例如PdfRenderingEndorsementAlternative.java 来自这个答案.

I have to generate a pdf file depending on some input .Each time the code runs , the input length may vary , so how can I add pages to the document dynamically depending on my input content .

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();
     }
     }

How do i ensure that the content is rendered on the next page by adding a new page dynamically when there is no space on the current page ?

解决方案

Pdfbox does not include any automatic layouting support. Thus, you have to keep track of how full a page is, and you have to close the current page, create a new one, reset fill indicators, etc

This obviously should not be done in static members in some project class but instead in some dedicated class and its instance members. E.g.

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.java)

You can use it like this

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();

(RenderSimple.java)

For more specialized rendering support you will implement improved rendering classes, e.g. PdfRenderingEndorsementAlternative.java from this answer.

这篇关于如何使用 PDFBOX 生成动态页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆