添加依赖 项目要使用iText,必须引入jar包。才能使用,maven依赖如下: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> 输出中文,还要引入下面itext-asian.jar包: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> 设置pdf文件密码,还要引入下面bcprov-jdk15on.jar包: <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.54</version> </dependency>
测试类:
1 package com.foster; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.net.URL; 7 import java.util.List; 8 9 import org.junit.Test; 10 11 import com.itextpdf.text.BaseColor; 12 import com.itextpdf.text.Chapter; 13 import com.itextpdf.text.Document; 14 import com.itextpdf.text.DocumentException; 15 import com.itextpdf.text.Element; 16 import com.itextpdf.text.Font; 17 import com.itextpdf.text.Image; 18 import com.itextpdf.text.ListItem; 19 import com.itextpdf.text.Paragraph; 20 import com.itextpdf.text.Section; 21 import com.itextpdf.text.pdf.BaseFont; 22 import com.itextpdf.text.pdf.PdfContentByte; 23 import com.itextpdf.text.pdf.PdfPCell; 24 import com.itextpdf.text.pdf.PdfPRow; 25 import com.itextpdf.text.pdf.PdfPTable; 26 import com.itextpdf.text.pdf.PdfReader; 27 import com.itextpdf.text.pdf.PdfStamper; 28 import com.itextpdf.text.pdf.PdfWriter; 29 30 public class TestPDFDemo1 { 31 32 /** 33 * 生成pdf 34 * 35 * @throws FileNotFoundException 36 * @throws DocumentException 37 */ 38 39 @Test 40 public void test01() throws FileNotFoundException, DocumentException { 41 // 1.新建document对象 42 Document document = new Document(); 43 44 // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 45 // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。 46 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 47 48 // 3.打开文档 49 document.open(); 50 51 // 4.添加一个内容段落 52 document.add(new Paragraph("Hello World!")); 53 54 // 5.关闭文档 55 document.close(); 56 } 57 58 /** 59 * 生成的pdf的详细的信息 60 * 坐标是和我们初中数学中的坐标轴一致的 61 * @throws Exception 62 */ 63 @Test 64 public void test02() throws Exception { 65 // 创建文件 66 Document document = new Document(); 67 // 建立一个书写器 68 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 69 // 打开文件 70 document.open(); 71 // 添加内容 72 document.add(new Paragraph("Some content here")); 73 74 // 设置属性 75 // 标题 76 document.addTitle("this is a title"); 77 // 作者 78 document.addAuthor("H__D"); 79 // 主题 80 document.addSubject("this is subject"); 81 // 关键字 82 document.addKeywords("Keywords"); 83 // 创建时间 84 document.addCreationDate(); 85 // 应用程序 86 document.addCreator("hd.com"); 87 88 // 关闭文档 89 document.close(); 90 // 关闭书写器 91 writer.close(); 92 } 93 94 /** 95 * 96 * 生成图片 97 * 98 * @throws Exception 99 */ 100 @Test 101 public void test03() throws Exception { 102 // 创建文件 103 Document document = new Document(); 104 // 建立一个书写器 105 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 106 // 打开文件 107 document.open(); 108 // 添加内容 109 document.add(new Paragraph("HD content here")); 110 111 // 图片1 112 Image image1 = Image.getInstance("F://logo.png"); 113 // 设置图片位置的x轴和y周 114 image1.setAbsolutePosition(100f, 100f); 115 // 设置图片的宽度和高度 116 image1.scaleAbsolute(200, 100); 117 // 将图片1添加到pdf文件中 118 document.add(image1); 119 120 // 图片2 .相当于图片的而连接 121 Image image2 = Image.getInstance(new URL("http://static.cnblogs.com/images/adminlogo.gif")); 122 // 将图片2添加到pdf文件中 123 document.add(image2); 124 125 // 关闭文档 126 document.close(); 127 // 关闭书写器 128 writer.close(); 129 } 130 131 /** 132 * 生成3列的表 133 * 134 * @throws DocumentException 135 * @throws FileNotFoundException 136 */ 137 @Test 138 public void test04() throws DocumentException, FileNotFoundException { 139 // 创建文件 140 Document document = new Document(); 141 // 建立一个书写器 142 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 143 // 打开文件 144 document.open(); 145 // 添加内容 146 document.add(new Paragraph("HD content here")); 147 148 // 3列的表. 149 PdfPTable table = new PdfPTable(3); 150 table.setWidthPercentage(100); // 宽度100%填充 151 table.setSpacingBefore(30f); // 前间距 152 table.setSpacingAfter(10f); // 后间距 153 154 List<PdfPRow> listRow = table.getRows(); 155 // 设置列宽 156 float[] columnWidths = { 1f, 2f, 3f }; 157 table.setWidths(columnWidths); 158 159 // 行1 160 PdfPCell cells1[] = new PdfPCell[3]; 161 PdfPRow row1 = new PdfPRow(cells1); 162 163 // 单元格 164 cells1[0] = new PdfPCell(new Paragraph("111"));// 单元格内容 165 cells1[0].setBorderColor(BaseColor.RED);// 边框验证 166 cells1[0].setPaddingLeft(30);// 左填充20 167 cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中 168 cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);// 垂直居中 169 170 cells1[1] = new PdfPCell(new Paragraph("222")); 171 cells1[2] = new PdfPCell(new Paragraph("333")); 172 173 // 行2 174 PdfPCell cells2[] = new PdfPCell[3]; 175 PdfPRow row2 = new PdfPRow(cells2); 176 cells2[0] = new PdfPCell(new Paragraph("444")); 177 178 // 把第一行添加到集合 179 listRow.add(row1); 180 listRow.add(row2); 181 // 把表格添加到文件中 182 document.add(table); 183 184 // 关闭文档 185 document.close(); 186 // 关闭书写器 187 writer.close(); 188 } 189 190 /** 191 * 添加有序的列表 192 * 193 * @throws Exception 194 */ 195 @Test 196 public void test05() throws Exception { 197 // 创建文件 198 Document document = new Document(); 199 // 建立一个书写器 200 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 201 // 打开文件 202 document.open(); 203 // 添加内容 204 document.add(new Paragraph("HD content here")); 205 206 // 添加有序列表 207 com.itextpdf.text.List orderedList = new com.itextpdf.text.List(com.itextpdf.text.List.ORDERED); 208 orderedList.add(new ListItem("Item one")); 209 orderedList.add(new ListItem("Item two")); 210 orderedList.add(new ListItem("Item three")); 211 document.add(orderedList); 212 213 // 关闭文档 214 document.close(); 215 // 关闭书写器 216 writer.close(); 217 } 218 219 /** 220 * PDF中设置样式/格式化输出,输出中文内容,必须引入itext-asian.jar 生成蓝色的中文字体 221 * 222 * @throws Exception 223 */ 224 @Test 225 public void test06() throws Exception { 226 // 创建文件 227 Document document = new Document(); 228 // 建立一个书写器 229 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 230 // 打开文件 231 document.open(); 232 233 // 中文字体,解决中文不能显示问题 234 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 235 236 // 蓝色字体 237 Font blueFont = new Font(bfChinese); 238 blueFont.setColor(BaseColor.BLUE); 239 // 段落文本 240 Paragraph paragraphBlue = new Paragraph("paragraphOne blue front", blueFont); 241 document.add(paragraphBlue); 242 243 // 绿色字体 244 Font greenFont = new Font(bfChinese); 245 greenFont.setColor(BaseColor.GREEN); 246 // 创建章节 247 Paragraph chapterTitle = new Paragraph("段落标题xxxx", greenFont); 248 Chapter chapter1 = new Chapter(chapterTitle, 1); 249 chapter1.setNumberDepth(0); 250 251 Paragraph sectionTitle = new Paragraph("部分标题", greenFont); 252 Section section1 = chapter1.addSection(sectionTitle); 253 254 Paragraph sectionContent = new Paragraph("部分内容", blueFont); 255 section1.add(sectionContent); 256 257 // 将章节添加到文章中 258 document.add(chapter1); 259 260 // 关闭文档 261 document.close(); 262 // 关闭书写器 263 writer.close(); 264 } 265 266 /** 267 * 给pdf设置密码 268 * 269 * @param args 270 * @throws DocumentException 271 * @throws IOException 272 */ 273 @Test 274 public void test07() throws Exception { 275 // 创建文件 276 Document document = new Document(); 277 // 建立一个书写器 278 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 279 280 // 用户密码 281 String userPassword = "123456"; 282 // 拥有者密码 283 String ownerPassword = "hd"; 284 writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING, 285 PdfWriter.ENCRYPTION_AES_128); 286 287 // 打开文件 288 document.open(); 289 290 // 添加内容 291 document.add(new Paragraph("password!!!")); 292 293 // 关闭文档 294 document.close(); 295 // 关闭书写器 296 writer.close(); 297 } 298 299 /** 300 * 给pdf设置权限..?? 301 * 302 * @throws Exception 303 */ 304 @Test 305 public void test08() throws Exception { 306 // 创建文件 307 Document document = new Document(); 308 // 建立一个书写器 309 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://testPdf//1.pdf")); 310 311 // 只读权限 312 writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 313 314 // 打开文件 315 document.open(); 316 317 // 添加内容 318 document.add(new Paragraph("password !!!!")); 319 320 // 关闭文档 321 document.close(); 322 // 关闭书写器 323 writer.close(); 324 } 325 /** 326 * 读取已有的pdf的文件 327 * @throws Exception 328 */ 329 330 @Test 331 public void test09() throws Exception { 332 333 // 读取pdf文件 334 PdfReader pdfReader = new PdfReader("D://testPdf//1.pdf"); 335 336 // 修改器 337 PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("F://2.pdf")); 338 339 Image image = Image.getInstance(new URL("http://static.cnblogs.com/images/adminlogo.gif")); 340 image.scaleAbsolute(50, 50); 341 image.setAbsolutePosition(0, 700);//位置 342 343 for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { 344 PdfContentByte content = pdfStamper.getUnderContent(i); 345 content.addImage(image); 346 } 347 348 pdfStamper.close(); 349 } 350 351 }