如何将内容写入pdf使用iText? [英] How to write content into pdf use iText?

查看:238
本文介绍了如何将内容写入pdf使用iText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Right now i use iText to generate a pdf automatically. And my problem is that when the content is really very large, i need to calculate the content's height and width, and then add new page... this is really very inconvinent.

所以我想知道是否有类似的方法:
Document.add(非常大的文章);
之后,它将自动生成一个pdf文件????

so I wonder whether or not there is a method like: Document.add("a very very large article"); and after this , it will auto generate a pdf file ????

提前致谢!

推荐答案

以下内容创建了9页pdf,无需计算高度和宽度。

The following creates a 9 page pdf without having to calculate height and width.

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

这篇关于如何将内容写入pdf使用iText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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