【问题标题】:How to write content into pdf use iText?如何使用 iText 将内容写入 pdf?
【发布时间】:2010-11-16 21:33:06
【问题描述】:

现在我使用 iText 自动生成 pdf。 而我的问题是,当内容真的很大时,我需要计算内容的高度和宽度,然后添加新页面...... 这真的很不方便。

所以我想知道是否有这样的方法: Document.add("一篇非常非常大的文章"); 在此之后,它会自动生成一个pdf文件????

提前致谢!

【问题讨论】:

  • 问自己同样的问题。
  • 您要添加什么样的“内容”?
  • 在您的程序中,将添加内容的代码行包装在 for(int i = 0; i

标签: java pdf-generation itext


【解决方案1】:

以下创建一个9页的pdf,无需计算高度和宽度。

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class HelloWorld {
        public static void main(String[] args) {
                Document document = new Document();
                try {
                        PdfWriter.getInstance(document,
                                        new FileOutputStream("HelloWorld.pdf"));
                        document.open();
                        String text = "";
                        for (int i = 0; i < 10000; i++) {
                                text += "test";
                        }
                        document.add(new Paragraph(text));
                } catch (DocumentException e) {
                        System.err.println(e.getMessage());
                } catch (IOException ex) {
                        System.err.println(ex.getMessage());
                }
                document.close();
        }
}

【讨论】:

    【解决方案2】:

    当当前页面内容已满时,会自动生成一个新页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 2011-12-31
      • 2014-08-22
      • 1970-01-01
      • 2022-08-04
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多