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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

itextpdf实现多PDF文件合并为一个PDF文档

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 itextpdf实现多PDF文件合并为一个PDF文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 1、引入POM
  • 2、編寫工具類
  • 3、代碼實現

1、引入POM

<dependencies><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.3</version></dependency></dependencies>

2、編寫工具類

public class PdfUtils { /*** 創建pdf文檔** @param response* @param fileName* @return*/public static Document createDocument(HttpServletResponse response, String fileName) {try {response.reset();response.setHeader("Content-Type", "application/pdf-stream");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");} catch (Exception e) {e.printStackTrace();}// 設置大小return new Document(PageSize.A4, 50, 50, 80, 50);}/*** 合并pdf** @param response* @param document* @param byteList*/public static void mergePdf(HttpServletResponse response, Document document, List<byte[]> byteList) {try {OutputStream os = response.getOutputStream();// 以任意一個頁面創建pdf模板document = new Document(new PdfReader(byteList.get(0)).getPageSize(1));PdfCopy copy = new PdfCopy(document, os);// 打開文檔document.open();// 遍歷pdf文件for (byte[] bytes : byteList) {// 讀取pdfPdfReader reader = new PdfReader(bytes);// 獲取頁數int numberOfPages = reader.getNumberOfPages();// 從第一頁開始for (int i = 1; i <= numberOfPages; i++) {// 新建文檔頁document.newPage();// 復制操作PdfImportedPage page = copy.getImportedPage(reader, i);copy.addPage(page);}}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("PDF合并失敗!");} finally {if (document != null) {document.close();}}} }

3、代碼實現

支持上傳MultipartFile文件或者通過本地文件的Path的進行合并PDF

@RestController @RequestMapping("/test") public class PdfController { /*** 上傳文件進行合并** @param files* @param response*/@PostMapping("pdfStream")public void pdf(@RequestParam("files") MultipartFile[] files, HttpServletResponse response) {try {// 判斷文件是否合規、以及轉化為輸入流List<byte[]> byteList = new ArrayList<>();for (MultipartFile file : files) {// 限制格式只能為pdfif (file == null) {throw new RuntimeException("文件不能為空!");}String originalFilename = file.getOriginalFilename();String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);if (!substring.equals("pdf")) {throw new RuntimeException("只能上傳pdf格式文件!");}// 獲取輸入流// 獲取輸入流InputStream inputStream = file.getInputStream();byte[] bytes = inputStreamToByte(inputStream);byteList.add(bytes);}// 1.設置輸出流名稱String fileName = "合并文件.pdf";Document document = PdfUtils.createDocument(response, fileName);// 2.開啟合并PdfUtils.mergePdf(response, document, byteList);} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e.getMessage());}}/*** 本地文件轉換合并** @param response*/@GetMapping("pdfPath")public void pdfPath(HttpServletResponse response) {try {// 加載本地文件List<String> paths = new ArrayList<>();paths.add("C:\\Users\\LiGezZ\\Desktop\\1.pdf");paths.add("C:\\Users\\LiGezZ\\Desktop\\2.pdf");// 判斷文件是否合規、以及轉化為輸入流List<byte[]> byteList = new ArrayList<>();for (String path : paths) {File file = new File(path);// 限制格式只能為pdfif (!file.exists()) {throw new RuntimeException("文件不存在!");}if (file.length() <= 0) {throw new RuntimeException("文件不能為空!");}String name = file.getName();String substring = name.substring(name.lastIndexOf(".") + 1);if (!substring.equals("pdf")) {throw new RuntimeException("只能上傳pdf格式文件!");}// 獲取輸入流FileInputStream inputStream = new FileInputStream(file);byte[] bytes = inputStreamToByte(inputStream);byteList.add(bytes);}// 1.設置輸出流名稱String fileName = "合并文件.pdf";Document document = PdfUtils.createDocument(response, fileName);// 2.開啟合并PdfUtils.mergePdf(response, document, byteList);} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e.getMessage());}}private byte[] inputStreamToByte(InputStream inputStream) {try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {byte[] bytes = new byte[1024];int len = 0;while ((len = inputStream.read(bytes)) != -1) {os.write(bytes, 0, len);}os.flush();return os.toByteArray();} catch (Exception e) {throw new RuntimeException("InputStream轉換失敗!");}} }

總結

以上是生活随笔為你收集整理的itextpdf实现多PDF文件合并为一个PDF文档的全部內容,希望文章能夠幫你解決所遇到的問題。

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