PDF打印工牌DEMO
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 482 483 484 485 486 487 488 489 490 | package com.pdf.util; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; /** * 生成pdf公共类 * @author ytj * @date 2021/11/21 */ public class PdfUtil { // 定义全局的字体静态变量 private static Font titlefont; private static Font headfont; private static Font keyfont; private static Font textfont; private static Font tablefont; // 最大宽度 private static int maxWidth = 520 ; // 静态代码块 static { try { // 不同字体(这里定义为同一种字体:包含不同字号、不同style) BaseFont bfChinese = BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" , BaseFont.NOT_EMBEDDED); titlefont = new Font(bfChinese, 16 , Font.BOLD); headfont = new Font(bfChinese, 14 , Font.BOLD); keyfont = new Font(bfChinese, 12 , Font.NORMAL); tablefont= new Font(bfChinese, 10 ,Font.BOLD); textfont = new Font(bfChinese, 10 , Font.NORMAL); } catch (Exception e) { e.printStackTrace(); } } /** * 每英寸的像素 */ public static final float DPI = 72f; /** * 每毫米像素(1英寸=25.4毫米) */ public static final float DPMM = DPI / 25 .4f; /** * 打印名单 * */ public static void nameDoublePdf(Document document) throws Exception{ // 段落 Paragraph paragraph = new Paragraph( "PDF工牌打印测试" , headfont); paragraph.setAlignment( 1 ); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft( 12 ); //设置左缩进 paragraph.setIndentationRight( 12 ); //设置右缩进 paragraph.setFirstLineIndent( 24 ); //设置首行缩进 paragraph.setLeading(20f); //行间距 paragraph.setSpacingBefore(5f); //设置段落上空白 paragraph.setSpacingAfter(10f); //设置段落下空白 // 表格 PdfPTable table = createTablesPaent( new float [] { 260 , 260 }); //table.addCell(createCell("第 1 页,共 1 页", keyfont, Element.ALIGN_RIGHT, 4, false)); //table.addCell(createCell("信息", keyfont, Element.ALIGN_LEFT, 2, false)); PdfPTable table1=createTables( new float []{ 50 , 50 , 80 , 90 }); PdfPTable table2=createTables( new float []{ 50 , 50 , 80 , 90 }); Image image = Image.getInstance( "E:/照片.jpg" ); image.setAlignment(Image.ALIGN_CENTER); image.scalePercent( 7 ); //依照比例缩放、 table1.addCell(createCell( "施工人员通行证" , headfont, Element.ALIGN_CENTER, 4 )); // PdfPCell table1.addCell(createImageCell(image, 2 , 2 , 5 )); table1.addCell(createCell( "姓名" , textfont, Element.ALIGN_LEFT, 1 )); table1.addCell(createCell( "张仁" , tablefont, Element.ALIGN_CENTER, 1 )); table1.addCell(createCell( "电话" , textfont, Element.ALIGN_LEFT, 1 )); table1.addCell(createCell( "13679073" , tablefont, Element.ALIGN_CENTER, 1 )); table1.addCell(createCell( "编号" , textfont, Element.ALIGN_LEFT, 1 )); table1.addCell(createCell( "1889" , tablefont, Element.ALIGN_CENTER, 1 )); table1.addCell(createCell( "施工项目" , textfont, Element.ALIGN_LEFT, 1 )); table1.addCell(createCell( "秀(父子)" , tablefont, Element.ALIGN_CENTER, 1 )); table1.addCell(createCell( "负责人电话" , textfont, Element.ALIGN_LEFT, 1 )); table1.addCell(createCell( "153511" , tablefont, Element.ALIGN_CENTER, 1 )); table1.addCell(createCell( "FOEN 奋安" , headfont, Element.ALIGN_CENTER, 4 )); table2.addCell(createCell( "施工人员通行证" , headfont, Element.ALIGN_CENTER, 4 )); table2.addCell(createImageCell(image, 2 , 2 , 5 )); table2.addCell(createCell( "姓名" , textfont, Element.ALIGN_LEFT, 1 )); table2.addCell(createCell( "张继2" , tablefont, Element.ALIGN_CENTER, 1 )); table2.addCell(createCell( "电话" , textfont, Element.ALIGN_LEFT, 1 )); table2.addCell(createCell( "139016073" , tablefont, Element.ALIGN_CENTER, 1 )); table2.addCell(createCell( "编号" , textfont, Element.ALIGN_LEFT, 1 )); table2.addCell(createCell( "1831" , tablefont, Element.ALIGN_CENTER, 1 )); table2.addCell(createCell( "施工项目" , textfont, Element.ALIGN_LEFT, 1 )); table2.addCell(createCell( "李(父子)" , tablefont, Element.ALIGN_CENTER, 1 )); table2.addCell(createCell( "负责人电话" , textfont, Element.ALIGN_LEFT, 1 )); table2.addCell(createCell( "1519549" , tablefont, Element.ALIGN_CENTER, 1 )); table2.addCell(createCell( "FOEN 奋安" , headfont, Element.ALIGN_CENTER, 4 )); // 添加图片//https://img-blog.csdn.net/20180801174617455?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzg0ODcxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 Image image2 = Image.getInstance( "E:/人资部章.gif" ); image2.setAlignment(Image.ALIGN_CENTER); //image.scalePercent(20); //依照比例缩放、 image2.scaleAbsolute( 20 * DPMM, 20 * DPMM); // 设置印章大小为 40mm × 40mm // table1.addCell(image2); PdfPCell cell = new PdfPCell(table1); cell.setVerticalAlignment(Element.ALIGN_LEFT); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setUseBorderPadding( true ); cell.disableBorderSide( 15 ); //去除全部线条 table.addCell(cell); PdfPCell cell1 = new PdfPCell(table2); cell1.setVerticalAlignment(Element.ALIGN_RIGHT); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setUseBorderPadding( true ); cell1.disableBorderSide( 15 ); table.addCell(cell1); document.add(paragraph); document.add(table); //document.add(image2); } /**------------------------创建表格单元格的方法start----------------------------*/ /** * 创建单元格(指定字体) * @param value * @param font * @return */ public PdfPCell createCell(String value, Font font) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置字体位置 cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平..) * @param value * @param font * @param align * @return */ public PdfPCell createCell(String value, Font font, int align) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setMinimumHeight( 20 ); cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并) * @param value * @param font * @param align * @param colspan * @return */ public static PdfPCell createCell(String value, Font font, int align, int colspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setMinimumHeight( 20 ); cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并) * @param value * @param font * @param align * @param colspan * @return */ public PdfPCell createCell(String value,String nextValue, Font font, int align, int colspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setMinimumHeight( 50 ); cell.setRight( 10 ); Phrase phrase= new Phrase(); phrase.add( new Chunk(value,font)); phrase.add(Chunk.NEWLINE); phrase.add(Chunk.NEWLINE); phrase.add( new Chunk(nextValue,font)); cell.setPhrase(phrase); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、单元格跨x行合并) * @param align * @param colspan * @param rowSpan * @return */ public static PdfPCell createImageCell(Image image, int align, int colspan, int rowSpan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setRowspan(rowSpan); cell.setMinimumHeight( 80 ); cell.addElement(image); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、单元格跨x行合并) * @param value * @param font * @param align * @param colspan * @param rowSpan * @return */ public PdfPCell createHeightCell(String value, Font font, int align, int colspan, int rowSpan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setRowspan(rowSpan); cell.setMinimumHeight( 80 ); //设置最小行高 cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、单元格跨x行合并) * @param value * @param font * @param align * @param colspan * @param rowSpan * @return */ public PdfPCell createCell(String value, Font font, int align, int colspan, int rowSpan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setRowspan(rowSpan); cell.setMinimumHeight( 20 ); cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并) * @param value * @param font * @param align * @param colspan * @return */ public PdfPCell createCell(String value, Font font, int align, int colspan, float height) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setMinimumHeight(height); cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、单元格跨x行合并) * @param value * @param font * @param align * @param colspan * @param rowSpan * @return */ public PdfPCell createCell(String value, Font font, int align, int colspan, int rowSpan, float height) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setRowspan(rowSpan); cell.setMinimumHeight(height); cell.setPhrase( new Phrase(value, font)); return cell; } public PdfPCell createCell(){ PdfPCell cell = new PdfPCell(); cell.setBorder( 0 ); //设置表格线宽 cell.setPaddingTop( 15 .0f); //上间距 cell.setPaddingBottom( 8 .0f); //下间距 return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距) * @param value * @return */ public PdfPCell createCell(String value) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setColspan( 7 ); cell.setPhrase( new Phrase(value,textfont )); cell.setBorder( 0 ); cell.setPaddingTop( 1 ); cell.setPaddingBottom( 2 .5F); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距) * @param value * @param font * @param align * @param colspan * @return */ public PdfPCell createTitleCell(String value, Font font, int align, int colspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPhrase( new Phrase(value, font)); cell.setPadding( 3 .0f); cell.setBorder( 0 ); cell.setPaddingTop( 1 .0f); cell.setPaddingBottom(3f); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距) * @param value * @param font * @param align * @param colspan * @param boderFlag * @return */ public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPhrase( new Phrase(value, font)); cell.setPadding( 3 .0f); if (!boderFlag) { cell.setBorder( 0 ); cell.setPaddingTop( 15 .0f); cell.setPaddingBottom( 8 .0f); } else if (boderFlag) { cell.setBorder( 0 ); cell.setPaddingTop( 0 .0f); cell.setPaddingBottom( 15 .0f); } return cell; } /** * 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距) * @param value * @param font * @param align * @param borderWidth * @param paddingSize * @param flag * @return */ public PdfPCell createCell(String value, Font font, int align, float [] borderWidth, float [] paddingSize, boolean flag) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setPhrase( new Phrase(value, font)); cell.setBorderWidthLeft(borderWidth[ 0 ]); cell.setBorderWidthRight(borderWidth[ 1 ]); cell.setBorderWidthTop(borderWidth[ 2 ]); cell.setBorderWidthBottom(borderWidth[ 3 ]); cell.setPaddingTop(paddingSize[ 0 ]); cell.setPaddingBottom(paddingSize[ 1 ]); if (flag) { cell.setColspan( 2 ); } return cell; } /**------------------------创建表格单元格的方法end----------------------------*/ /**--------------------------创建表格的方法start------------------- ---------*/ /** * 创建默认列宽,指定列数、水平(居中、右、左)的表格 * @param colNumber * @param align * @return */ public PdfPTable createTable( int colNumber, int align) { PdfPTable table = new PdfPTable(colNumber); try { table.setTotalWidth(maxWidth); table.setLockedWidth( true ); table.setHorizontalAlignment(align); table.getDefaultCell().setBorder( 1 ); } catch (Exception e) { e.printStackTrace(); } return table; } /** * 创建指定列宽、列数的表格 * @param widths * @return */ public PdfPTable createTable( float [] widths) { PdfPTable table = new PdfPTable(widths); try { table.setTotalWidth(maxWidth); table.setLockedWidth( true ); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder( 1 ); } catch (Exception e) { e.printStackTrace(); } return table; } /** * 创建指定列宽、列数的表格 * @param widths * @return */ public static PdfPTable createTablesPaent( float [] widths) { PdfPTable table = new PdfPTable(widths); try { table.setTotalWidth(maxWidth); table.setLockedWidth( true ); table.setHorizontalAlignment(Element.ALIGN_CENTER); } catch (Exception e) { e.printStackTrace(); } return table; } /** * 创建指定列宽、列数的表格 * @param widths * @return PdfPTable */ public static PdfPTable createTables( float [] widths) { PdfPTable table = new PdfPTable(widths); try { table.setTotalWidth( 230 ); table.setLockedWidth( true ); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder( 1 ); } catch (Exception e) { e.printStackTrace(); } return table; } /** * 创建空白的表格 * @return */ public PdfPTable createBlankTable() { PdfPTable table = new PdfPTable( 1 ); table.getDefaultCell().setBorder( 0 ); table.addCell(createCell( "" , keyfont)); table.setSpacingAfter( 20 .0f); table.setSpacingBefore( 20 .0f); return table; } /**--------------------------创建表格的方法end------------------- ---------*/ } |
1 2 3 4 5 6 7 8 9 10 11 | <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version> 5.5 . 13 </version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version> 5.2 . 0 </version> </dependency> |
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 | package com.pdf.service; import com.itextpdf.text.Document; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfGState; import com.itextpdf.text.pdf.PdfWriter; import com.pdf.util.MyHeaderFooter; import com.pdf.util.PdfUtil; import com.pdf.util.Watermark; import com.pdf.util.WatermarkImage; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author ytj * @date 2021/12/2 */ @Service public class PrintService { public void print(HttpServletResponse response) { try { Document document = new Document(PageSize.A4); // 建立一个Document对象 PdfWriter writer = PdfWriter.getInstance(document,response.getOutputStream()); writer.setPageEvent( new Watermark( "FOEN,FOEN,FOEN,FOEN" )); // 水印 writer.setPageEvent( new MyHeaderFooter()); // 页眉/页脚 document.open(); //打开 document.addTitle( "F工牌打印" ); // 标题 document.addAuthor( "GZH" ); // 作者 document.addSubject( "主题" ); // 主题 document.addKeywords( "Keywords@iTextpdf" ); // 关键字 document.addCreator( "GZH" ); // 创建者 writer.setPageEvent( new WatermarkImage(Image.getInstance( "E:/人资部章.gif" ),writer)); // 水印来解决公章问题 /** // 加入水印 PdfContentByte waterMar = writer.getDirectContentUnder(); // 开始设置水印 waterMar.beginText(); // 设置水印透明度 PdfGState gs = new PdfGState(); // 设置笔触字体不透明度为0.4f gs.setStrokeOpacity(0.95f); try { Image image = Image.getInstance("E:/人资部章.gif"); // 设置坐标 绝对位置 X Y image.setAbsolutePosition(100, 100); // 设置旋转弧度 image.setRotation(0);// 旋转 弧度 // 设置旋转角度 image.setRotationDegrees(0);// 旋转 角度 // 设置等比缩放 //image.scalePercent(90);// 依照比例缩放 image.scaleAbsolute(100,100);//自定义大小 // 设置透明度 waterMar.setGState(gs); // 添加水印图片 waterMar.addImage(image); // 设置透明度 waterMar.setGState(gs); //结束设置 waterMar.endText(); waterMar.stroke(); } catch (IOException e) { e.printStackTrace(); }finally { waterMar = null; gs = null; } **/ new PdfUtil().nameDoublePdf(document); //生成PDF内容 document.close(); //必须要关闭,否则生成的文件打不开 } catch (Exception e) { System.out.println( "创建pdf文件出错:" +e.getMessage()); } } } |
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 | package com.pdf.util; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.IOException; /** * 水印内容(图片或公章) */ public class WatermarkImage extends PdfPageEventHelper { //private static final Log logger = Log.get(); private Image image; //水印内容图片 private PdfWriter writer; @Override public void onEndPage(PdfWriter writer, Document document) { for ( int i= 0 ; i< 1 ; i++) { for ( int j= 0 ; j< 2 ; j++) { // 加入水印 PdfContentByte waterMar = writer.getDirectContentUnder(); // 开始设置水印 waterMar.beginText(); // 设置水印透明度 PdfGState gs = new PdfGState(); // 设置笔触字体不透明度为0.4f gs.setStrokeOpacity( 0 .25f); try { // 设置透明度 waterMar.setGState(gs); // 设置坐标 绝对位置 X Y System.out.println( "writer.getPageNumber():" +writer.getPageNumber()); if ( j % 2 == 1 ){ this .image.setAbsolutePosition( 150 , 650 ); } else { this .image.setAbsolutePosition( 420 , 650 ); } // 设置旋转弧度 this .image.setRotation( 0 ); // 旋转 弧度 // 设置旋转角度 this .image.setRotationDegrees( 0 ); // 旋转 角度 // 设置等比缩放 //image.scalePercent(90);// 依照比例缩放 this .image.scaleAbsolute( 100 , 100 ); //自定义大小 // 添加水印图片 waterMar.addImage( this .image); // 设置透明度 waterMar.setGState(gs); //结束设置 waterMar.endText(); waterMar.stroke(); } catch (DocumentException e) { e.printStackTrace(); } finally { waterMar = null ; gs = null ; } } } } public WatermarkImage(Image image,PdfWriter writer) { this .writer = writer; this .image = image; } } |
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 | package com.pdf.util; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.GrayColor; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfWriter; /** * 水印内容(文本) */ public class Watermark extends PdfPageEventHelper { Font FONT = new Font(Font.FontFamily.HELVETICA, 30 , Font.BOLD, new GrayColor( 0 .95f)); private String waterCont; //水印内容 public Watermark(String waterCont) { this .waterCont = waterCont; } @Override public void onEndPage(PdfWriter writer, Document document) { for ( int i= 0 ; i< 5 ; i++) { for ( int j= 0 ; j< 5 ; j++) { ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_CENTER, new Phrase( this .waterCont == null ? "HELLO WORLD" : this .waterCont, FONT), ( 50 .5f+i* 350 ), ( 40 .0f+j* 150 ), writer.getPageNumber() % 2 == 1 ? 45 : - 45 ); } } } } |
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 | package com.pdf.util; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.IOException; /** * PDF页头页码 */ public class MyHeaderFooter extends PdfPageEventHelper { // 总页数 PdfTemplate totalPage; Font hfFont; { try { hfFont = new Font(BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" , BaseFont.NOT_EMBEDDED), 8 , Font.NORMAL); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 打开文档时,创建一个总页数的模版 public void onOpenDocument(PdfWriter writer, Document document) { PdfContentByte cb =writer.getDirectContent(); totalPage = cb.createTemplate( 30 , 16 ); } // 一页加载完成触发,写入页眉和页脚 public void onEndPage(PdfWriter writer, Document document) { PdfPTable table = new PdfPTable( 3 ); try { table.setTotalWidth(PageSize.A4.getWidth() - 100 ); table.setWidths( new int [] { 24 , 24 , 3 }); table.setLockedWidth( true ); table.getDefaultCell().setFixedHeight(- 10 ); table.getDefaultCell().setBorder(Rectangle.BOTTOM); table.addCell( new Paragraph( "FOEN工牌打印/" , hfFont)); // 可以直接使用addCell(str),不过不能指定字体,中文无法显示 table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell( new Paragraph( "第" + writer.getPageNumber() + "页/" , hfFont)); // 总页数 PdfPCell cell = new PdfPCell(Image.getInstance(totalPage)); cell.setBorder(Rectangle.BOTTOM); table.addCell(cell); // 将页眉写到document中,位置可以指定,指定到下面就是页脚 table.writeSelectedRows( 0 , - 1 , 50 ,PageSize.A4.getHeight() - 20 , writer.getDirectContent()); } catch (Exception de) { throw new ExceptionConverter(de); } } // 全部完成后,将总页数的pdf模版写到指定位置 public void onCloseDocument(PdfWriter writer,Document document) { String text = "总" + (writer.getPageNumber()) + "页" ; ColumnText.showTextAligned(totalPage, Element.ALIGN_LEFT, new Paragraph(text,hfFont), 2 , 2 , 0 ); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2020-01-06 微信小程序var和let以及const的区别: