先上效果圖java
這是用IText作的,是一個表格佈局佈局
運行該例須要的資源,須要IText的core包,還有IText的字體包itext-asian在代碼裏面,須要圖片,字體,而後指定一個輸出的路徑,這些我在java文件裏面都有寫字體
而後貼上代碼,有興趣的同窗能夠研究一下ui
1 package com.itext.utils; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.OutputStream; 6 7 import com.itext.demo.PrintPDF2; 8 import com.itextpdf.text.Chunk; 9 import com.itextpdf.text.Document; 10 import com.itextpdf.text.Element; 11 import com.itextpdf.text.Font; 12 import com.itextpdf.text.Image; 13 import com.itextpdf.text.Paragraph; 14 import com.itextpdf.text.Phrase; 15 import com.itextpdf.text.Rectangle; 16 import com.itextpdf.text.pdf.BaseFont; 17 import com.itextpdf.text.pdf.PdfPCell; 18 import com.itextpdf.text.pdf.PdfPTable; 19 import com.itextpdf.text.pdf.PdfWriter; 20 import com.itextpdf.text.pdf.draw.DottedLineSeparator; 21 22 public class PrintPDFUtils { 23 24 // 圖片路徑放在classpath img包下 25 public static String ztoImgPath = "/img/ZTO2.png"; 26 public static String tiaoImgPath = "/img/test.jpg"; 27 public static String ewmImgPath = "/img/EWM.png"; 28 public static String tbImgPath = "/img/TB.jpg"; 29 public static String jiImgPath = "/img/ji.png"; 30 31 // 字體文件,放在classpath fonts包下 32 public static String boldPATH = "/fonts/SourceHanSansK-Bold.ttf"; 33 public static String lightPATH = "/fonts/SourceHanSansK-Light.ttf"; 34 public static String normalPATH = "/fonts/SourceHanSansK-Normal.ttf"; 35 36 // 面單信息 37 public static String receiver = "receiver"; 38 public static String receiverPhone = "receiverPhone"; 39 public static String receiverAddr = "receiverAddr-4"; 40 public static String sender = "sender"; 41 public static String senderPhone = "senderPhone"; 42 public static String senderAddr = "senderAddr 22"; 43 public static String barCode = "barCode"; 44 public static String topCode = "topCode"; 45 public static String topAddr = "topAddr"; 46 public static String printDate = "printDate"; 47 public static String orderCode = "orderCode"; 48 49 50 static { 51 boldPATH = PrintPDFUtils.class.getResource(boldPATH).getFile(); 52 lightPATH = PrintPDFUtils.class.getResource(lightPATH).getFile(); 53 normalPATH = PrintPDFUtils.class.getResource(normalPATH).getFile(); 54 55 ztoImgPath = PrintPDFUtils.class.getResource(ztoImgPath).getFile(); 56 tiaoImgPath = PrintPDFUtils.class.getResource(tiaoImgPath).getFile(); 57 ewmImgPath = PrintPDFUtils.class.getResource(ewmImgPath).getFile(); 58 tbImgPath = PrintPDFUtils.class.getResource(tbImgPath).getFile(); 59 jiImgPath = PrintPDFUtils.class.getResource(jiImgPath).getFile(); 60 } 61 62 public static void printPDF(OutputStream os) { 63 64 try { 65 Rectangle rectangle = new Rectangle(280, 426); 66 Document document = new Document(rectangle, 0, 0, 0, 0); 67 PdfWriter writer = PdfWriter.getInstance(document, os); 68 69 //粗體 70 BaseFont boldFont = BaseFont.createFont(boldPATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 71 Font boldFont12 = new Font(boldFont, 12, Font.NORMAL); 72 Font boldFont16 = new Font(boldFont, 16, Font.NORMAL); 73 Font boldFont10 = new Font(boldFont, 10, Font.NORMAL); 74 Font boldFont8 = new Font(boldFont, 8, Font.NORMAL); 75 //細體 76 BaseFont lightFont = BaseFont.createFont(lightPATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 77 Font lightFont16 = new Font(lightFont, 16, Font.NORMAL); 78 Font lightFont12 = new Font(lightFont, 12, Font.NORMAL); 79 Font lightFont10 = new Font(lightFont, 10, Font.NORMAL); 80 Font lightFont9 = new Font(lightFont, 9, Font.NORMAL); 81 Font lightFont8 = new Font(lightFont, 8, Font.NORMAL); 82 Font lightFont6 = new Font(lightFont, 6, Font.NORMAL); 83 //普通 84 BaseFont normalFont = BaseFont.createFont(normalPATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 85 Font normalFont21 = new Font(normalFont, 21, Font.NORMAL); 86 Font normalFont16 = new Font(normalFont, 16, Font.NORMAL); 87 Font normalFont12 = new Font(normalFont, 12, Font.NORMAL); 88 Font normalFont11 = new Font(normalFont, 11, Font.NORMAL); 89 Font normalFont10 = new Font(normalFont, 10, Font.NORMAL); 90 Font normalFont8 = new Font(normalFont, 8, Font.NORMAL); 91 Font normalFont7 = new Font(normalFont, 7, Font.NORMAL); 92 93 document.open(); 94 // 第一行 95 int[] table1Width = { 30, 40, 30 }; 96 PdfPTable table1 = getPdfPTable(3, table1Width); 97 98 Image table1Img = Image.getInstance(ztoImgPath); 99 100 PdfPCell l1c2 = new PdfPCell(); 101 PdfPCell l1c3 = new PdfPCell(new Phrase("國際快遞", normalFont16)); 102 103 l1c2.setBorder(0); 104 l1c3.setBorder(0); 105 l1c3.setHorizontalAlignment(Element.ALIGN_RIGHT); 106 107 table1.addCell(table1Img); 108 table1.addCell(l1c2); 109 table1.addCell(l1c3); 110 //document.add(table1); 111 112 // 虛線 113 //document.add(getDashLineParagraph()); 114 115 //第二行 116 PdfPTable table2 = getPdfPTable(3, new int[]{20, 60, 20}); 117 118 PdfPCell table2C1 = getPdfPCell(new Phrase(topCode, normalFont21)); 119 table2C1.setHorizontalAlignment(Element.ALIGN_CENTER); 120 table2C1.setVerticalAlignment(Element.ALIGN_CENTER); 121 122 table2.addCell(getPdfPCell()); 123 table2.addCell(table2C1); 124 table2.addCell(getPdfPCell()); 125 //document.add(table2); 126 127 //虛線 128 //document.add(getDashLineParagraph()); 129 130 //第三行 131 PdfPTable table3 = getPdfPTable(4, new int[]{10, 15, 55, 20}); 132 133 //PdfPCell table3C1 = getPdfPCell(new Phrase("集", fontChinese14Bold)); 134 Image table3JIImg = Image.getInstance(jiImgPath); 135 table3JIImg.scalePercent(45); 136 PdfPCell table3C1 = new PdfPCell(table3JIImg); 137 138 PdfPCell table3C2 = getPdfPCell(new Phrase("Exchange Office", lightFont8)); 139 PdfPCell table3C3 = getPdfPCell(new Phrase(topAddr, boldFont16), Element.ALIGN_CENTER); 140 PdfPCell table3C4 = getPdfPCell(); 141 142 table3C1.setBorder(0); 143 table3C1.setPaddingLeft(6); 144 //table3C1.setPaddingBottom(5.5f); 145 table3C1.setVerticalAlignment(Element.ALIGN_CENTER); 146 table3C2.setVerticalAlignment(Element.ALIGN_CENTER); 147 table3C3.setVerticalAlignment(Element.ALIGN_CENTER); 148 149 150 table3.addCell(table3C1); 151 table3.addCell(table3C2); 152 table3.addCell(table3C3); 153 table3.addCell(table3C4); 154 //document.add(table3); 155 156 //虛線 157 //document.add(getDashLineParagraph()); 158 159 //第四、5行 160 PdfPTable table45 = getPdfPTable(3, new int[]{20, 55, 25}); 161 Phrase t45C1P = new Phrase(); 162 t45C1P.add(new Chunk("收件人", normalFont10)); 163 t45C1P.add(Chunk.NEWLINE); 164 t45C1P.add(new Chunk("To/Consignee", normalFont7)); 165 166 PdfPCell table45C1 = getPdfPCell(t45C1P); 167 168 Phrase t45C2P = new Phrase(); 169 t45C2P.add(new Chunk(receiver + " " + receiverPhone, boldFont8)); 170 t45C2P.add(Chunk.NEWLINE); 171 t45C2P.add(new Chunk(receiverAddr, normalFont7)); 172 173 PdfPCell table45C2 = getPdfPCell(t45C2P); 174 175 /*Phrase tableC3P = new Phrase(); 176 tableC3P.add(new Chunk("□ 重量/Weight:", fontChinese8Normal)); 177 tableC3P.add(Chunk.NEWLINE); 178 tableC3P.add(new Chunk("□ 價值/Value:", fontChinese8Normal)); 179 tableC3P.add(Chunk.NEWLINE); 180 tableC3P.add(new Chunk("□ 代收/Payable:", fontChinese8Normal)); 181 tableC3P.add(Chunk.NEWLINE); 182 tableC3P.add(new Chunk("□ 長/L:", fontChinese8Normal)); 183 tableC3P.add(Chunk.NEWLINE); 184 tableC3P.add(new Chunk("□ x寬/W:", fontChinese8Normal)); 185 tableC3P.add(Chunk.NEWLINE); 186 tableC3P.add(new Chunk("□ 高/H:", fontChinese8Normal)); 187 tableC3P.add(Chunk.NEWLINE);*/ 188 //替換爲單列表格 189 PdfPTable table45_Cell3_table = getTable45Cell3(lightFont6); 190 191 PdfPCell table45C3 = new PdfPCell(table45_Cell3_table); 192 193 Phrase t45C4P = new Phrase(); 194 t45C4P.add(new Chunk("寄件人", normalFont10)); 195 t45C4P.add(Chunk.NEWLINE); 196 t45C4P.add(new Chunk("From/Shipper", lightFont6)); 197 t45C4P.add(Chunk.NEWLINE); 198 t45C4P.add(new Chunk("Return Add.", lightFont6)); 199 200 PdfPCell table45C4 = getPdfPCell(t45C4P); 201 202 Phrase t45C5P = new Phrase(); 203 t45C5P.add(new Chunk(sender + " " + senderPhone, boldFont8)); 204 t45C5P.add(Chunk.NEWLINE); 205 t45C5P.add(new Chunk(senderAddr, lightFont6)); 206 207 //table45設置樣式 208 int table45Hight = 35; 209 PdfPCell table45C5 = getPdfPCell(t45C5P); 210 table45C1.setFixedHeight(table45Hight); 211 table45C1.setPadding(5); 212 setBoder(table45C1, null, 1, null, 1); 213 214 table45C2.setPadding(5); 215 table45C2.setFixedHeight(table45Hight); 216 setBoder(table45C2, null, 1, null, 1); 217 218 table45C3.setBorder(0); 219 table45C3.setPadding(1); 220 table45C3.setRowspan(2); 221 table45C3.setFixedHeight(table45Hight); 222 223 table45C4.setPadding(5); 224 table45C4.setFixedHeight(table45Hight); 225 table45C4.setBorderWidthRight(1); 226 227 table45C5.setPadding(5); 228 table45C5.setFixedHeight(table45Hight); 229 table45C5.setBorderWidthRight(1); 230 231 table45.addCell(table45C1); 232 table45.addCell(table45C2); 233 table45.addCell(table45C3); 234 table45.addCell(table45C4); 235 table45.addCell(table45C5); 236 //document.add(table45); 237 //虛線 238 //document.add(getDashLineParagraph()); 239 240 //第6行(條形碼) 241 PdfPTable table6 = getPdfPTable(3, new int[]{15, 70 ,15}); 242 243 Image tiaoImg = Image.getInstance(tiaoImgPath); 244 tiaoImg.scalePercent(70); 245 PdfPCell table6C2 = new PdfPCell(tiaoImg); 246 table6C2.setHorizontalAlignment(Element.ALIGN_CENTER); 247 table6C2.setPadding(3); 248 table6C2.setBorder(0); 249 250 PdfPCell table6C5 = getPdfPCell(new Phrase(barCode, boldFont16)); 251 table6C5.setHorizontalAlignment(Element.ALIGN_CENTER); 252 table6C5.setPadding(3); 253 table6C5.setPaddingTop(-3); 254 255 table6.addCell(getPdfPCell()); 256 table6.addCell(table6C2); 257 table6.addCell(getPdfPCell()); 258 table6.addCell(getPdfPCell()); 259 table6.addCell(table6C5); 260 table6.addCell(getPdfPCell()); 261 //document.add(table6); 262 263 //虛線 264 //document.add(getDashLineParagraph()); 265 266 //第七行 267 PdfPTable table7 = getPdfPTable(3, new int[]{40, 35 ,25}); 268 269 String table7C1_s1 = "快件送達收件人地址,經收件人或收件人(寄件人)容許的代收件人簽字,視爲送達," 270 + "您的簽字表明您已簽收此包裹,並以確認商品信息無誤,包裝無缺,沒有劃痕,破損等方面質量問題。"; 271 Phrase table7C1P = new Phrase(); 272 table7C1P.add(new Chunk(table7C1_s1, lightFont6)); 273 table7C1P.add(Chunk.NEWLINE); 274 table7C1P.add(Chunk.NEWLINE); 275 table7C1P.add(new Chunk(" " + printDate, lightFont6)); 276 277 PdfPCell table7C1 = getPdfPCell(table7C1P); 278 279 Phrase table7C2P = new Phrase(); 280 table7C2P.add(new Chunk("簽收", normalFont8)); 281 table7C2P.add(Chunk.NEWLINE); 282 table7C2P.add(new Chunk("Signature required:", normalFont7)); 283 table7C2P.add(Chunk.NEWLINE); 284 table7C2P.add(Chunk.NEWLINE); 285 table7C2P.add(new Chunk("時間", normalFont8)); 286 table7C2P.add(Chunk.NEWLINE); 287 table7C2P.add(new Chunk("Date:", normalFont7)); 288 289 PdfPCell table7C2 = getPdfPCell(table7C2P); 290 291 Image table7Img = Image.getInstance(ewmImgPath); 292 table7Img.scalePercent(20); 293 PdfPCell table7C3 = new PdfPCell(table7Img); 294 295 //table7 296 int table7Width = 60; 297 table7C1.setPadding(3); 298 table7C1.setFixedHeight(table7Width); 299 setBoder(table7C1, null, 1, null, 1); 300 301 table7C2.setPadding(3); 302 table7C2.setFixedHeight(table7Width); 303 setBoder(table7C2, null, 1, null, 1); 304 305 table7C3.setPadding(2); 306 table7C3.setPaddingLeft(5); 307 table7C3.setPaddingBottom(2); 308 table7C3.setFixedHeight(table7Width); 309 table7C3.setBorder(0); 310 setBoder(table7C3, null, 1, null, null); 311 312 table7.addCell(table7C1); 313 table7.addCell(table7C2); 314 table7.addCell(table7C3); 315 //document.add(table7); 316 317 //第8行 318 PdfPTable table8 = getPdfPTable(2, new int[]{40, 60}); 319 320 Image table8ZTO = Image.getInstance(ztoImgPath); 321 table8ZTO.scalePercent(38); 322 PdfPCell table8C1 = new PdfPCell(table8ZTO); 323 324 Image table8Img = Image.getInstance(tiaoImgPath); 325 table8Img.scalePercent(70); 326 PdfPCell table8C2 = new PdfPCell(table8Img); 327 PdfPCell table8C4 = getPdfPCell(new Phrase(barCode, boldFont10)); 328 329 //table8樣式 330 int table8Hight = 50; 331 table8C1.setPadding(1); 332 table8C1.setPaddingTop(11f); 333 table8C1.setPaddingLeft(5); 334 table8C1.setBorder(0); 335 table8C1.setRowspan(2); 336 table8C1.setVerticalAlignment(Element.ALIGN_CENTER); 337 338 339 table8C2.setPadding(1); 340 table8C2.setBorder(0); 341 table8C2.setHorizontalAlignment(Element.ALIGN_CENTER); 342 343 table8C4.setHorizontalAlignment(Element.ALIGN_CENTER); 344 345 table8.addCell(table8C1); 346 table8.addCell(table8C2); 347 table8.addCell(table8C4); 348 349 //第9行 350 PdfPTable table9 = getPdfPTable(2, new int[]{50, 50}); 351 352 Phrase table9C1P = new Phrase(); 353 table9C1P.add(new Chunk("收件人信息/To:", normalFont8)); 354 table9C1P.add(Chunk.NEWLINE); 355 table9C1P.add(new Chunk(receiver + " " + receiverPhone, normalFont7)); 356 table9C1P.add(Chunk.NEWLINE); 357 table9C1P.add(new Chunk(receiverAddr, normalFont7)); 358 359 PdfPCell table9C1 = getPdfPCell(table9C1P); 360 table9C1.setPadding(2); 361 setBoder(table9C1, null, null, null, 1); 362 363 364 Phrase table9C2P = new Phrase(); 365 table9C2P.add(new Chunk("寄件人/From:", normalFont8)); 366 table9C2P.add(Chunk.NEWLINE); 367 table9C2P.add(new Chunk(sender + " " + senderPhone, normalFont7)); 368 table9C2P.add(Chunk.NEWLINE); 369 table9C2P.add(new Chunk(senderAddr, normalFont7)); 370 371 PdfPCell table9C2 = getPdfPCell(table9C2P); 372 table9C2.setPadding(2); 373 374 table9.addCell(table9C1); 375 table9.addCell(table9C2); 376 //document.add(table9); 377 378 //虛線 379 //document.add(getDashLineParagraph()); 380 381 //第10行 382 PdfPTable table10 = getPdfPTable(2, new int[]{70, 30}); 383 384 PdfPCell table10C1 = getPdfPCell(new Phrase("內件描述/Description of Contents:", normalFont8)); 385 PdfPCell table10C2 = getPdfPCell(new Phrase("已驗視/Visual inspection", normalFont8)); 386 387 table10C1.setPadding(1); 388 table10C2.setPadding(1); 389 390 table10.addCell(table10C1); 391 table10.addCell(getPdfPCell()); 392 table10.addCell(getPdfPCell()); 393 table10.addCell(table10C2); 394 //document.add(table10); 395 396 //虛線 397 //document.add(getDashLineParagraph()); 398 399 //第11行 400 float table11Height = 22f; 401 PdfPTable table11 = getPdfPTable(3, new int[]{30, 60, 10}); 402 403 Image table11Img = Image.getInstance(tbImgPath); 404 table11Img.scaleToFit(80, 25); 405 PdfPCell table11C1 = new PdfPCell(table11Img); 406 PdfPCell table11C2 = getPdfPCell(new Phrase(orderCode, boldFont12)); 407 408 table11C1.setPadding(2); 409 table11C1.setBorder(0); 410 table11C1.setFixedHeight(table11Height); 411 412 table11C2.setPadding(2); 413 table11C2.setFixedHeight(table11Height); 414 415 table11.addCell(table11C1); 416 table11.addCell(table11C2); 417 table11.addCell(getPdfPCell()); 418 //document.add(table11); 419 420 //建立totalTable 421 PdfPTable totalTable = new PdfPTable(1); 422 totalTable.setWidthPercentage(100); 423 424 PdfPCell tableCell1 = getPdfPTableCell(table1); 425 PdfPCell tableCell2 = getPdfPTableCell(table2); 426 PdfPCell tableCell3 = getPdfPTableCell(table3); 427 PdfPCell tableCell45 = getPdfPTableCell(table45); 428 PdfPCell tableCell6 = getPdfPTableCell(table6); 429 PdfPCell tableCell7 = getPdfPTableCell(table7); 430 PdfPCell tableCell8 = getPdfPTableCell(table8); 431 PdfPCell tableCell9 = getPdfPTableCell(table9); 432 PdfPCell tableCell10 = getPdfPTableCell(table10); 433 PdfPCell tableCell11 = getPdfPTableCell(table11); 434 435 setBoder(tableCell1, 2, null, 2, 2); 436 setBoder(tableCell2, null, null, 2, 2); 437 setBoder(tableCell3, null, null, 2, 2); 438 setBoder(tableCell45, null, null, 2, 2); 439 setBoder(tableCell6, null, null, 2, 2); 440 setBoder(tableCell7, null, null, 2, 2); 441 setBoder(tableCell8, null, null, 2, 2); 442 setBoder(tableCell9, null, null, 2, 2); 443 setBoder(tableCell10, null, null, 2, 2); 444 setBoder(tableCell11, null, 2, 2, 2); 445 446 totalTable.addCell(tableCell1); 447 totalTable.addCell(getDashLineInCell()); 448 totalTable.addCell(tableCell2); 449 totalTable.addCell(getDashLineInCell()); 450 totalTable.addCell(tableCell3); 451 totalTable.addCell(getDashLineInCell()); 452 totalTable.addCell(tableCell45); 453 totalTable.addCell(getDashLineInCell()); 454 totalTable.addCell(tableCell6); 455 totalTable.addCell(getDashLineInCell()); 456 totalTable.addCell(tableCell7); 457 totalTable.addCell(tableCell8); 458 totalTable.addCell(getDashLineInCell()); 459 totalTable.addCell(tableCell9); 460 totalTable.addCell(getDashLineInCell()); 461 totalTable.addCell(tableCell10); 462 totalTable.addCell(getDashLineInCell()); 463 totalTable.addCell(tableCell11); 464 465 document.add(totalTable); 466 document.close(); 467 } catch (Exception e) { 468 System.out.println("生成失敗!"); 469 e.printStackTrace(); 470 471 if (os != null) { 472 try { 473 os.close(); 474 } catch (IOException e1) { 475 e1.printStackTrace(); 476 } 477 } 478 } 479 } 480 481 public static PdfPTable getTable45Cell3(Font font) { 482 PdfPTable table45_Cell3_table = new PdfPTable(1); 483 484 PdfPCell table45_Cell3_table_C1 = getPdfPCell(new Phrase("□ 重量/Weight:", font), Element.ALIGN_LEFT); 485 PdfPCell table45_Cell3_table_C2 = getPdfPCell(new Phrase("□ 價值/Value:", font), Element.ALIGN_LEFT); 486 PdfPCell table45_Cell3_table_C3 = getPdfPCell(new Phrase("□ 重量/Weight:", font), Element.ALIGN_LEFT); 487 PdfPCell table45_Cell3_table_C4 = getPdfPCell(new Phrase("□ 代收/Payable:", font), Element.ALIGN_LEFT); 488 PdfPCell table45_Cell3_table_C5 = getPdfPCell(new Phrase("□ 長/L:", font), Element.ALIGN_LEFT); 489 PdfPCell table45_Cell3_table_C6 = getPdfPCell(new Phrase("□ 寬/W:", font), Element.ALIGN_LEFT); 490 PdfPCell table45_Cell3_table_C7 = getPdfPCell(new Phrase("□ 高/H:", font), Element.ALIGN_LEFT); 491 492 table45_Cell3_table_C1.setPadding(2); 493 table45_Cell3_table_C2.setPadding(2); 494 table45_Cell3_table_C3.setPadding(2); 495 table45_Cell3_table_C4.setPadding(2); 496 table45_Cell3_table_C5.setPadding(2); 497 table45_Cell3_table_C6.setPadding(2); 498 table45_Cell3_table_C7.setPadding(2); 499 500 table45_Cell3_table_C1.setPaddingBottom(1); 501 table45_Cell3_table_C2.setPaddingBottom(1); 502 table45_Cell3_table_C3.setPaddingBottom(1); 503 table45_Cell3_table_C4.setPaddingBottom(1); 504 table45_Cell3_table_C5.setPaddingBottom(1); 505 table45_Cell3_table_C6.setPaddingBottom(1); 506 table45_Cell3_table_C7.setPaddingBottom(1); 507 508 table45_Cell3_table.addCell(table45_Cell3_table_C1); 509 table45_Cell3_table.addCell(table45_Cell3_table_C2); 510 table45_Cell3_table.addCell(table45_Cell3_table_C3); 511 table45_Cell3_table.addCell(table45_Cell3_table_C4); 512 table45_Cell3_table.addCell(table45_Cell3_table_C5); 513 table45_Cell3_table.addCell(table45_Cell3_table_C6); 514 table45_Cell3_table.addCell(table45_Cell3_table_C7); 515 return table45_Cell3_table; 516 } 517 518 public static Paragraph getDashLineParagraph() { 519 Paragraph p2 = new Paragraph(); 520 p2.add(new Chunk(new DottedLineSeparator())); 521 p2.setSpacingBefore(-15); 522 // p2.setSpacingAfter(-6); 523 return p2; 524 } 525 526 public static PdfPCell getDashLineInCell() { 527 Phrase phrase = new Phrase(); 528 DottedLineSeparator lineSeparator = new DottedLineSeparator(); 529 lineSeparator.setPercentage(98); 530 phrase.add(new Chunk(lineSeparator)); 531 532 PdfPCell pdfPCell = new PdfPCell(phrase); 533 pdfPCell.setBorder(0); 534 pdfPCell.setVerticalAlignment(Element.ALIGN_CENTER); 535 pdfPCell.setPaddingTop(-10); 536 // pdfPCell.setFixedHeight(10); 537 setBoder(pdfPCell, null, null, 2, 2); 538 return pdfPCell; 539 } 540 541 public static PdfPTable getPdfPTable(int column, int[] tableWidth) throws Exception { 542 PdfPTable table = new PdfPTable(column); 543 table.setWidths(tableWidth); 544 table.getDefaultCell().setBorder(0); 545 table.setWidthPercentage(100); 546 return table; 547 } 548 549 public static PdfPCell getPdfPCell() { 550 return getPdfPCell(null, null); 551 } 552 553 public static PdfPCell getPdfPCell(Phrase phrase) { 554 return getPdfPCell(phrase, null); 555 } 556 557 public static PdfPCell getPdfPCell(Phrase phrase, Integer horizontalAlignment) { 558 PdfPCell cell = null; 559 if (phrase != null) { 560 cell = new PdfPCell(phrase); 561 } else { 562 cell = new PdfPCell(); 563 } 564 if (horizontalAlignment != null) { 565 cell.setHorizontalAlignment(horizontalAlignment); 566 } 567 cell.setBorder(0); 568 return cell; 569 } 570 571 public static void setBoder(PdfPCell pdfPCell, Integer top, Integer bottom, Integer left, Integer right) { 572 if (top != null) { 573 pdfPCell.setBorderWidthTop(top); 574 } 575 576 if (bottom != null) { 577 pdfPCell.setBorderWidthBottom(bottom); 578 } 579 580 if (left != null) { 581 pdfPCell.setBorderWidthLeft(left); 582 } 583 584 if (right != null) { 585 pdfPCell.setBorderWidthRight(right); 586 } 587 } 588 589 public static PdfPCell getPdfPTableCell(PdfPTable pdfPTable) throws Exception { 590 if (pdfPTable == null) { 591 throw new Exception("pdfPTable不能爲空"); 592 } 593 PdfPCell pdfPCell = new PdfPCell(); 594 pdfPCell.addElement(pdfPTable); 595 pdfPCell.setBorder(0); 596 pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER); 597 return pdfPCell; 598 } 599 600 }最後是資源壓縮包https://files.cnblogs.com/files/blog-bo/printPdf.zip