如何通过itext导出pdf简表以及在表格中添加图片

在maven中导入相应的jar包
groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
关于简表样式的pdf相应的代码以及在表格中添加图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | package com.how2java.springboot.web; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.File; import java.io.FileOutputStream; public class PdfReport { // main测试 public static void main(String[] args) throws Exception { try { // 1.新建document对象 Document document = new Document(PageSize.A4); // 建立一个Document对象 // 2.建立一个书写器(Writer)与document对象关联 File file = new File( "D:\\PDFDemo.pdf" ); file.createNewFile(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); //writer.setPageEvent(new Watermark("HELLO ITEXTPDF"));// 水印 //writer.setPageEvent(new MyHeaderFooter());// 页眉/页脚 // 3.打开文档 document.open(); document.addTitle( "Title@PDF-Java" ); // 标题 document.addAuthor( "Author@umiz" ); // 作者 document.addSubject( "Subject@iText pdf sample" ); // 主题 document.addKeywords( "Keywords@iTextpdf" ); // 关键字 document.addCreator( "Creator@umiz`s" ); // 创建者 // 4.向文档中添加内容 new PdfReport().generatePDF(document); // 5.关闭文档 document.close(); } catch (Exception e) { e.printStackTrace(); } } // 定义全局的字体静态变量 private static Font titlefont; private static Font headfont; private static Font keyfont; private static Font textfont; // 最大宽度 private static int maxWidth = 360; // 静态代码块 static { try { // 不同字体(这里定义为同一种字体:包含不同字号、不同style) BaseFont bfChinese = BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" , BaseFont.NOT_EMBEDDED); titlefont = new Font(bfChinese, 16, Font.BOLD); headfont = new Font(bfChinese, 14, Font.BOLD); keyfont = new Font(bfChinese, 10, Font.BOLD); textfont = new Font(bfChinese, 10, Font.NORMAL); } catch (Exception e) { e.printStackTrace(); } } // 生成PDF文件 public void generatePDF(Document document) throws Exception { // 段落 Paragraph paragraph = new Paragraph( "干部情况简历表" , titlefont); paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12); //设置左缩进 paragraph.setIndentationRight(12); //设置右缩进 paragraph.setFirstLineIndent(24); //设置首行缩进 paragraph.setLeading(50f); //行间距 paragraph.setSpacingBefore(5f); //设置段落上空白 paragraph.setSpacingAfter(10f); //设置段落下空白 /* * // 直线 Paragraph p1 = new Paragraph(); p1.add(new Chunk(new LineSeparator())); * * // 点线 Paragraph p2 = new Paragraph(); p2.add(new Chunk(new * DottedLineSeparator())); * * // 超链接 Anchor anchor = new Anchor("baidu"); * anchor.setReference("www.baidu.com"); * * // 定位 Anchor gotoP = new Anchor("goto"); gotoP.setReference("#top"); */ // 添加图片 Image image = Image.getInstance( "https://img-blog.csdn.net/20180801174617455?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzg0ODcxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" ); image.setAlignment(Image.ALIGN_CENTER); image.scalePercent(40); //依照比例缩放 image.setAbsolutePosition(40,60); //Image image = Image.getInstance("E:\\2019-01\\ygtd.png"); // image.scaleAbsolute(5,5);//自定义大小 // image.scalePercent(getPercent2(image.getWidth(),image.getHeight()));//缩放百分比 --- 不起作用 // image.scaleToFit(50, 50);//自定义大 // 表格 PdfPTable table = createTable( new float [] { 40, 60, 40, 60, 60, 60,80 }); table.addCell(createCell( "档案编号:--------" , textfont, Element.ALIGN_RIGHT, 7, false )); table.addCell(createCell( "姓名" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "中午11:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "性别" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "下午15:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "出生年月" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "晚上19:00" , textfont, Element.ALIGN_CENTER)); //cell.setRowspan(3); table.addCell(createCell(image, textfont,1,1,3)); table.addCell(createCell( "民族" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "中午11:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "籍贯" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "下午15:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "出生地" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "晚上19:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "入党时间" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "中午11:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "工作时间" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "下午15:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "职称" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "晚上19:00" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "身份证号码" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "412722199511052511" , textfont, Element.ALIGN_CENTER,6)); table.addCell(createCell( "现任职务" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "412722199511052511" , textfont, Element.ALIGN_CENTER,6)); table.addCell(createCell( "学习培训经历" , keyfont,1,1,3)); table.addCell(createCell( "全日制学历学位" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "412722199511052511" , textfont, Element.ALIGN_CENTER,5)); table.addCell(createCell( "全日制学历学位" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "412722199511052511" , textfont, Element.ALIGN_CENTER,5)); table.addCell(createCell( "全日制学历学位" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "412722199511052511" + "\n" + "12" + "\n" + "23" + "\n" + "234" + "\n" + "2132" + "12" + "\n" + "23" + "\n" + "234" + "\n" + "2132" , textfont, Element.ALIGN_CENTER,5)); table.addCell(createCell( "主要简历" , keyfont,1,1,6)); table.addCell(createCell( "412722199511052511" , textfont,6,6,6)); table.addCell(createCell( "家庭成员及主要社会关系" , keyfont,1,1,8)); table.addCell(createCell( "与本人关系" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "姓名" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "出生年月" , keyfont, Element.ALIGN_CENTER)); table.addCell(createCell( "政治面貌" , textfont, Element.ALIGN_CENTER)); table.addCell(createCell( "工作单位及职务" , keyfont, Element.ALIGN_CENTER,2)); for ( int i = 0; i < 8; i++) { table.addCell(createCell( "起床" , textfont)); table.addCell(createCell( "吃午饭" , textfont)); table.addCell(createCell( "午休" , textfont)); table.addCell(createCell( "下午茶" , textfont)); table.addCell(createCell( "回家" , textfont,2,2)); } document.add(paragraph); // document.add(anchor); // document.add(p2); // document.add(gotoP); // document.add(p1); document.add(table); //document.add(image); } /**------------------------创建表格单元格的方法start----------------------------*/ public PdfPCell createCell(Image image, Font font, int align, int colspan, int rowspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setRowspan(rowspan); cell.setFixedHeight(rowspan*25f); cell.setImage(image); // cell.setPhrase(new Phrase(new Chunk(image, 0, 0,false))); return cell; } /** * 创建单元格(指定字体) * @param value * @param font * @return */ public PdfPCell createCell(String value, Font font) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPhrase( new Phrase(value, font)); cell.setFixedHeight(25f); return cell; } /** * 创建单元格(指定字体、水平..) * @param value * @param font * @param align * @return */ public PdfPCell createCell(String value, Font font, int align) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setPhrase( new Phrase(value, font)); cell.setFixedHeight(25f); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并) * @param value * @param font * @param align * @param colspan * @return */ public PdfPCell createCell(String value, Font font, int align, int colspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPhrase( new Phrase(value, font)); return cell; } public PdfPCell createCell(String value, Font font, int align, int colspan, int rowspan) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setRowspan(rowspan); cell.setFixedHeight(rowspan*25f); cell.setPhrase( new Phrase(value, font)); return cell; } /** * 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距) * @param value * @param font * @param align * @param colspan * @param boderFlag * @return */ public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setColspan(colspan); cell.setPhrase( new Phrase(value, font)); cell.setPadding(3.0f); if (!boderFlag) { cell.setBorder(0); cell.setPaddingTop(15.0f); cell.setPaddingBottom(8.0f); } else if (boderFlag) { cell.setBorder(0); cell.setPaddingTop(0.0f); cell.setPaddingBottom(15.0f); } return cell; } /** * 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距) * @param value * @param font * @param align * @param borderWidth * @param paddingSize * @param flag * @return */ public PdfPCell createCell(String value, Font font, int align, float [] borderWidth, float [] paddingSize, boolean flag) { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(align); cell.setFixedHeight(25f); cell.setPhrase( new Phrase(value, font)); cell.setBorderWidthLeft(borderWidth[0]); cell.setBorderWidthRight(borderWidth[1]); cell.setBorderWidthTop(borderWidth[2]); cell.setBorderWidthBottom(borderWidth[3]); cell.setPaddingTop(paddingSize[0]); cell.setPaddingBottom(paddingSize[1]); if (flag) { cell.setColspan(2); } return cell; } /**------------------------创建表格单元格的方法end----------------------------*/ /**--------------------------创建表格的方法start------------------- ---------*/ /** * 创建默认列宽,指定列数、水平(居中、右、左)的表格 * @param colNumber * @param align * @return */ public PdfPTable createTable( int colNumber, int align) { PdfPTable table = new PdfPTable(colNumber); try { table.setTotalWidth(maxWidth); table.setLockedWidth( true ); table.setHorizontalAlignment(align); table.getDefaultCell().setBorder(1); } catch (Exception e) { e.printStackTrace(); } return table; } /** * 创建指定列宽、列数的表格 * @param widths * @return */ public PdfPTable createTable( float [] widths) { PdfPTable table = new PdfPTable(widths); try { table.setTotalWidth(maxWidth); table.setLockedWidth( true ); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder(1); } catch (Exception e) { e.printStackTrace(); } return table; } /** * 创建空白的表格 * @return */ public PdfPTable createBlankTable() { PdfPTable table = new PdfPTable(1); table.getDefaultCell().setBorder(0); table.addCell(createCell( "" , keyfont)); table.setSpacingAfter(20.0f); table.setSpacingBefore(20.0f); return table; } /**--------------------------创建表格的方法end------------------- ---------*/ } |
如果你不知道自己要去哪里,那么去哪里都是一样
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?