先上效果图

这是用IText做的,是一个表格布局

运行该例需要的资源,需要IText的core包,还有IText的字体包itext-asian在代码里面,需要图片,字体,然后指定一个输出的路径,这些我在java文件里面都有写

然后贴上代码,有兴趣的同学可以研究一下

1 packagecom.itext.utils;2
3 importjava.io.FileOutputStream;4 importjava.io.IOException;5 importjava.io.OutputStream;6
7 importcom.itext.demo.PrintPDF2;8 importcom.itextpdf.text.Chunk;9 importcom.itextpdf.text.Document;10 importcom.itextpdf.text.Element;11 importcom.itextpdf.text.Font;12 importcom.itextpdf.text.Image;13 importcom.itextpdf.text.Paragraph;14 importcom.itextpdf.text.Phrase;15 importcom.itextpdf.text.Rectangle;16 importcom.itextpdf.text.pdf.BaseFont;17 importcom.itextpdf.text.pdf.PdfPCell;18 importcom.itextpdf.text.pdf.PdfPTable;19 importcom.itextpdf.text.pdf.PdfWriter;20 importcom.itextpdf.text.pdf.draw.DottedLineSeparator;21
22 public classPrintPDFUtils {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 voidprintPDF(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 = newPdfPCell();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(newPhrase(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 = newPdfPCell(table3JIImg);137
138             PdfPCell table3C2 = getPdfPCell(new Phrase("Exchange Office", lightFont8));139             PdfPCell table3C3 = getPdfPCell(newPhrase(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             //第4、5行
160             PdfPTable table45 = getPdfPTable(3, new int[]{20, 55, 25});161             Phrase t45C1P = newPhrase();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 = newPhrase();169             t45C2P.add(new Chunk(receiver + " " +receiverPhone, boldFont8));170 t45C2P.add(Chunk.NEWLINE);171             t45C2P.add(newChunk(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 = newPdfPCell(table45_Cell3_table);192
193             Phrase t45C4P = newPhrase();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 = newPhrase();203             t45C5P.add(new Chunk(sender + " " +senderPhone, boldFont8));204 t45C5P.add(Chunk.NEWLINE);205             t45C5P.add(newChunk(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 = newPdfPCell(tiaoImg);246 table6C2.setHorizontalAlignment(Element.ALIGN_CENTER);247             table6C2.setPadding(3);248             table6C2.setBorder(0);249
250             PdfPCell table6C5 = getPdfPCell(newPhrase(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 = newPhrase();272             table7C1P.add(newChunk(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 = newPhrase();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 = newPdfPCell(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 = newPdfPCell(table8ZTO);323
324             Image table8Img =Image.getInstance(tiaoImgPath);325             table8Img.scalePercent(70);326             PdfPCell table8C2 = newPdfPCell(table8Img);327             PdfPCell table8C4 = getPdfPCell(newPhrase(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 = newPhrase();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(newChunk(receiverAddr, normalFont7));358
359             PdfPCell table9C1 =getPdfPCell(table9C1P);360             table9C1.setPadding(2);361             setBoder(table9C1, null, null, null, 1);362
363
364             Phrase table9C2P = newPhrase();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(newChunk(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 = newPdfPCell(table11Img);406             PdfPCell table11C2 = getPdfPCell(newPhrase(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 staticPdfPTable 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         returntable45_Cell3_table;516 }517
518     public staticParagraph getDashLineParagraph() {519         Paragraph p2 = newParagraph();520         p2.add(new Chunk(newDottedLineSeparator()));521         p2.setSpacingBefore(-15);522         //p2.setSpacingAfter(-6);
523         returnp2;524 }525
526     public staticPdfPCell getDashLineInCell() {527         Phrase phrase = newPhrase();528         DottedLineSeparator lineSeparator = newDottedLineSeparator();529         lineSeparator.setPercentage(98);530         phrase.add(newChunk(lineSeparator));531
532         PdfPCell pdfPCell = newPdfPCell(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         returnpdfPCell;539 }540
541     public static PdfPTable getPdfPTable(int column, int[] tableWidth) throwsException {542         PdfPTable table = newPdfPTable(column);543 table.setWidths(tableWidth);544         table.getDefaultCell().setBorder(0);545         table.setWidthPercentage(100);546         returntable;547 }548
549     public staticPdfPCell getPdfPCell() {550         return getPdfPCell(null, null);551 }552
553     public staticPdfPCell getPdfPCell(Phrase phrase) {554         return getPdfPCell(phrase, null);555 }556
557     public staticPdfPCell getPdfPCell(Phrase phrase, Integer horizontalAlignment) {558         PdfPCell cell = null;559         if (phrase != null) {560             cell = newPdfPCell(phrase);561         } else{562             cell = newPdfPCell();563 }564         if (horizontalAlignment != null) {565 cell.setHorizontalAlignment(horizontalAlignment);566 }567         cell.setBorder(0);568         returncell;569 }570
571     public static voidsetBoder(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) throwsException {590         if (pdfPTable == null) {591             throw new Exception("pdfPTable不能为空");592 }593         PdfPCell pdfPCell = newPdfPCell();594 pdfPCell.addElement(pdfPTable);595         pdfPCell.setBorder(0);596 pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);597         returnpdfPCell;598 }599
600 }最后是资源压缩包https://files.cnblogs.com/files/blog-bo/printPdf.zip

转载于:https://www.cnblogs.com/blog-bo/p/9185972.html

使用IText画复杂布局的页面相关推荐

  1. SharePoint 2013 入门教程之创建页面布局及页面

    原文:SharePoint 2013 入门教程之创建页面布局及页面 在SharePoint的使用过程中,页面布局和页面时很重要的两个概念,主要用于数据个性化展示,下面,我们简单介绍一下SharePoi ...

  2. 百分比布局Design页面无法显示 :The following classes could not be found 解决方法

    关于百分比布局Design页面 :The following classes could not be found: android.percentlayout.widget.PercentRelat ...

  3. 仿优酷视频焦点轮播图布局html页面前端源码

    大家好,今天给大家介绍一款,仿优酷视频焦点轮播图布局html页面前端源码(图1).送给大家哦,获取方式在本文末尾. 图1 可以用菜单控制切换(图2) 图2 整体布局简洁明了,干净简单(图3) 图3 代 ...

  4. linux内存布局及页面映射

    在Linux系统中,以32bit x86系统来说,进程的4GB内存空间(虚拟地址空间)被划分成为两个部分 ------用户空间和内核空间,大小分别为0-3G,3-4G. 用户进程通常情况下,只能访问用 ...

  5. 大家一起来博皮——2:液态布局和固态布局,页面框架篇

    大家一起来博皮 虽然博客园的皮肤很多,而且很漂亮.但是那些自己想更"个性化"自己博客皮肤的朋友,对博客园的皮肤模板还是颇多不满,认为皮肤的结构过于混乱,css样式难以掌控.针对这种 ...

  6. Android:安卓布局分类及布局和页面的关系

    布局有多少种? 其实安卓有六大基本布局 View 被 ViewGroup继承,View是组件 View:是绘制在屏幕上的用户能与之交互的一个对象 ViewGroup:是一个用于存放其他view(和vi ...

  7. 页面布局_word页面布局

    右上角蓝色字体"古墓office"可以关注我:) 之前写了很多期excel的,哈哈,有小伙伴想要看word的了.那就来吧^^ 今天复刻一份在前司工作的时候做的一个系列报告模板吧.选 ...

  8. 安卓App的启动过程、安卓布局分类及布局和页面的关系

    Android App 启动流程: 当你想要启动一个app时,首先得点击该app桌面图标.那点击图标时到底发生了什么呢?先看个理论知识 .Launcher: launcher其实就是一个app,从功能 ...

  9. DIV CSS布局-固定页面开度布局

    DIV CSS布局中主要CSS属性介绍: Float: Float属性是DIV CSS布局中最基本也是最常用的属性,用于实现多列功能,我们知道<div>标签默认一行只能显示一个,而使用Fl ...

最新文章

  1. SAP Spartacus 支持 Vue 吗?
  2. LeetCode 面试题55 二叉树的深度
  3. 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(2)
  4. SQL service
  5. 电商大促特辑:蘑菇街致美丽新世界的架构礼
  6. kubernetes视频教程笔记 (15)-RC、RS和Deployment的关联
  7. 数据库系统概论(第五版)概念大全 —— 第一章
  8. 服务性能测试和内存监控
  9. CDRX -- LTE连接态下的DRX
  10. HexoNext添加网易云音乐
  11. 一款基于 Spring Boot 的公众号管理系统,已开源,别再自己写了!
  12. vue如何制作自定义表单校验样式
  13. SDM(Supervised Descent Method)代码实现在Windows下的配置与使用
  14. 关于谷歌浏览器的自动填充密码
  15. 蓝牙耳机哪款性价比高?2023年高性价比蓝牙耳机盘点
  16. 常用收发邮件服务器地址
  17. 打造数字生活新体验,华为终端云服务在盘算什么?
  18. 【linux驱动之字符设备驱动基础】
  19. element表格里面放图片_elementUI 表格中预览图片
  20. 新南威语言班C加,2020年新南威尔士大学语言班要求高不高

热门文章

  1. 【JDK7】新特性(4) NIO2.0 文件系统
  2. RGB与Lab颜色空间互相转换
  3. 强化学习笔记:多臂老虎机问题(2)--Python仿真
  4. 【图像处理基础】BMP格式图片的解析
  5. mysql group by 和 having 使用注意事项
  6. 【项目--Hi3559A】Hi3559A平台仿真功能操作介绍
  7. 2022年安全员-B证试题及模拟考试
  8. Proteus C51仿真学习板0——KeilProteus
  9. HTML实体字符、ASCII码、URLEncoder、Base64、MD5
  10. 【原】Linux shell script 21是什么意思