iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
项目要使用iText,必须引入jar包。才能使用,maven依赖如下:
1 <dependency> 2 <groupId>com.itextpdf</groupId> 3 <artifactId>itextpdf</artifactId> 4 <version>5.5.10</version> 5 </dependency>输出中文,还要引入下面itext-asian.jar包:
1 <dependency> 2 <groupId>com.itextpdf</groupId> 3 <artifactId>itext-asian</artifactId> 4 <version>5.2.0</version> 5 </dependency>设置pdf文件密码,还要引入下面bcprov-jdk15on.jar包:
1 <dependency> 2 <groupId>org.bouncycastle</groupId> 3 <artifactId>bcprov-jdk15on</artifactId> 4 <version>1.54</version> 5 </dependency>
打开文件
给PDF文件设置文件属性,例如:
1 public static void main(String[] args) throws FileNotFoundException, DocumentException { 2 3 //创建文件 4 Document document = new Document(); 5 //建立一个书写器 6 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test2.pdf")); 7 //打开文件 8 document.open(); 9 //添加内容 10 document.add(new Paragraph("Some content here")); 11 12 //设置属性 13 //标题 14 document.addTitle("this is a title"); 15 //作者 16 document.addAuthor("H__D"); 17 //主题 18 document.addSubject("this is subject"); 19 //关键字 20 document.addKeywords("Keywords"); 21 //创建时间 22 document.addCreationDate(); 23 //应用程序 24 document.addCreator("hd.com"); 25 26 //关闭文档 27 document.close(); 28 //关闭书写器 29 writer.close(); 30 }打开文件
PDF中添加图片
1 public static void main(String[] args) throws DocumentException, IOException { 2 3 //创建文件 4 Document document = new Document(); 5 //建立一个书写器 6 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test3.pdf")); 7 //打开文件 8 document.open(); 9 //添加内容 10 document.add(new Paragraph("HD content here")); 11 12 //图片1 13 Image image1 = Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG"); 14 //设置图片位置的x轴和y周 15 image1.setAbsolutePosition(100f, 550f); 16 //设置图片的宽度和高度 17 image1.scaleAbsolute(200, 200); 18 //将图片1添加到pdf文件中 19 document.add(image1); 20 21 //图片2 22 Image image2 = Image.getInstance(new URL("http://static.cnblogs.com/images/adminlogo.gif")); 23 //将图片2添加到pdf文件中 24 document.add(image2); 25 26 //关闭文档 27 document.close(); 28 //关闭书写器 29 writer.close(); 30 }打开文件
PDF中创建表格
按 Ctrl+C 复制代码 按 Ctrl+C 复制代码打开文件
PDF中创建列表
1 public static void main(String[] args) throws DocumentException, FileNotFoundException { 2 //创建文件 3 Document document = new Document(); 4 //建立一个书写器 5 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test5.pdf")); 6 //打开文件 7 document.open(); 8 //添加内容 9 document.add(new Paragraph("HD content here")); 10 11 //添加有序列表 12 List orderedList = new List(List.ORDERED); 13 orderedList.add(new ListItem("Item one")); 14 orderedList.add(new ListItem("Item two")); 15 orderedList.add(new ListItem("Item three")); 16 document.add(orderedList); 17 18 //关闭文档 19 document.close(); 20 //关闭书写器 21 writer.close(); 22 }打开文件
PDF中设置样式/格式化输出,输出中文内容,必须引入itext-asian.jar
1 public static void main(String[] args) throws DocumentException, IOException { 2 //创建文件 3 Document document = new Document(); 4 //建立一个书写器 5 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test6.pdf")); 6 //打开文件 7 document.open(); 8 9 //中文字体,解决中文不能显示问题 10 BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 11 12 //蓝色字体 13 Font blueFont = new Font(bfChinese); 14 blueFont.setColor(BaseColor.BLUE); 15 //段落文本 16 Paragraph paragraphBlue = new Paragraph("paragraphOne blue front", blueFont); 17 document.add(paragraphBlue); 18 19 //绿色字体 20 Font greenFont = new Font(bfChinese); 21 greenFont.setColor(BaseColor.GREEN); 22 //创建章节 23 Paragraph chapterTitle = new Paragraph("段落标题xxxx", greenFont); 24 Chapter chapter1 = new Chapter(chapterTitle, 1); 25 chapter1.setNumberDepth(0); 26 27 Paragraph sectionTitle = new Paragraph("部分标题", greenFont); 28 Section section1 = chapter1.addSection(sectionTitle); 29 30 Paragraph sectionContent = new Paragraph("部分内容", blueFont); 31 section1.add(sectionContent); 32 33 //将章节添加到文章中 34 document.add(chapter1); 35 36 //关闭文档 37 document.close(); 38 //关闭书写器 39 writer.close(); 40 }打开文件
给PDF文件设置密码,需要引入bcprov-jdk15on.jar包:
1 public static void main(String[] args) throws DocumentException, IOException { 2 // 创建文件 3 Document document = new Document(); 4 // 建立一个书写器 5 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test8.pdf")); 6 7 //用户密码 8 String userPassword = "123456"; 9 //拥有者密码 10 String ownerPassword = "hd"; 11 writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING, 12 PdfWriter.ENCRYPTION_AES_128); 13 14 // 打开文件 15 document.open(); 16 17 //添加内容 18 document.add(new Paragraph("password !!!!")); 19 20 // 关闭文档 21 document.close(); 22 // 关闭书写器 23 writer.close(); 24 }打开文件
给PDF文件设置权限
1 public static void main(String[] args) throws DocumentException, IOException { 2 // 创建文件 3 Document document = new Document(); 4 // 建立一个书写器 5 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test9.pdf")); 6 7 // 只读权限 8 writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 9 10 // 打开文件 11 document.open(); 12 13 // 添加内容 14 document.add(new Paragraph("password !!!!")); 15 16 // 关闭文档 17 document.close(); 18 // 关闭书写器 19 writer.close(); 20 }
读取/修改已有的PDF文件
1 public static void main(String[] args) throws DocumentException, IOException { 2 3 //读取pdf文件 4 PdfReader pdfReader = new PdfReader("C:/Users/H__D/Desktop/test1.pdf"); 5 6 //修改器 7 PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("C:/Users/H__D/Desktop/test10.pdf")); 8 9 Image image = Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG"); 10 image.scaleAbsolute(50, 50); 11 image.setAbsolutePosition(0, 700); 12 13 for(int i=1; i<= pdfReader.getNumberOfPages(); i++) 14 { 15 PdfContentByte content = pdfStamper.getUnderContent(i); 16 content.addImage(image); 17 } 18 19 pdfStamper.close(); 20 }打开文件
