iText PDF

参考: http://www.ibm.com/developerworks/cn/opensource/os-javapdf/

java生成PDF用的是开源的 iText 。

/**

* @throws Exception

*/

private static void PDFReport() throws Exception {

// 1.实例化

Document document = new Document();

 

// add Chinese font

BaseFont bfChinese = BaseFont.createFont("STSong-Light",

"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

 

// Font headfont=new Font(bfChinese,10,Font.BOLD);

Font keyfont = new Font(bfChinese, 8, Font.BOLD);

Font textfont = new Font(bfChinese, 8, Font.NORMAL);

 

// 2.创建 PdfWriter 对象

PdfWriter.getInstance(document, new FileOutputStream(new File(

"D:\\POReceiveReport.pdf")));

document.open();

 

document.newPage();

 

// 3.创建段落对象

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.",

FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,

new CMYKColor(0, 255, 0, 0))));

 

Paragraph pageNumber = new Paragraph("page 1/2", keyfont);

pageNumber.setSpacingBefore(50);// 设置空格。

document.add(pageNumber);

 

// create title image

Image jpeg = Image.getInstance("D:\\test.jpg");

jpeg.setAlignment(Image.ALIGN_CENTER);

jpeg.scaleAbsolute(530, 37);

document.add(jpeg);

 

// 5. 创建尾

Paragraph foot11 = new Paragraph("footer", keyfont);

document.add(foot11);

HeaderFooter footer11 = new HeaderFooter(foot11, true);

footer11.setAlignment(HeaderFooter.ALIGN_BOTTOM);

 

// 6. 清单 5. 创建章对象和 创建节对象

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

 

// 7. 创建表格

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

 

// 8.创建列表对象

com.lowagie.text.List l = new com.lowagie.text.List(true, false, 10);

l.add(new ListItem("First item of list"));

l.add(new ListItem("Second item of list"));

 

section1.add(l);

 

// 9. 将定位符添加到主文档中

Paragraph title2 = new Paragraph("Using Anchor", FontFactory.getFont(

FontFactory.HELVETICA, 16, Font.BOLD, new CMYKColor(0, 255, 0, 0)));

 

section1.add(title2);

title2.setSpacingBefore(5000);

Anchor anchor2 = new Anchor("Back To Top");

anchor2.setReference("#BackToTop");

 

section1.add(anchor2);

document.add(chapter1);

 

document.close();

 

 

}

你可能感兴趣的:(itext)