Java利⽤iText操作PDF功能⼤全
iText是著名的开放项⽬,是⽤于⽣成PDF⽂档的⼀个java类库。通过iText不仅可以⽣成PDF或rtf的⽂档,⽽且可以将XML、Html⽂件转化为PDF⽂件。
1、⽣成⼀个PDF
//Step 1—Create a Document.
Document document = new Document();
//Step 2—Get a PdfWriter instance.
//Step 3—Open the Document.
document.open();
//Step 4—Add content.
document.add(new Paragraph("Hello World"));
//Step 5—Clo the Document.
document.clo();
2、页⾯⼤⼩,页⾯背景⾊,页边空⽩,Title,Author,Subject,Keywords
//页⾯⼤⼩
爱护花草树木的宣传标语Rectangle rect = new Rectangle(ate());
//页⾯背景⾊
rect.tBackgroundColor(BaColor.ORANGE);
Document doc = new Document(rect);
PdfWriter writer = Instance(doc, out);
//PDF版本(默认1.4)
writer.tPdfVersion(PdfWriter.PDF_VERSION_1_2);
/
/⽂档属性
doc.addTitle("Title@sample");
doc.addAuthor("Author@rensanning");
doc.addSubject("Subject@iText sample");
doc.addKeywords("Keywords@iText");
doc.addCreator("Creator@iText");
//页边空⽩
doc.tMargins(10, 20, 30, 40);
doc.open();
doc.add(new Paragraph("Hello World"));
3、设置密码
PdfWriter writer = Instance(doc, out);
// 设置密码为:"World"
writer.tEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_SCREENREADERS,
PdfWriter.STANDARD_ENCRYPTION_128);
doc.open();
doc.add(new Paragraph("Hello World"));
4、添加Page
document.open();
document.add(new Paragraph("First page")); document.add(new Version())); wPage();
writer.tPageEmpty(fal);
document.add(new Paragraph("New page"));
5、添加⽔印(背景图)
//图⽚⽔印
PdfReader reader = new PdfReader(FILE_DIR + "tWatermark.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR
+ "tWatermark2.pdf"));
Image img = Instance("resource/watermark.jpg");
img.tAbsolutePosition(200, 400);
PdfContentByte under = UnderContent(1);
under.addImage(img);
//⽂字⽔印
PdfContentByte over = OverContent(2);
over.beginText();
BaFont bf = ateFont(BaFont.HELVETICA, BaFont.WINANSI,
BaFont.EMBEDDED);
over.tFontAndSize(bf, 18);
over.tTextMatrix(30, 30);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
//背景图
Image img2 = Instance("resource/test.jpg");
img2.tAbsolutePosition(0, 0);
PdfContentByte under2 = UnderContent(3);
under2.addImage(img2);
打基础
stamp.clo();
reader.clo();
6、插⼊Chunk, Phra, Paragraph, List庸人自扰什么意思
//Chunk对象: a String, a Font, and some attributes
document.add(new Chunk("China"));
document.add(new Chunk(" "));
Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaColor.WHITE); Chunk id = new Chunk("chine", font);
id.tBackground(BaColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id.tTextRi(6);
document.add(id);
document.add(Chunk.NEWLINE);
document.add(new Chunk("Japan"));
document.add(new Chunk(" "));
Font font2 = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaColor.WHITE); Chunk id2 = new Chunk("japane", font2);
id2.tBackground(BaColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id2.tTextRi(6);
id2.tUnderline(0.2f, -2f);
document.add(id2);
document.add(Chunk.NEWLINE);
//Phra对象: a List of Chunks with leading
document.add(new Phra("Phra page"));
Phra director = new Phra();
Chunk name = new Chunk("China");
name.tUnderline(0.2f, -2f);
director.add(name);古诗名言
director.add(new Chunk(","));
director.add(new Chunk(" "));
director.add(new Chunk("chine"));
director.tLeading(24);
document.add(director);
document.add(director);
Phra director2 = new Phra();
Chunk name2 = new Chunk("Japan");
name2.tUnderline(0.2f, -2f);
director2.add(name2);
director2.add(new Chunk(","));
director2.add(new Chunk(" "));
director2.add(new Chunk("japane"));
director2.tLeading(24);
document.add(director2);
//Paragraph对象: a Phra with extra properties and a newline wPage();
document.add(new Paragraph("Paragraph page"));
Paragraph info = new Paragraph();
info.add(new Chunk("China "));
info.add(new Chunk("chine"));
info.add(Chunk.NEWLINE);
info.add(new Phra("Japan "));
info.add(new Phra("japane"));
九顶山滑雪场document.add(info);
//List对象: a quence of Paragraphs called ListItem
List list = new List(List.ORDERED);
for (int i = 0; i < 10; i++) {
ListItem item = new ListItem(String.format("%s: %d movies",
"country" + (i + 1), (i + 1) * 100), new Font(
Font.FontFamily.HELVETICA, 6, Font.BOLD, BaColor.WHITE)); List movielist = new List(List.ORDERED, List.ALPHABETICAL);
movielist.tLowerca(List.LOWERCASE);
for (int j = 0; j < 5; j++) {
ListItem movieitem = new ListItem("Title" + (j + 1));
List directorlist = new List(List.UNORDERED);
for (int k = 0; k < 3; k++) {
directorlist.add(String.format("%s, %s", "Name1" + (k + 1),
"Name2" + (k + 1)));
}
movieitem.add(directorlist);
movielist.add(movieitem);
}
天津社保item.add(movielist);
list.add(item);
}
document.add(list);
7、插⼊Anchor, Image, Chapter, Section
//Anchor对象: internal and external links
Paragraph country = new Paragraph();
Anchor dest = new Anchor("china", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaColor.BLUE));
dest.tName("CN");
dest.tReference("");//external
country.add(dest);
新土味情话
country.add(String.format(": %d sites", 10000));
document.add(country);
Anchor toUS = new Anchor("Go to first page.", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaColor.BLUE)); toUS.tReference("#CN");//internal
document.add(toUS);
//Image对象
Image img = Instance("resource/test.jpg");
img.tAlignment(Image.LEFT | Image.TEXTWRAP);
img.tBorder(Image.BOX);
img.tBorderWidth(10);
img.tBorderColor(BaColor.WHITE);
img.scaleToFit(1000, 72);//⼤⼩
img.tRotationDegrees(-30);//旋转
document.add(img);
//Chapter, Section对象(⽬录)
Paragraph title = new Paragraph("Title");
Chapter chapter = new Chapter(title, 1);
title = new Paragraph("Section A");
Section ction = chapter.addSection(title);
ction.tBookmarkTitle("bmk");
ction.tIndentation(30);
ction.tBookmarkOpen(fal);
奖杯简笔画ction.tNumberStyle(
Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
Section subction = ction.addSection(new Paragraph("Sub Section A"));
subction.tIndentationLeft(20);
subction.tNumberDepth(1);
document.add(chapter);
8、画图