1 importjava.awt.Color;2 importjava.io.File;3 importjava.io.FileOutputStream;4 importjava.io.IOException;5 importjava.util.ArrayList;6 importjava.util.List;7
8 importcom.lowagie.text.Cell;9 importcom.lowagie.text.Document;10 importcom.lowagie.text.DocumentException;11 importcom.lowagie.text.Element;12 importcom.lowagie.text.Font;13 importcom.lowagie.text.Image;14 importcom.lowagie.text.PageSize;15 importcom.lowagie.text.Paragraph;16 importcom.lowagie.text.Table;17 importcom.lowagie.text.pdf.BaseFont;18 importcom.lowagie.text.pdf.PdfPCell;19 importcom.lowagie.text.pdf.PdfPTable;20 importcom.lowagie.text.pdf.PdfWriter;21
22
23 public classMySelf {24
25 /**
26 *@paramargs27 *@throwsIOException28 *@throwsDocumentException29 */
30 public static void main(String[] args) throwsDocumentException, IOException {31 //TODO Auto-generated method stub
32 float head= (float)50.0;33 float lineHeight1 = (float)50.0;34 float lineHeight2 = (float)50.0;35 float lineHeight3 = (float)51.0;36 //创建文档,设置页面大小, 左、右、上和下页边距。
37 Document document = new Document(PageSize.A4, 10, 10, 50, 50);38 //处理中文显示问题,使用计算机自带字体
39 BaseFont bfChinese=BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);40 //方法二:使用iTextAsian.jar中的字体41 //BaseFont baseFont = BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);42
43 //方法三:使用资源字体(ClassPath)
44 BaseFont baseFont = BaseFont.createFont("/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
45
46 Font headfont=new Font(bfChinese,10,Font.BOLD);//设置字体大小 样式
47 Font keyfont=new Font(bfChinese,12,Font.BOLD);//文字加粗
48 Font title=new Font(bfChinese,18,Font.BOLD);//文字加粗
49 Font textfont=new Font(bfChinese,16,Font.NORMAL);//正常文字50
51 //document是创建的文档,FileOutputStream是向文档中输入
52 PdfWriter.getInstance(document, new FileOutputStream(new File("D:\\MySelf.pdf")));53 //打开文档
54 document.open();55 //开始生成一个2列的表格
56 PdfPTable table=new PdfPTable(2);57 PdfPTable table1=new PdfPTable(6);58 //定义每个单元格的宽度
59 float[] widthsHeader={20f,5f};60 float[] widthsHeade1={3f,3f,3f,4f,3f,6f};61 //设置表格每一各的宽度
62 table.setWidths(widthsHeader);63 table1.setWidths(widthsHeade1);64 //设置边距65 //设置单元格间距66 //table.setSpacing(1f);67 //设置表格的总体宽度
68 table.setWidthPercentage(100);69
70
71 //如果表格格式一样采用遍历的方式读取添加
72 /*List list=new ArrayList();73 list.add("姓名");74 list.add("姓名");75 list.add("姓名");76 list.add("姓名");77 list.add("姓名");78 list.add("姓名");79 list.add("姓名");80 for(int i=0;i
91 }92 document.add(table);93 document.close();94 System.out.println("表格创建成功");95 return;*/
96
97 //单元格对象
98 PdfPCell cell=null;99 //设置单元格内容
100 cell=new PdfPCell(new Paragraph("文档标题栏",title));101 //合并单元格列
102 cell.setColspan(2);103 //设置垂直居中
104 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);105 //设置水平居中
106 cell.setHorizontalAlignment(Element.ALIGN_CENTER);107 cell.setFixedHeight(head);108 //将单元格内容添加到表格中去
109 table.addCell(cell);110
111
112
113 cell=new PdfPCell(new Paragraph("A",keyfont));114 //设置单元格背景颜色
115 cell.setBackgroundColor(Color.lightGray);116 //设置水平居中
117 cell.setHorizontalAlignment(Element.ALIGN_CENTER);118 //设置垂直居中
119 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);120 cell.setFixedHeight(lineHeight1);121 table1.addCell(cell);122
123 cell=new PdfPCell(new Paragraph("B",keyfont));124 cell.setHorizontalAlignment(Element.ALIGN_CENTER);125 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);126 cell.setFixedHeight(lineHeight1);127 table1.addCell(cell);128 cell=new PdfPCell(new Paragraph("C",keyfont));129 cell.setBackgroundColor(Color.lightGray);130 cell.setHorizontalAlignment(Element.ALIGN_CENTER);131 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);132 cell.setFixedHeight(lineHeight1);133 table1.addCell(cell);134 cell=new PdfPCell(new Paragraph("D",keyfont));135 cell.setHorizontalAlignment(Element.ALIGN_CENTER);136 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);137 cell.setFixedHeight(lineHeight1);138 table1.addCell(cell);139 cell=new PdfPCell(new Paragraph("E",keyfont));140 cell.setBackgroundColor(Color.lightGray);141 cell.setHorizontalAlignment(Element.ALIGN_CENTER);142 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);143 cell.setFixedHeight(lineHeight1);144 table1.addCell(cell);145 cell=new PdfPCell(new Paragraph("F",keyfont));146 cell.setHorizontalAlignment(Element.ALIGN_CENTER);147 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);148 cell.setFixedHeight(lineHeight1);149 table1.addCell(cell);150
151 cell=new PdfPCell(new Paragraph("G",keyfont));152 cell.setBackgroundColor(Color.lightGray);153 cell.setHorizontalAlignment(Element.ALIGN_CENTER);154 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);155 cell.setFixedHeight(lineHeight2);156 table1.addCell(cell);157 cell=new PdfPCell(new Paragraph("H",keyfont));158 cell.setHorizontalAlignment(Element.ALIGN_CENTER);159 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);160 cell.setFixedHeight(lineHeight2);161 table1.addCell(cell);162 cell=new PdfPCell(new Paragraph("I",keyfont));163 cell.setBackgroundColor(Color.lightGray);164 cell.setHorizontalAlignment(Element.ALIGN_CENTER);165 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);166 cell.setFixedHeight(lineHeight2);167 table1.addCell(cell);168 cell=new PdfPCell(new Paragraph("J",keyfont));169 cell.setHorizontalAlignment(Element.ALIGN_CENTER);170 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);171 cell.setFixedHeight(lineHeight2);172 table1.addCell(cell);173 cell=new PdfPCell(new Paragraph("K",keyfont));174 cell.setBackgroundColor(Color.lightGray);175 cell.setHorizontalAlignment(Element.ALIGN_CENTER);176 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);177 cell.setFixedHeight(lineHeight2);178 table1.addCell(cell);179 cell=new PdfPCell(new Paragraph("L",keyfont));180 cell.setHorizontalAlignment(Element.ALIGN_CENTER);181 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);182 cell.setFixedHeight(lineHeight2);183 table1.addCell(cell);184
185 cell=new PdfPCell(new Paragraph("M",keyfont));186 cell.setBackgroundColor(Color.lightGray);187 cell.setHorizontalAlignment(Element.ALIGN_CENTER);188 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);189 cell.setFixedHeight(lineHeight3);190 table1.addCell(cell);191 cell=new PdfPCell(new Paragraph("N",keyfont));192 cell.setHorizontalAlignment(Element.ALIGN_CENTER);193 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);194 cell.setFixedHeight(lineHeight3);195 table1.addCell(cell);196 cell=new PdfPCell(new Paragraph("O",keyfont));197 cell.setBackgroundColor(Color.lightGray);198 cell.setHorizontalAlignment(Element.ALIGN_CENTER);199 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);200 cell.setFixedHeight(lineHeight3);201 table1.addCell(cell);202 cell=new PdfPCell(new Paragraph("P",keyfont));203 cell.setHorizontalAlignment(Element.ALIGN_CENTER);204 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);205 cell.setFixedHeight(lineHeight3);206 table1.addCell(cell);207 cell=new PdfPCell(new Paragraph("Q",keyfont));208 cell.setBackgroundColor(Color.lightGray);209 cell.setHorizontalAlignment(Element.ALIGN_CENTER);210 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);211 cell.setFixedHeight(lineHeight3);212 table1.addCell(cell);213 cell=new PdfPCell(new Paragraph("R",keyfont));214 cell.setHorizontalAlignment(Element.ALIGN_CENTER);215 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);216 cell.setFixedHeight(lineHeight3);217 table1.addCell(cell);218
219 PdfPCell pdfpcell=newPdfPCell(table1);220 pdfpcell.setPadding(0);221 table.addCell(pdfpcell);222 //加载图片添加到指定位置
223 Image image = Image.getInstance ("C:/Users/admin/Desktop/日志文件/IMG_0696.JPG");224 float height=image.height();225 float width=image.width();226 System.out.println(height);227 System.out.println(width);228
229 cell=newPdfPCell(image);230 cell.setHorizontalAlignment(Element.ALIGN_CENTER);231 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);232 cell.setFixedHeight(lineHeight1+lineHeight2+lineHeight3);233 table.addCell(cell);234
235 document.add(table);236 document.close();237
238
239 }240
241 }242
243 生成PDF代码