java生成带书签的pdf,iText生成PDF的书签

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 Michael Sun

*/

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值