【问题标题】:How to generate Dyanamic no of pages using PDFBOX如何使用 PDFBOX 生成动态页面数
【发布时间】:2015-09-02 04:15:36
【问题描述】:

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

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

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

【问题讨论】:

  • Pdfbox 不包括任何自动布局支持。因此,您必须跟踪页面的填充程度(例如通过测试lineno),并且您必须关闭当前页面、创建新页面、重置填充指示器等。
  • 是的,新页面应该在满足 (lineno>0 && lineno

标签: java pdf pdf-generation pdfbox


【解决方案1】:

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

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

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)

你可以这样使用它

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)

要获得更专业的渲染支持,您将实现改进的渲染类,例如PdfRenderingEndorsementAlternative.java 来自this answer

【讨论】:

  • 谢谢 mkl .. 这有帮助。
猜你喜欢
  • 2013-01-30
  • 2018-07-28
  • 2012-04-30
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2019-12-13
  • 1970-01-01
相关资源
最近更新 更多