itext常用API

更新时间:2023-07-31 06:14:10 阅读: 评论:0

1、生成一个PDF
Java代码  收藏代码
//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
Java代码  收藏代码
//页面大小 
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、设置密码
Java代码  收藏代码
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
Java代码  收藏代码
document.open(); 
document.add(new Paragraph("First page")); 
document.add(new Version())); 
潘玮柏歌曲
writer.tPageEmpty(fal); 
document.add(new Paragraph("New page")); 
5、添加水印(背景图)
Java代码  收藏代码
//图片水印 
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
Java代码  收藏代码
//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); 
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 
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
Java代码  收藏代码
//Anchor对象: internal and external links 
Paragraph country = new Paragraph(); 
Anchor dest = new Anchor("china", new Font(Font.FontFamily.HELVET
ICA, 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、画图
Java代码  收藏代码
//左右箭头 
document.add(new VerticalPositionMark() { 
public void draw(PdfContentByte canvas, float llx, float lly, 
float urx, float ury, float y) { 
canvas.beginText(); 
BaFont bf = null; 
try { 
bf = ateFont(BaFont.ZAPFDINGBATS, "", BaFont.EMBEDDED); 
} catch (Exception e) { 
e.printStackTrace(); 
canvas.tFontAndSize(bf, 12); 
// LEFT 
canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), llx - 10, y, 0); 
// RIGHT 
canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), urx + 10, y + 8, 180); 
});  秦统一六国
//直线 
Paragraph p1 = new Paragraph("LEFT"); 
p1.add(new Chunk(new LineSeparator())); 
p1.add("R"); 
document.add(p1); 
//点线 
Paragraph p2 = new Paragraph("LEFT"); 
p2.add(new Chunk(new DottedLineSeparator())); 
p2.add("R"); 
document.add(p2); 
//下滑线 
LineSeparator UNDERLINE = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2); 
Paragraph p3 = new Paragraph("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN"); 
p3.add(UNDERLINE); 
document.add(p3); 
9、设置段落
Java代码  收藏代码
Paragraph p = new Paragraph("In the previous example, you added a header and footer with the showTextAligned() method. This example demonstrates that it’s sometimes more interesting to u PdfPTable and writeSelect
edRows(). You can define a bottom border for each cell so that the header is underlined. This is the most elegant way to add headers and footers, becau the table mechanism allows you to position and align lines, images, and text."); 
//默认 
p.tAlignment(Element.ALIGN_JUSTIFIED); 
document.add(p); 
p.tAlignment(Element.ALIGN_JUSTIFIED); 
p.tIndentationLeft(1 * 15f); 
p.tIndentationRight((5 - 1) * 15f); 
个人素质包括哪些
document.add(p); 
//居右 
p.tAlignment(Element.ALIGN_RIGHT); 
p.tSpacingAfter(15f); 
document.add(p); 
//居左 
p.tAlignment(Element.ALIGN_LEFT); 
p.tSpacingBefore(15f); 
document.add(p); 
//居中 
p.tAlignment(Element.ALIGN_CENTER); 
p.tSpacingAfter(15f); 
p.tSpacingBefore(15f); 
document.add(p); 
10、删除Page
Java代码  收藏代码
FileOutputStream out = new FileOutputStream(FILE_DIR + "deletePage.pdf"); 
Document document = new Document(); 
PdfWriter writer = Instance(document, out); 
document.open(); 
document.add(new Paragraph("First page")); 
document.add(new Version())); 
番薯的功效与作用wPage(); 
writer.tPageEmpty(fal); 
document.add(new Paragraph("New page")); 
document.clo(); 
PdfReader reader = new PdfReader(FILE_DIR + "deletePage.pdf"); 
reader.lectPages("1,3"); 
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR 
+ "deletePage2.pdf")); 
stamp.clo(); 
reader.clo(); 
11、插入Page
Java代码  收藏代码
FileOutputStream out = new FileOutputStream(FILE_DIR + "inrtPage.pdf"); 
Document document = new Document(); 
document.open(); 
document.add(new Paragraph("1 page")); 
document.add(new Paragraph("2 page")); 
document.add(new Paragraph("3 page")); 
document.clo(); 
PdfReader reader = new PdfReader(FILE_DIR + "inrtPage.pdf"); 
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR 
+ "inrtPage2.pdf")); 
stamp.inrtPage(2, PageSize(1)); 
ColumnText ct = new ColumnText(null); 
ct.addElement(new Paragraph(24, new Chunk("INSERT PAGE"))); 
ct.OverContent(2)); 
ct.tSimpleColumn(36, 36, 559, 770); 
stamp.clo(); 
reader.clo(); 
12、排序page
Java代码  收藏代码
PdfWriter writer = Instance(doc, out); 
writer.tLinearPageMode(); 
doc.open(); 
doc.add(new Paragraph("1 page")); 
doc.add(new Paragraph("2 page")); 
doc.add(new Paragraph("3 page")); 
doc.add(new Paragraph("4 page")); 
doc.add(new Paragraph("5 page")); 
int[] order = {4,3,2,1}; 
writer
.reorderPages(order); 
13、目录
Java代码  收藏代码
// Code 1 
document.add(new Chunk("Chapter 1").tLocalDestination("1")); 
document.add(new Chunk("Chapter 2").tLocalDestination("2")); 
document.add(new Paragraph(new Chunk("Sub 2.1").tLocalDestination("2.1"))); 
document.add(new Paragraph(new Chunk("Sub 2.2").tLocalDestination("2.2"))); 
document.add(new Chunk("Chapter 3").tLocalDestination("3")); 
// Code 2 
PdfContentByte cb = DirectContent(); 
PdfOutline root = cb.getRootOutline(); 
// Code 3 
@SuppressWarnings("unud") 
PdfOutline oline1 = new PdfOutline(root, LocalPage("1", fal), "Chapter 1"); 
PdfOutline oline2 = new PdfOutline(root, LocalPage("2", fal), "Chapter 2"); 
oline2.tOpen(fal); 
@SuppressWarnings("unud") 
PdfOutline oline2_1 = new PdfOutline(oline2, LocalPage("2.1", fal), "Sub 2.1"); 
@SuppressWarnings("unud") 
PdfOutline oline2_2 = new PdfOutline(oline2, LocalPage("2.2", fal), "Sub 2.2"); 
@SuppressWarnings("unud") 
PdfOutline oline3 = new PdfOutline(root, LocalPage("3", fal), "Chapter 3"); 
14、Header, Footer
Java代码  收藏代码
PdfWriter writer = Instance(doc, new FileOutputStream(FILE_DIR + "tHeaderFooter.pdf")); 
writer.tPageEvent(new PdfPageEventHelper() { 
public void onEndPage(PdfWriter writer, Document document) { 
PdfContentByte cb = DirectContent(); 
cb.saveState();  实施顾问
cb.beginText(); 
BaFont bf = null; 
try { 
bf = ateFont(BaFont.HELVETICA, BaFont.WINANSI, BaFont.EMBEDDED); 
} catch (Exception e) { 
e.printStackTrace(); 
cb.tFontAndSize(bf, 10); 
//Header 
float x = p(-20); 
//左 
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, 
"H-Left", 
document.left(), x, 0); 
//中 
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, 
(document.right() + document.left())/2, 
x, 0); 
//右 
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, 
"H-Right", 
document.right(), x, 0); 
//Footer 
float y = document.bottom(-20); 
//左 
戚继光的故事cb.showTextAligned(PdfContentByte.ALIGN_LEFT, 
"F-Left", 
document.left(), y, 0); 
//中 
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, 

本文发布于:2023-07-31 06:14:10,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1124451.html

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

标签:水印   设置   页面   背景色
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图