Itext生成PDF文件
来源:互联网 发布:sql select表 编辑:程序博客网 时间:2024/04/30 04:10
最近做一个项目的报表,设计导出问题,要求pdf、excel、word。说实话一个字,烦。写个备忘录吧。。。虽然还是很烦
所需依赖:itext-asian-5.2.0.jar(亚洲字体),itextpdf-5.5.7.jar
iText 是一个可从这里免费获取的 Java 库。iText 库非常强大,且支持生成 HTML、RTF 和 XML 文档,还支持生成 PDF 文档。您可以选择各种各样的字体来用于文档中。另外,iText 的结构允许您使用相同代码生成上面提及的任何类型的文档。
主要API:
com.itextpdf.text.Document 是用于生成 PDF 文档的主要类。这是第一个需要实例化的类。创建文档之后,您需要一个编写器来向其中写入信息。com.itextpdf.text.pdf.PdfWriter 是一个 PDF 编写器。下面提供了其他一些常用的类:
com.itextpdf.text.Paragraph:此类表示一个缩进的段落。
com.itextpdf.text.Chapter:此类表示 PDF 文档中的一章。使用 Paragraph 作为标题、int 作为章节编号来创建该类。
com.itextpdf.text.Font:此类包含一种字体的所有规范,比如字体集、字号、样式和颜色。各种字体都在此类中声明为静态常量。
com.itextpdf.text.List:此类表示一个列表,反过来,该列表又包含许多 ListItems。
com.itextpdf.text.pdf.PDFPTable:这是一个表格,可放在一个绝对位置上,也可作为类 Table 添加到文档中。
com.itextpdf.text.Anchor:一个 Anchor 可能是一个引用,或是一个引用的目标。
对象的包含关系:Section->chapter->document. 所有的文本添加均依赖于Paragraph
可将表格、列表、图像添加在Section中
接下来看一些代码:
1.创建文档
Document document = new Document(PageSize.A4, 50, 50, 50, 50); 参数依次对应页面大小,左右上下边距,此时类型未定,类型依赖于com.itextpdf.text.pdf.PdfWriter等编写器类型,还有HtmlWriter、RtfWriter、XmlWriter,见名知意。
2.创建PrintWriter
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
document.open();
参数:文档对象的引用,将写入文件的绝对名称。
3.创建段落对象(paragraph):
document.add(new Paragraph("Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 255, 0, 0))));
参数:添加的字符串,字体
任何文本都借助 com.itextpdf.text.Paragraph 来进行添加。您可使用您的文本以及字体、颜色、字号等默认设置创建一个默认段落。否则,您可提供自己的字体。
4.创建章对象(chapter):
Paragraph title1 = new Paragraph("Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA,
18, Font.BOLDITALIC, new CMYKColor(0, 255, 255,17)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
参数:章节标题,章节编号
设置编号深度设置为0就不会再文当中显示章节编号
5.创建节对象(section):
Section section1 = chapter1.addSection(title11,font);
section1.add(new Paragraph("Following is a 3 X 2 table."));
参数:节标题,字体
6.创建表格(PDFPTable):
PdfPTable t = new PdfPTable(3);
t.setSpacingBefore(25);//前后空白
t.setSpacingAfter(25);
PdfPCell c1 = new PdfPCell(new Phrase("Header1"));
t.addCell(c1);
PdfPCell c2 = new PdfPCell(new Phrase("Header2"));
t.addCell(c2);
PdfPCell c3 = new PdfPCell(new Phrase("Header3"));
t.addCell(c3);
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
表格创建时必须指定列数,行数不固定。cell按从左到右从上到下依次添加
7.创建列表(List):
List l = new List(true, false, 10);
l.add(new ListItem("First item of list"));
l.add(new ListItem("Second item of list"));
section1.add(l);
参数:
8.创建图像(Image):
Image image2 = Image.getInstance("IBMLogo.bmp");//重载方法,实际应用中参数一般为byte数组
image2.scaleAbsolute(120f, 120f);
section1.add(image2);
完成所有内容创建后将 chapter1 添加到主 document 中,关闭文档对象:
document.add(chapter1);
document.close();
亚洲字体设置(亲测可用):
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font font = new Font(baseFont);
现在就可以像自带字体一样使用font了
所需依赖:itext-asian-5.2.0.jar(亚洲字体),itextpdf-5.5.7.jar
iText 是一个可从这里免费获取的 Java 库。iText 库非常强大,且支持生成 HTML、RTF 和 XML 文档,还支持生成 PDF 文档。您可以选择各种各样的字体来用于文档中。另外,iText 的结构允许您使用相同代码生成上面提及的任何类型的文档。
主要API:
com.itextpdf.text.Document 是用于生成 PDF 文档的主要类。这是第一个需要实例化的类。创建文档之后,您需要一个编写器来向其中写入信息。com.itextpdf.text.pdf.PdfWriter 是一个 PDF 编写器。下面提供了其他一些常用的类:
com.itextpdf.text.Paragraph:此类表示一个缩进的段落。
com.itextpdf.text.Chapter:此类表示 PDF 文档中的一章。使用 Paragraph 作为标题、int 作为章节编号来创建该类。
com.itextpdf.text.Font:此类包含一种字体的所有规范,比如字体集、字号、样式和颜色。各种字体都在此类中声明为静态常量。
com.itextpdf.text.List:此类表示一个列表,反过来,该列表又包含许多 ListItems。
com.itextpdf.text.pdf.PDFPTable:这是一个表格,可放在一个绝对位置上,也可作为类 Table 添加到文档中。
com.itextpdf.text.Anchor:一个 Anchor 可能是一个引用,或是一个引用的目标。
对象的包含关系:Section->chapter->document. 所有的文本添加均依赖于Paragraph
可将表格、列表、图像添加在Section中
接下来看一些代码:
1.创建文档
Document document = new Document(PageSize.A4, 50, 50, 50, 50); 参数依次对应页面大小,左右上下边距,此时类型未定,类型依赖于com.itextpdf.text.pdf.PdfWriter等编写器类型,还有HtmlWriter、RtfWriter、XmlWriter,见名知意。
2.创建PrintWriter
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
document.open();
参数:文档对象的引用,将写入文件的绝对名称。
3.创建段落对象(paragraph):
document.add(new Paragraph("Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 255, 0, 0))));
参数:添加的字符串,字体
任何文本都借助 com.itextpdf.text.Paragraph 来进行添加。您可使用您的文本以及字体、颜色、字号等默认设置创建一个默认段落。否则,您可提供自己的字体。
4.创建章对象(chapter):
Paragraph title1 = new Paragraph("Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA,
18, Font.BOLDITALIC, new CMYKColor(0, 255, 255,17)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
参数:章节标题,章节编号
设置编号深度设置为0就不会再文当中显示章节编号
5.创建节对象(section):
Section section1 = chapter1.addSection(title11,font);
section1.add(new Paragraph("Following is a 3 X 2 table."));
参数:节标题,字体
6.创建表格(PDFPTable):
PdfPTable t = new PdfPTable(3);
t.setSpacingBefore(25);//前后空白
t.setSpacingAfter(25);
PdfPCell c1 = new PdfPCell(new Phrase("Header1"));
t.addCell(c1);
PdfPCell c2 = new PdfPCell(new Phrase("Header2"));
t.addCell(c2);
PdfPCell c3 = new PdfPCell(new Phrase("Header3"));
t.addCell(c3);
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
表格创建时必须指定列数,行数不固定。cell按从左到右从上到下依次添加
7.创建列表(List):
List l = new List(true, false, 10);
l.add(new ListItem("First item of list"));
l.add(new ListItem("Second item of list"));
section1.add(l);
参数:
8.创建图像(Image):
Image image2 = Image.getInstance("IBMLogo.bmp");//重载方法,实际应用中参数一般为byte数组
image2.scaleAbsolute(120f, 120f);
section1.add(image2);
完成所有内容创建后将 chapter1 添加到主 document 中,关闭文档对象:
document.add(chapter1);
document.close();
亚洲字体设置(亲测可用):
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font font = new Font(baseFont);
现在就可以像自带字体一样使用font了
1 0
- Itext生成PDF文件
- 用iText生成PDF文件
- Itext 生成 pdf文件使用方法:
- iText生成PDF报表文件
- Freemarker+IText生成pdf文件
- 使用iText生成pdf文件
- 关于iText生成PDF文件
- 使用iText生成PDF文件
- 生成PDF文件的Java库iText
- Java应用iText动态生成PDF文件
- 生成PDF文件的Java库iText
- itext使用模板生成pdf文件
- 使用itext组件生成PDF文件
- itext生成pdf文件的HelloWorld
- itext使用模板生成pdf文件
- itext使用html标签生成pdf文件
- 【Java】使用iText生成PDF文件
- java-pdf-(itext+adobe acrobat+pdf模板)生成pdf文件
- vs2010中启用jquery的智能感知
- istView.setItemsCanFocus(true);
- java JDK 动态代理(如何使用,以及工作原理解析)
- Java Io 笔记总结
- java 读文件的几种方式
- Itext生成PDF文件
- 基于C++中常见内存错误的总结
- JVM-GC算法_分代收集算法
- Ubuntu 查找 最近 大 文件
- 关于判断WebBrowser1加载状态 转帖
- U3D和ULUA热更新
- objc_msgSend()报错Too many arguments to function call
- 你陪我在一起的那几年
- Ubuntu下编译 cleanflight