maven依赖
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.6</version></dependency><dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.6</version></dependency>
private void create() throws Exception {// 创建一个文档(默认大小A4,边距36, 36, 36, 36)Document document = new Document(PageSize.A4,10,10,50,50);// 设置pdf生成的路径FileOutputStream fileOutputStream= new FileOutputStream("D:/蒹葭.pdf");// 创建writer,通过writer将文档写入磁盘PdfWriter writer = PdfWriter.getInstance(document,fileOutputStream);// 定义字体FontFactoryImp ffi = new FontFactoryImp();// 注册全部默认字体目录,windows会自动找fonts文件夹的,返回值为注册到了多少字体ffi.registerDirectories();// 获取字体,其实不用这么麻烦,后面有简单方法Font font = ffi.getFont("宋体", BaseFont.IDENTITY_H,BaseFont.EMBEDDED, 12, Font.UNDEFINED, null);// 打开文档,只有打开后才能往里面加东西document.open();// 创建表格,5列的表格PdfPTable table = new PdfPTable(4);table.setTotalWidth(PageSize.A4.getWidth()- 100);table.setLockedWidth(true);// 创建头PdfPHeaderCell header = new PdfPHeaderCell();header.setColspan(4);table.addCell(header);//一个cell 代表一个单元格PdfPCell cell = new PdfPCell(new Phrase("蒹葭", font));// 设置可以居中cell.setUseAscender(true);// 设置水平居中cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);// 设置垂直居中cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格背景为黑色BaseColor grey = new BaseColor(204, 204, 204);cell.setBackgroundColor(grey);cell.setColspan(4);table.addCell(cell);// 添加内容溯洄从之,道阻且长。溯游从之,宛在水中央// 蒹葭萋萋,白露未晞。所谓伊人,在水之湄。//溯洄从之,道阻且跻。溯游从之,宛在水中坻。//蒹葭采采,白露未已。所谓伊人,在水之涘。//溯洄从之,道阻且右。溯游从之,宛在水中沚。tableColumn4(font,table,grey,"蒹葭苍苍","白露为霜","所谓伊人","在水一方");tableColumn4(font,table,grey,"溯游从之","道阻且长","溯游从之","宛在水中央");tableColumn4(font,table,grey,"蒹葭萋萋","白露未晞","所谓伊人","在水之湄");tableColumn4(font,table,grey,"溯洄从之","道阻且跻","溯游从之","宛在水中坻");tableColumn4(font,table,grey,"蒹葭采采","白露未已","所谓伊人","在水之涘");tableColumn4(font,table,grey,"溯洄从之","道阻且右","溯游从之","宛在水中沚");// cell = new PdfPCell(new Phrase(FileConstant.TRACK_MAP, font));String url ="图片路径";Image instance = Image.getInstance(url);cell.setUseAscender(true);cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);cell.setBackgroundColor(grey);cell.setColspan(4);table.addCell(cell);cell = new PdfPCell(new Phrase());cell.setUseAscender(true);cell.setColspan(4);cell.setImage(instance);table.addCell(cell);document.add(table);// 关闭文档,才能输出document.close();writer.close();}
private void tableColumn4(Font font, PdfPTable t, BaseColor grey, String columnKeyName1, String columnValue1, String columnKeyName2, String columnValue2) {PdfPCell cell;cell = new PdfPCell(new Phrase(columnKeyName1, font));// cell.setBackgroundColor(grey);t.addCell(cell);cell = new PdfPCell(new Phrase(columnValue1, font));t.addCell(cell);cell = new PdfPCell(new Phrase(columnKeyName2, font));// cell.setBackgroundColor(grey);t.addCell(cell);cell = new PdfPCell(new Phrase(columnValue2, font));t.addCell(cell);}
@Testpublic void test() {try {create();System.out.println("生成成功");}catch (Exception ex){System.out.println("文件路径错误或者权限不够");}}