JAVA生成PDF文件
Posted 一念花开满天下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA生成PDF文件相关的知识,希望对你有一定的参考价值。
生成PDF文件是主要应用的是ITEXT插件
1 import java.awt.Color; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import com.lowagie.text.Cell; 9 import com.lowagie.text.Document; 10 import com.lowagie.text.DocumentException; 11 import com.lowagie.text.Element; 12 import com.lowagie.text.Font; 13 import com.lowagie.text.Image; 14 import com.lowagie.text.PageSize; 15 import com.lowagie.text.Paragraph; 16 import com.lowagie.text.Table; 17 import com.lowagie.text.pdf.BaseFont; 18 import com.lowagie.text.pdf.PdfPCell; 19 import com.lowagie.text.pdf.PdfPTable; 20 import com.lowagie.text.pdf.PdfWriter; 21 22 23 public class MySelf { 24 25 /** 26 * @param args 27 * @throws IOException 28 * @throws DocumentException 29 */ 30 public static void main(String[] args) throws DocumentException, 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<list.size();i++){ 81 Cell cell=null; 82 cell=new Cell(new Paragraph((String) list.get(i),keyfont)); 83 //设置单元格背景颜色 84 cell.setBackgroundColor(Color.lightGray); 85 //设置水平居中 86 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 87 //设置垂直居中 88 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 89 table.addCell(cell); 90 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=new PdfPCell(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=new PdfPCell(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 }
以上是关于JAVA生成PDF文件的主要内容,如果未能解决你的问题,请参考以下文章
java代码编辑器 pdf文件预览 主流SSM 代码生成器 shrio redis websocke