Java利用iText操作PDF功能大全

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—Close the Document.
document.close();
2、页⾯⼤⼩,页⾯背景⾊,页边空⽩,Title,Author,Subject,Keywords
//页⾯⼤⼩
Rectangle rect = new Rectangle(ate());
//页⾯背景⾊
rect.setBackgroundColor(BaseColor.ORANGE);
Document doc = new Document(rect);
PdfWriter writer = Instance(doc, out);
//PDF版本(默认1.4)
writer.setPdfVersion(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.setMargins(10, 20, 30, 40);
doc.open();
doc.add(new Paragraph("Hello World"));
3、设置密码
PdfWriter writer = Instance(doc, out);
// 设置密码为:"World"
writer.setEncryption("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.setPageEmpty(false);
document.add(new Paragraph("New page"));
5、添加⽔印(背景图)
//图⽚⽔印
PdfReader reader = new PdfReader(FILE_DIR + "setWatermark.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR
+ "setWatermark2.pdf"));
Image img = Instance("resource/watermark.jpg");
img.setAbsolutePosition(200, 400);
PdfContentByte under = UnderContent(1);
under.addImage(img);
//⽂字⽔印
PdfContentByte over = OverContent(2);
over.beginText();
上海 国企改革
BaseFont bf = ateFont(BaseFont.HELVETICA, BaseFont.WINANSI,
BaseFont.EMBEDDED);
over.setFontAndSize(bf, 18);
over.setTextMatrix(30, 30);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
//背景图
Image img2 = Instance("resource/test.jpg");
img2.setAbsolutePosition(0, 0);
甘露消毒饮PdfContentByte under2 = UnderContent(3);
under2.addImage(img2);
stamp.close();
reader.close();
6、插⼊Chunk, Phrase, 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, BaseColor.WHITE);  Chunk id = new Chunk("chinese", font);
id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id.setTextRise(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, BaseColor.WHITE);  Chunk id2 = new Chunk("japanese", font2);
id2.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id2.setTextRise(6);
id2.setUnderline(0.2f, -2f);
document.add(id2);
document.add(Chunk.NEWLINE);
//Phrase对象: a List of Chunks with leading
document.add(new Phrase("Phrase page"));
Phrase director = new Phrase();
Chunk name = new Chunk("China");
name.setUnderline(0.2f, -2f);
director.add(name);
director.add(new Chunk(","));
director.add(new Chunk(" "));
director.add(new Chunk("chinese"));
director.setLeading(24);
document.add(director);
document.add(director);
Phrase director2 = new Phrase();
Chunk name2 = new Chunk("Japan");
name2.setUnderline(0.2f, -2f);
director2.add(name2);
director2.add(new Chunk(","));
director2.add(new Chunk(" "));
director2.add(new Chunk("japanese"));
director2.setLeading(24);
document.add(director2);
//Paragraph对象: a Phrase 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("chinese"));
info.add(Chunk.NEWLINE);
info.add(new Phrase("Japan "));
info.add(new Phrase("japanese"));
document.add(info);
//List对象: a sequence 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, BaseColor.WHITE));      List movielist = new List(List.ORDERED, List.ALPHABETICAL);
movielist.setLowercase(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, BaseColor.BLUE));
dest.setName("CN");
dest.setReference("www.china");//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, BaseColor.BLUE));  toUS.setReference("#CN");//internal
document.add(toUS);
//Image对象
Image img = Instance("resource/test.jpg");
img.setAlignment(Image.LEFT | Image.TEXTWRAP);
img.setBorder(Image.BOX);
img.setBorderWidth(10);
img.setBorderColor(BaseColor.WHITE);
img.scaleToFit(1000, 72);//⼤⼩
img.setRotationDegrees(-30);//旋转
document.add(img);
//Chapter, Section对象(⽬录)
数字频率计设计
Paragraph title = new Paragraph("Title");
Chapter chapter = new Chapter(title, 1);
title = new Paragraph("Section A");
Section section = chapter.addSection(title);
section.setBookmarkTitle("bmk");
section.setIndentation(30);
section.setBookmarkOpen(false);
section.setNumberStyle(
Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
Section subsection = section.addSection(new Paragraph("Sub Section A"));
subsection.setIndentationLeft(20);
subsection.setNumberDepth(1);
document.add(chapter);什么叫差动保护
8、画图

本文发布于:2024-09-21 22:39:08,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/297667.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:频率计   操作   论坛   默认   设置
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议