说一说iText(2)

看看怎样生成pdf的章节,也就是pdf的书签:
public static void main(String[] args) throws DocumentException,
			IOException {
		String pdfPath = "d:/test2.pdf";
		Document document = new Document(PageSize.A4);
		PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
		document.open();
		// 中文字体必备
		BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED);
		// 定义章字体
		Font chaFont = new Font(bf, 12, Font.BOLD, BaseColor.BLACK);
		// 定义节字体
		Font secFont = new Font(bf, 10, Font.BOLDITALIC, BaseColor.RED);
		// 定义文本字体
		Font textFont = new Font(bf, 8, Font.NORMAL, BaseColor.BLUE);

		// 创建章1
		Chapter chapter1 = new Chapter(new Paragraph("介绍我", chaFont), 1);
		// 创建节1_1
		Section section1_1 = chapter1
				.addSection(new Paragraph("基本信息", secFont));
		// 设置节本身的缩进
		section1_1.setIndentationLeft(10);
		// 设置节包含文本的缩进
		section1_1.setIndentation(10);
		// 设置该书签的下级书签是否展开
		section1_1.setBookmarkOpen(false);
		// 设置该节的数字样式
		section1_1.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
		// 设置该节包含的文本
		section1_1.add(new Paragraph("KB的程序员一名……", textFont));

		// 创建节1_2
		Section section1_2 = chapter1
				.addSection(new Paragraph("详细信息", secFont));
		section1_2.setIndentationLeft(10);
		section1_2.setIndentation(10);
		section1_2.setBookmarkOpen(false);
		section1_2.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
		section1_2.add(new Paragraph("各种地址", textFont));

		// 创建节1_2_1
		Section section1_2_1 = section1_2.addSection(new Paragraph(new Chunk(
				"我的博客", secFont).setUnderline(0.2f, -2f).setAnchor(
				"http://boke")));
		section1_2_1.setIndentationLeft(10);
		section1_2_1.setIndentation(10);
		section1_2_1.setBookmarkOpen(false);
		section1_2_1
				.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
		section1_2_1.add(new Paragraph(new Chunk("http://boke", textFont)
				.setUnderline(0.2f, -2f).setAnchor("http://boke")));
		section1_2_1.add(new Paragraph("我的博客分享", textFont));

		// 创建节1_2_2
		Section section1_2_2 = section1_2.addSection(new Paragraph(new Chunk(
				"我的微博", secFont).setUnderline(0.2f, -2f).setAnchor(
				"http://weibo")));
		section1_2_2.setIndentationLeft(10);
		section1_2_2.setIndentation(10);
		section1_2_2.setBookmarkOpen(false);
		section1_2_2
				.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
		section1_2_2.add(new Paragraph(new Chunk("http://weibo", textFont)
				.setUnderline(0.2f, -2f).setAnchor("http://weibo")));
		section1_2_2.add(new Paragraph("我的微博分享", textFont));

		// 添加分割线
		LineSeparator ls = new LineSeparator(1, 100, BaseColor.RED,
				Element.ALIGN_CENTER, -2);
		Paragraph pLine = new Paragraph("分割线", textFont);
		pLine.add(ls);
		chapter1.add(pLine);

		// 添加章到文档中
		document.add(chapter1);

		Chapter chapter2 = new Chapter(new Paragraph("介绍他", chaFont), 2);
		document.add(chapter2);

		document.close();

	}


生成的pdf截图:
说一说iText(2)

看看注释和截图基本上是没什么好说的了……


你可能感兴趣的:(itext,书签,section,CHAPTER,章节)