一、什么是iText?
在企业的信息系统中,报表处理一直占比较重要的作用,iText是一种生成PDF报表的Java组件。通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超链接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。
二、引入jar
1、项目要使用iText,必须引入jar包
1 2 3 4 5 |
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version> 5.5 . 6 </version> </dependency> |
2、输出中文,还要引入下面 itext-asian.jar 包
1 2 3 4 5 |
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version> 5.2 . 0 </version> </dependency> |
3、设置pdf文件密码,还要引入下面 bcprov-jdk15on.jar 包
1 2 3 4 5 |
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version> 1.47 </version> </dependency> |
三、iText常用类
com.itextpdf.text.Document :这是iText库中最常用的类,它代表了一个 pdf 实例。如果你需要从零开始生成一个pdf文件,你需要使用这个 Document 类。首先创建该实例,然后打开它,并添加内容,最后关闭该实例,即可生成一个pdf文件。 com.itextpdf.text.Paragraph :表示一个缩进的文本段落,在段落中,你可以设置对齐方式,缩进,段落前后间隔等 com.itextpdf.text.Chapter :表示 pdf 的一个章节,他通过一个 Paragraph 类型的标题和整形章数创建 com.itextpdf.text.Font :这个类包含了所有规范好的字体,包括 family of font ,大小,样式和颜色,所有这些字体都被声明为静态常量 com.itextpdf.text.List :表示一个列表; com.itextpdf.text.Anchor :表示一个锚,类似于HTML页面的链接。 com.itextpdf.text.pdf.PdfWriter :当这个 PdfWriter 被添加到 PdfDocument 后,所有添加到 Document 的内容将会写入到与文件或网络关联的输出流中。 com.itextpdf.text.pdf.PdfReader :用于读取 pdf 文件;
四、生成PDF步骤
1、创建文档
1 |
Document document = new Document(); |
2、通过书写器将文档写入磁盘
1 |
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDFWithColor.pdf" )); |
3、打开文档
1 |
document.open(); |
4、向文档中添加内容
1 |
document.add( new Paragraph( "i love CSDN" )); |
5、关闭文档
1 |
document.close(); |
五、Java操作pdf的工具类itext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
package com.neusoft.guor.itext;
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfAction; import com.itextpdf.text.pdf.PdfAnnotation; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfName; import com.itextpdf.text.pdf.PdfOutline; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfString; import com.itextpdf.text.pdf.PdfTransition; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.draw.DottedLineSeparator; import com.itextpdf.text.pdf.draw.LineSeparator; import com.itextpdf.text.pdf.draw.VerticalPositionMark; /** * itext PDF操作 */ public class PDFInit { public static final String FILE_DIR = "E:\\guor\\itext\\" ; public static final String const_JPG_JAVA = FILE_DIR + "java.jpg" ; public static final String const_JPG_NGINX = FILE_DIR + "nginx.jpg" ; private static final String const_NEZHA = "哪吒编程" ; private static final String const_NEZHA_PROGRAM = "获取Java学习资料请关注公众号:哪吒编程" ; private static final String const_BIBIDONG = "比比东" ; private static final String const_YUNYUN = "云韵" ; private static final String const_BAIDU = "百度一下 你就知道" ; private static final String const_BAIDU_URL = "https://HdhCmsTestbaidu测试数据" ; private static final String const_PAGE_FIRST = "第一页" ; private static final String const_PAGE_SECOND = "第二页" ; private static final String const_PAGE_THIRD = "第三页" ; private static final String const_PAGE_FOUR = "第四页" ; private static final String const_PAGE_FIVE = "第五页" ; private static final String const_TITLE_FIRST = "一级标题" ; private static final String const_TITLE_SECOND = "二级标题" ; private static final String const_CONTENT = "内容" ; // 普通中文字体 public static Font static_FONT_CHINESE = null ; // 超链字体 public static Font static_FONT_LINK = null ; private static void pdfFontInit() throws IOException, DocumentException { // 微软雅黑 BaseFont chinese = BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" , BaseFont.NOT_EMBEDDED); // 普通中文字体 static_FONT_CHINESE = new Font(chinese, 12 , Font.NORMAL); // Font.BOLD为加粗 // 超链字体 static_FONT_LINK = new Font(chinese, 12 , Font.NORMAL, BaseColor.BLUE); } public static Document document; public static void main(String[] args) throws Exception { pdfFontInit(); //createPDF();// 生成一个 PDF 文件 //createPDFWithColor();// 设置PDF的页面大小和背景颜色 //createPDFWithPassWord();// 创建带密码的PDF //createPDFWithNewPages();// 为PDF添加页 //createPDFWithWaterMark();// 为PDF文件添加水印,背景图 //createPDFWithContent();//插入块Chunk, 内容Phrase, 段落Paragraph, List //createPDFWithExtraContent();//插入Anchor, Image, Chapter, Section //draw();//画图 //createPDFWithAlignment();//设置段落 //createPDFToDeletePage();//删除 page //insertPage();// 插入 page //splitPDF();//分割 page //mergePDF();// 合并 PDF 文件 //sortpage();// 排序page //setHeaderFooter();// 页眉,页脚 //addColumnText();// 左右文字 //setView();// 文档视图 //pdfToZip();// 压缩PDF到Zip addAnnotation(); // 注释 /** * 创建一个 PDF 文件,并添加文本 */ public static void createPDF() throws IOException, DocumentException { // 实例化 document document = new Document(); // 生成文件 String path = FILE_DIR + "createPDF.pdf" ; File file = new File(path); if (!file.exists()){ file.createNewFile(); } PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDF.pdf" )); // 打开 document document.open(); // 添加文本 此处无法写入中文 TODO document.add( new Paragraph(const_NEZHA)); document.add( new Paragraph(const_NEZHA, static_FONT_CHINESE)); // 关闭 document document.close(); * 创建PDF文件,修改文件的属性 public static void createPDFWithColor() throws FileNotFoundException, DocumentException { // 页面大小 Rectangle rect = new Rectangle(PageSize.A5.rotate()); // 页面背景色 rect.setBackgroundColor(BaseColor.YELLOW); document = new Document(rect); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDFWithColor.pdf" )); // PDF版本(默认1.4) writer.setPdfVersion(PdfWriter.VERSION_1_6); // 文档属性 document.addAuthor(const_NEZHA); document.addTitle( "我的第一个pdf" ); // 页边空白 document.setMargins( 10 , 10 , 10 , 10 ); // 打开 // 关闭 * 创建带密码的PDF public static void createPDFWithPassWord() throws FileNotFoundException, DocumentException { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDFWithPassWord.pdf" )); // userPassword打开密码:"123" // ownerPassword编辑密码: "123456" writer.setEncryption( "123" .getBytes(), "123456" .getBytes(), PdfWriter.ALLOW_SCREENREADERS, PdfWriter.STANDARD_ENCRYPTION_128); * 为PDF添加页 public static void createPDFWithNewPages() throws FileNotFoundException, DocumentException { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDFAddNewPages.pdf" )); document.add( new Paragraph(const_PAGE_FIRST, static_FONT_CHINESE)); document.newPage(); document.add( new Paragraph(const_PAGE_SECOND, static_FONT_CHINESE)); writer.setPageEmpty( true ); document.add( new Paragraph(const_PAGE_THIRD, static_FONT_CHINESE)); * 为PDF文件添加水印,背景图 public static void createPDFWithWaterMark() throws IOException, DocumentException { FileOutputStream out = new FileOutputStream(FILE_DIR + "createPDFWithWaterMark.pdf" ); PdfWriter.getInstance(document, out); // 图片水印 PdfReader reader = new PdfReader(FILE_DIR + "createPDFWithWaterMark.pdf" ); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR + "createPDFWithWaterMark2.pdf" )); Image img = Image.getInstance(const_JPG_JAVA); img.setAbsolutePosition( 200 , 200 ); PdfContentByte under = stamp.getUnderContent( 1 ); under.addImage(img); // 文字水印 PdfContentByte over = stamp.getOverContent( 2 ); // 加载字库来完成对字体的创建 BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); over.beginText(); // 设置颜色 默认为蓝色 over.setColorFill(BaseColor.RED); // 设置字体字号 over.setFontAndSize(bf, 50 ); // 设置起始位置 over.setTextMatrix( 30 , 30 ); // 开始写入水印 左-下-倾斜度 over.showTextAligned(Element.ALIGN_LEFT, "nezha" , 245 , 400 , 30 ); over.endText(); // 背景图 Image img2 = Image.getInstance(const_JPG_NGINX); img2.setAbsolutePosition( 0 , 0 ); PdfContentByte under2 = stamp.getUnderContent( 3 ); under2.addImage(img2); stamp.close(); reader.close(); * 插入Chunk, Phrase, Paragraph, List * Chunk : 块,PDF文档中描述的最小原子元素 * Phrase : 短语,Chunk的集合 * Paragraph : 段落,一个有序的Phrase集合 public static void createPDFWithContent() throws DocumentException, FileNotFoundException { FileOutputStream out = new FileOutputStream(FILE_DIR + "createPDFWithContent.pdf" ); // 添加块 document.add( new Chunk(const_NEZHA, static_FONT_CHINESE)); Font font = new Font(Font.FontFamily.HELVETICA, 8 , Font.BOLD, BaseColor.WHITE); Chunk id = new Chunk( "springboot" , font); id.setBackground(BaseColor.BLACK, 1f, 0 .5f, 1f, 1 .5f); id.setTextRise( 7 ); document.add(id); Font font2 = new Font(Font.FontFamily.HELVETICA, 6 , Font.BOLD, BaseColor.WHITE); Chunk id2 = new Chunk( "springcloud" , font2); id2.setBackground(BaseColor.BLACK, 1f, 0 .5f, 1f, 1 .5f); id2.setTextRise( 3 ); id2.setUnderline( 0 .2f, -2f); document.add(id2); document.add(Chunk.NEWLINE); // 添加一页,添加短语 document.add( new Phrase( "Phrase page" )); // 添加短语 Phrase director = new Phrase(); Chunk name = new Chunk(const_BIBIDONG, static_FONT_CHINESE); // 添加下划线(thickness:下划线的粗细,yPosition:下划线离字的距离) name.setUnderline( 0 .5f, -1f); director.add(name); director.add( new Chunk( "," )); director.add( new Chunk( " " )); director.add( new Chunk(const_YUNYUN, static_FONT_CHINESE)); director.setLeading( 24 ); document.add(director); // 添加一页 Phrase director2 = new Phrase(); Chunk name2 = new Chunk(const_BIBIDONG, static_FONT_CHINESE); name2.setUnderline( 0 .2f, -2f); director2.add(name2); director2.add( new Chunk( "," )); director2.add( new Chunk( " " )); director2.add( new Chunk(const_YUNYUN, static_FONT_CHINESE)); director2.setLeading( 24 ); document.add(director2); // 添加段落 document.add( new Paragraph( "Paragraph page" )); Paragraph info = new Paragraph(); info.add( new Chunk(const_NEZHA)); info.add( new Chunk(const_BIBIDONG)); info.add(Chunk.NEWLINE); info.add( new Phrase(const_NEZHA)); document.add(info); // 通过循环添加段落信息 List list = new List(List.ORDERED); for ( int i = 0 ; i < 5 ; i++) { ListItem item = new ListItem(String.format( "%s: %d " +const_NEZHA, const_YUNYUN + (i + 1 ), (i + 1 ) * 100 ), new Font(Font.FontFamily.HELVETICA, 6 , Font.BOLD, BaseColor.WHITE)); List girllist = new List(List.ORDERED, List.ALPHABETICAL); girllist.setLowercase(List.LOWERCASE); for ( int j = 0 ; j < 3 ; j++) { ListItem girlitem = new ListItem( "girls" + (j + 1 )); List rolelist = new List(List.UNORDERED); for ( int k = 0 ; k < 2 ; k++) { rolelist.add(String.format( "%s, %s" , const_NEZHA + (k + 1 ), const_BIBIDONG + (k + 1 ))); } girlitem.add(rolelist); girllist.add(girlitem); } item.add(girllist); list.add(item); document.add(list); * 插入锚Anchor, Image, 章节Chapter, 子列表Section public static void createPDFWithExtraContent() throws DocumentException, MalformedURLException, IOException { FileOutputStream out = new FileOutputStream(FILE_DIR + "createPDFWithExtraContent.pdf" ); String content = "you can get anything from : " ; Paragraph paragraph = new Paragraph(content); // 创建一个链接到外部网站的新锚点 // 并将此锚点添加到段落中。 Anchor anchor = new Anchor(const_BAIDU, static_FONT_LINK); anchor.setReference(const_BAIDU_URL); paragraph.add(anchor); document.add(paragraph); // Image对象 Image img = Image.getInstance(const_JPG_NGINX); img.setAlignment(Image.LEFT | Image.TEXTWRAP); img.setBorder(Image.BOX); img.setBorderWidth( 10 ); img.setBorderColor(BaseColor.WHITE); img.scaleToFit( 800 , 50 ); // 大小 img.setRotationDegrees(- 50 ); // 旋转 document.add(img); // 章节Chapter -- 目录 Paragraph id="codetool">
六、更多的Java代码实例
【Java 代码实例 1】java反射三种方法 到此这篇关于Java操作pdf的工具类itext的文章就介绍到这了,更多相关Java pdf的工具类itext内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! 原文链接:https://blog.csdn.net/guorui_java/article/details/124229103
查看更多关于Java操作pdf的工具类itext的处理方法的详细内容... 声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did193374
阅读:17次
|