日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Itext 7 生成pdf带有背景图

發布時間:2024/1/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Itext 7 生成pdf带有背景图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Itext 7 生成pdf帶有背景圖并下載PDF到本地

引入jar包

<dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.0.3</version><type>pom</type></dependency>

代碼實現

@GetMapping(value = "itextPdfDownLoad")public void itextPdfDownLoad(HttpServletResponse response) throws IOException {response.reset();//設置文件編碼response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("個人榮譽.PDF", "UTF-8"));response.setContentType("application/octet-stream;charset=UTF-8");OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());PageSize pageSize = PageSize.A4.rotate();PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));Document doc = new Document(pdfDoc, pageSize);float[] f = new float[]{0.01f};Table table = new Table(UnitValue.createPercentArray(f));table.setWidth(745);Cell cell = new Cell();//設置表格邊框顏色 這里設置的是白色cell.setBorder(new SolidBorder(new DeviceRgb(0xFFF, 0xFFF, 0xFFF),3));//設置插入字體PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H",true);//背景圖上寫入漢字Paragraph p = new Paragraph("努爾哈赤").setFont(font).setFontSize(13f).setFontColor(DeviceGray.BLACK);//寫入漢字坐標p.setFirstLineIndent(140f);p.setFixedLeading(418f);cell.add(p);//獲取表格背景圖URL resource = this.getClass().getClassLoader().getResource("honour.png");assert resource != null;Image img = new Image(ImageDataFactory.create(resource.getPath()));cell.setNextRenderer(new ImageBackgroundCellRenderer(cell, img));cell.setHeight(590 * img.getImageHeight() / img.getImageWidth());table.addCell(cell);doc.add(table);doc.close();outputStream.flush();}

實現效果

總結

以上是生活随笔為你收集整理的Itext 7 生成pdf带有背景图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。