[JAVA] java动态生成PDF文档,
[JAVA] java动态生成PDF文档,
生成PDF文档,虽然需求不多,但总是存在这样的需要。
java生成PDF文档,需要的jar包[没办法,还是需要利用框架,这也是java强大的一个理由]。
jar包下载地址:http://download.csdn.net/detail/gopain/8180711
jar包已经解决中文的编码问题,可以直接使用。
首先,介绍官方的案例代码:以下即为PDF编辑的过程。
/*
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
*/
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
一个Document就是一份PDF文档,此处参数很显然,后面4个50分别表示边距。
显然,定义一个document之后就该对其编辑、存储。
/*
* import com.lowagie.text.pdf.PdfWriter;
*/
File pdf = new File("PDF/ITextTest.pdf");
f(!new File("PDF").exists()) new File("PDF").mkdir();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf));//throws FileNotFoundException, DocumentException此处会抛出异常,为了节约版面,就直接抛出了。
PdfWriter是iText编辑PDF文档的编辑器,那么就要指向新建的document,document为编辑对象,pdf(File)为输出文件。
以上就是编辑(生成)PDF的开始。[其结束为document.close();]
现在就可以run一下了。结果:
因为还没有写入任何元素,所以,打开为空。
下面分别写入以下元素:
创建段落,
创建章对象,
创建节对象,
创建表格对象,
创建列表对象,
添加图片。
1. 创建段落
<span > </span>/*
* import com.lowagie.text.Anchor;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.CMYKColor;
*/
document.open();
Anchor anchorTarget = new Anchor("First page of the document.");
anchorTarget.setName("BackToTop");
Paragraph paragraph1 = new Paragraph();
paragraph1.setSpacingBefore(50);
paragraph1.add(anchorTarget);
document.add(paragraph1);
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))));
document.close();
document.open()之后就可以为其添加元素和节点了,最后还要document.close().不是每次添加数据都要open和close,只需要在编辑开始前open然写完后最后添加close就好了。结果图:
2.创建章对象
<span > </span>/*
* import com.lowagie.text.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);
创建了一个新的章对象 chapter1
,它的标题为
“This is Chapter 1”。将编号深度设置为 0,这样就不会在页面上显示章编号。
效果图:
3.创建节对象
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
new CMYKColor(0, 255, 255,17)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
然后创建一个节对象,负数以上创建的章节。
效果图:
4.创建表格
/*
* import com.lowagie.text.pdf.PdfPCell;
* import com.lowagie.text.pdf.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);
document.add(chapter1);
效果图:
5.添加图片:
<span > </span>/*
* import com.lowagie.text.Image;
*/
document.add(Image.getInstance("PDF/f.png"));//MalformedURLException
抛出异常,MalformedURLException。
效果图:
像素太高了,显示不全。
以上代码段都是官方实例。
以下给出处理中文编码问题。
/*
* import com.lowagie.text.pdf.BaseFont;
*/
BaseFont bfChinese = null;// FontFactory.getFont(FontFactory.COURIER,
// 14, Font.BOLD, new CMYKColor(0, 255,
// 0, 0);
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Font f10 = new Font(bfChinese, 10, Font.NORMAL);
Font f12 = new Font(bfChinese, 12, Font.BOLD);
document.add(new Paragraph("中文字体" , f10));
document.add(new Paragraph("试中文字体:" , f12));
效果图:
请使用以上链接给出的iText需要的jar包,测试可以使用。
测试全部代码:
package cn.gopain.main;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.lowagie.text.Anchor;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* 博客编辑案例:java生成PDF文档
*
* @author gopain
*
*/
public class JavaPDF {
public static void main(String[] args) throws DocumentException,
MalformedURLException, IOException {
/*
* import com.lowagie.text.Document; import com.lowagie.text.PageSize;
*/
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
/*
* import com.lowagie.text.pdf.PdfWriter;
*/
File pdf = new File("PDF/ITextTest.pdf");
if (!new File("PDF").exists())
new File("PDF").mkdir();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdf));// throws FileNotFoundException,
// DocumentException
/*
* import com.lowagie.text.Anchor; import com.lowagie.text.Font; import
* com.lowagie.text.FontFactory; import com.lowagie.text.PageSize;
* import com.lowagie.text.Paragraph; import
* com.lowagie.text.pdf.CMYKColor;
*/
document.open();
Anchor anchorTarget = new Anchor("First page of the document.");
anchorTarget.setName("BackToTop");
Paragraph paragraph1 = new Paragraph();
paragraph1.setSpacingBefore(50);
paragraph1.add(anchorTarget);
document.add(paragraph1);
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))));
/*
* import com.lowagie.text.Chapter; import com.lowagie.text.Section;
*/
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);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
new CMYKColor(0, 255, 255, 17)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph(
"This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
/*
* import com.lowagie.text.pdf.PdfPCell; import
* com.lowagie.text.pdf.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);
document.add(chapter1);
/*
* import com.lowagie.text.Image;
*/
document.add(Image.getInstance("PDF/f.png"));// MalformedURLException
/*
* import com.lowagie.text.pdf.BaseFont;
*/
BaseFont bfChinese = null;// FontFactory.getFont(FontFactory.COURIER,
// 14, Font.BOLD, new CMYKColor(0, 255,
// 0, 0);
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Font f10 = new Font(bfChinese, 10, Font.NORMAL);
Font f12 = new Font(bfChinese, 12, Font.BOLD);
document.add(new Paragraph("中文字体" , f10));
document.add(new Paragraph("试中文字体:" , f12));
document.close();
}
}
iText的官方jar包已经更新到5.1.x了,但是不一定最新就是最好,好用才好,iText给出的PDF输出都比较基础,当然也比较灵活。
也有直接把HTML/XML直接输出到PDF的jar包,也是基于iText。但是这样可以修改的空间就比较大了。
项目中需要怎样的功能就使用怎么样的工具,能解决实际问题的才是关键,而不是一定要精通很多技术。
相关文章
- 暂无相关文章
用户点评