本文主要演示iText生成PDF的书签功能,详细介绍和注意点直接看代码中的注释部分吧,下面直接上代码:
[一]、Java代码:
Demo4BookMark.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
package com.micmiu.pdf.itext; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chapter; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.draw.LineSeparator; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */ public class Demo4BookMark { /** * @param args */ public static void main(String[] args) throws Exception { String fileName = "d:/test/itext/demo-bookmark.pdf"; Demo4BookMark.testBookMarks(fileName); } /** * * @param fileName * @throws Exception */ public static void testBookMarks(String fileName) throws Exception { BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); // 章的字体 Font chFont = new Font(bfCN, 12, Font.NORMAL, BaseColor.BLUE); // 节的字体 Font secFont = new Font(bfCN, 12, Font.NORMAL, new BaseColor(0, 204, 255)); // 正文的字体 Font textFont = new Font(bfCN, 12, Font.NORMAL, BaseColor.BLACK); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); int chNum = 1; Chapter chapter = new Chapter(new Paragraph("Michael介绍", chFont), chNum++); Section section = chapter.addSection(new Paragraph("基本信息", secFont)); // section.setNumberDepth(2); // section.setBookmarkTitle("基本信息"); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Paragraph("苦逼的码农一枚。。。", textFont)); Section section2 = chapter.addSection(new Paragraph("SNS", secFont)); section2.setIndentation(10); section2.setIndentationLeft(10); section2.setBookmarkOpen(false); section2.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section2.add(new Paragraph("SNS地址分类:", textFont)); section = section2.addSection(new Paragraph(new Chunk("我的博客", secFont) .setUnderline(0.2f, -2f).setAnchor("http://www.micmiu.com"))); section.setBookmarkOpen(false); section.setIndentation(10); section.setIndentationLeft(10); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Paragraph(new Chunk("我的blog地址:http://www.micmiu.com", textFont).setUnderline(0.2f, -2f).setAnchor( "http://www.micmiu.com"))); section.add(new Paragraph("分享自己的技术心得。", textFont)); section = section2.addSection(new Paragraph(new Chunk("我的weibo", secFont).setUnderline(0.2f, -2f).setAnchor( "http://weibo.com/ctosun"))); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Paragraph(new Chunk("我的weibo:http://weibo.com/ctosun", textFont).setUnderline(0.2f, -2f).setAnchor( "http://weibo.com/ctosun"))); section.add(new Paragraph("发表下心情,分享下技术,转转乱七八糟的新闻。", textFont)); section = section2.addSection(new Paragraph(new Chunk("twitter", secFont))); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Paragraph(new Chunk("twitter:@suncto", textFont) .setUnderline(0.2f, -2f).setAnchor("http://twitter/cuncto"))); section.add(new Paragraph("一个常常被墙的地方", textFont)); LineSeparator line = new LineSeparator(1, 100, new BaseColor(204, 204, 204), Element.ALIGN_CENTER, -2); Paragraph p_line = new Paragraph("分割线"); p_line.add(line); chapter.add(p_line); document.add(chapter); chapter = new Chapter(new Paragraph("Miu的介绍", chFont), chNum++); section = chapter.addSection(new Paragraph("基本信息", secFont)); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Paragraph("90后一枚,喜欢美食和旅游。。。", textFont)); document.add(chapter); document.close(); } } |
[二]、运行结果:
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/opensource/expdoc/itext-bookmark/
😛 用了你这篇文章的例子,成功实现了PDF书签功能,在此表示感谢!分享是程序员最重要的精神。博主文章很不错,如果愿意我我的小博客交换链接的话,还请联系我~~