日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

SpringBoot+Vue+Itext实现前端请求文件流的方式导出PDF时在指定位置添加照片

發(fā)布時(shí)間:2025/3/19 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot+Vue+Itext实现前端请求文件流的方式导出PDF时在指定位置添加照片 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

場(chǎng)景

SpringBoot+Vue+Itext實(shí)現(xiàn)前端請(qǐng)求文件流的方式下載PDF:

SpringBoot+Vue+Itext實(shí)現(xiàn)前端請(qǐng)求文件流的方式下載PDF_BADAO_LIUMANG_QIZHI的博客-CSDN博客

在上面的基礎(chǔ)上怎樣實(shí)現(xiàn)在導(dǎo)出pdf時(shí)在指定的位置插入照片,并設(shè)置照片大小。

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓氣質(zhì)_CSDN博客-C#,SpringBoot,架構(gòu)之路領(lǐng)域博主
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書(shū)、教程推送與免費(fèi)下載。

實(shí)現(xiàn)

1、前后端請(qǐng)求的流程同上面一樣,準(zhǔn)備一張照片,這里路徑是D:\badao.jpg

2、然后后臺(tái)在doc對(duì)象上繼續(xù)添加Image對(duì)象

??????? //添加照片Image jpg = Image.getInstance("D:\\badao.jpg");//設(shè)置圖片坐標(biāo)位置jpg.setAbsolutePosition(400,100);//設(shè)置圖片長(zhǎng)和寬的縮放,這里都縮放到50%jpg.scalePercent(50f,50f);doc.add(jpg);

注意這里的Image是com.itextpdf.text包下的。

然后可以通過(guò)setAbsolutePosition設(shè)置圖片的位置。

還可以通過(guò)scalePercent設(shè)置長(zhǎng)和寬的縮放百分比

3、完整后臺(tái)Controller代碼

@RequestMapping("/getPdfWithImage")public void getPdfWithImage(HttpServletRequest request, HttpServletResponse response) throws Exception {//設(shè)置響應(yīng)格式等response.setContentType("application/pdf");response.setHeader("Expires", "0");response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");response.setHeader("Pragma", "public");Map<String,Object> map = new HashMap<>();//設(shè)置紙張規(guī)格為A4紙Rectangle rect = new Rectangle(PageSize.A4);//創(chuàng)建文檔實(shí)例Document doc=new Document(rect);//添加中文字體BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//設(shè)置字體樣式Font textFont = new Font(bfChinese,11, Font.NORMAL); //正常//Font redTextFont = new Font(bfChinese,11,Font.NORMAL,Color.RED); //正常,紅色Font boldFont = new Font(bfChinese,11,Font.BOLD); //加粗//Font redBoldFont = new Font(bfChinese,11,Font.BOLD,Color.RED); //加粗,紅色Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一級(jí)標(biāo)題Font secondTitleFont = new Font(bfChinese,15,Font.BOLD, CMYKColor.BLUE); //二級(jí)標(biāo)題Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下劃線斜體//設(shè)置字體com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);//設(shè)置要導(dǎo)出的pdf的標(biāo)題String title = "霸道流氓氣質(zhì)";response.setHeader("Content-disposition","attachment; filename=".concat(String.valueOf(URLEncoder.encode(title + ".pdf", "UTF-8"))));OutputStream out = response.getOutputStream();PdfWriter.getInstance(doc,out);doc.open();doc.newPage();//新建段落//使用二級(jí)標(biāo)題 顏色為藍(lán)色Paragraph p1 = new Paragraph("二級(jí)標(biāo)題", secondTitleFont);//設(shè)置行高p1.setLeading(0);//設(shè)置標(biāo)題居中p1.setAlignment(Element.ALIGN_CENTER);//將段落添加到文檔上doc.add(p1);//設(shè)置一個(gè)空的段落,行高為18 什么內(nèi)容都不顯示Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese11);doc.add(blankRow1);//新建表格 列數(shù)為2PdfPTable table1 = new PdfPTable(2);//給表格設(shè)置寬度int width1[] = {80,60};table1.setWidths(width1);//新建單元格String name="霸道的程序猿";String gender="男";//給單元格賦值 每個(gè)單元格為一個(gè)段落,每個(gè)段落的字體為加粗PdfPCell cell11 = new PdfPCell(new Paragraph("姓名: "+name,boldFont));PdfPCell cell12 = new PdfPCell(new Paragraph("性別: "+gender,boldFont));//設(shè)置單元格邊框?yàn)?cell11.setBorder(0);cell12.setBorder(0);table1.addCell(cell11);table1.addCell(cell12);doc.add(table1);PdfPTable table3 = new PdfPTable(2);table3.setWidths(width1);PdfPCell cell15 = new PdfPCell(new Paragraph("博客主頁(yè): https://blog.csdn.net/BADAO_LIUMANG_QIZHI ",boldFont));PdfPCell cell16 = new PdfPCell(new Paragraph("當(dāng)前時(shí)間: "+new Date().toString(),boldFont));cell15.setBorder(0);cell16.setBorder(0);table3.addCell(cell15);table3.addCell(cell16);doc.add(table3);//添加照片Image jpg = Image.getInstance("D:\\badao.jpg");//設(shè)置圖片坐標(biāo)位置jpg.setAbsolutePosition(400,100);//設(shè)置圖片長(zhǎng)和寬的縮放,這里都縮放到50%jpg.scalePercent(50f,50f);doc.add(jpg);doc.close();}

總結(jié)

以上是生活随笔為你收集整理的SpringBoot+Vue+Itext实现前端请求文件流的方式导出PDF时在指定位置添加照片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。