Java實(shí)現(xiàn)在Word模板指定位置添加二維碼并生成?PDF
在實(shí)際業(yè)務(wù)場(chǎng)景中,我們常常需要在 Word 模板的指定位置貼上二維碼,然后將其轉(zhuǎn)換為 PDF 電子憑證文檔。下面將詳細(xì)介紹如何使用 Java 完成這一任務(wù),我們會(huì)借助 Apache POI 處理 Word 文檔,ZXing 生成二維碼,以及 Docx4J 將 Word 文檔轉(zhuǎn)換為 PDF。
1. 引入依賴
如果你使用 Maven 管理項(xiàng)目,在 pom.xml 中添加以下依賴:
<dependencies> <!-- Apache POI 處理 Word 文檔 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> <!-- ZXing 生成二維碼 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> <!-- Docx4J 將 Word 轉(zhuǎn)換為 PDF --> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-JAXB-Internal</artifactId> <version>11.4.9</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-JAXB-ReferenceImpl</artifactId> <version>11.4.9</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>11.4.9</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-export-fo</artifactId> <version>11.4.9</version> </dependency> </dependencies>
2. 生成二維碼
使用 ZXing 庫(kù)生成二維碼的代碼如下:
import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; public class QRCodeGenerator { public static BufferedImage generateQRCode(String text, int width, int height) throws WriterException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints); return MatrixToImageWriter.toBufferedImage(bitMatrix); } }
3. 在 Word 模板指定位置插入二維碼
使用 Apache POI 在 Word 模板的指定位置插入二維碼,這里假設(shè)模板中使用特定占位符來(lái)標(biāo)記二維碼的插入位置。
import org.apache.poi.xwpf.usermodel.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; public class WordQRCodeInserter { public static void insertQRCodeIntoWord(String templatePath, String outputPath, BufferedImage qrCodeImage, String placeholder) throws IOException { try (FileInputStream fis = new FileInputStream(templatePath); XWPFDocument document = new XWPFDocument(fis)) { for (XWPFParagraph paragraph : document.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { String text = run.getText(0); if (text != null && text.contains(placeholder)) { // 清除占位符文本 run.setText("", 0); // 插入二維碼圖片 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(qrCodeImage, "png", byteArrayOutputStream); byte[] imageBytes = byteArrayOutputStream.toByteArray(); int pictureType = XWPFDocument.PICTURE_TYPE_PNG; int width = qrCodeImage.getWidth(); int height = qrCodeImage.getHeight(); paragraph.createRun().addPicture(new ByteArrayInputStream(imageBytes), pictureType, "qrcode.png", width * 20, height * 20); } } } try (FileOutputStream fos = new FileOutputStream(outputPath)) { document.write(fos); } } } }
4. 將 Word 文檔轉(zhuǎn)換為 PDF
使用 Docx4J 將插入二維碼后的 Word 文檔轉(zhuǎn)換為 PDF。
import org.docx4j.Docx4J; import org.docx4j.convert.out.FOSettings; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import java.io.*; public class WordToPdfConverter { public static void convertWordToPdf(String wordPath, String pdfPath) throws Exception { WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(wordPath)); FOSettings foSettings = Docx4J.createFOSettings(); foSettings.setWmlPackage(wordMLPackage); try (OutputStream os = new FileOutputStream(pdfPath)) { Docx4J.toPDF(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); } } }
5. 主程序調(diào)用示例
import java.io.IOException; import com.google.zxing.WriterException; public class Main { public static void main(String[] args) { try { // 要生成二維碼的內(nèi)容 String qrCodeContent = "https://www.example.com"; // 生成二維碼圖片 BufferedImage qrCodeImage = QRCodeGenerator.generateQRCode(qrCodeContent, 200, 200); // Word 模板文件路徑 String templatePath = "path/to/your/template.docx"; // 插入二維碼后的 Word 文件輸出路徑 String wordOutputPath = "path/to/your/output.docx"; // 最終生成的 PDF 文件輸出路徑 String pdfOutputPath = "path/to/your/output.pdf"; // Word 模板中的二維碼占位符 String placeholder = "{QR_CODE}"; // 在 Word 模板中插入二維碼 WordQRCodeInserter.insertQRCodeIntoWord(templatePath, wordOutputPath, qrCodeImage, placeholder); // 將插入二維碼后的 Word 文檔轉(zhuǎn)換為 PDF WordToPdfConverter.convertWordToPdf(wordOutputPath, pdfOutputPath); System.out.println("PDF 電子憑證文檔生成成功!"); } catch (WriterException | IOException | Exception e) { e.printStackTrace(); } } }
6. 代碼解釋
1.生成二維碼
QRCodeGenerator 類使用 ZXing 庫(kù)根據(jù)指定的文本內(nèi)容生成二維碼的 BufferedImage 對(duì)象。
2.在 Word 模板插入二維碼
WordQRCodeInserter 類遍歷 Word 文檔的段落和運(yùn)行對(duì)象,查找包含占位符的文本,清除占位符文本后插入二維碼圖片。
3.Word 轉(zhuǎn) PDF
WordToPdfConverter 類使用 Docx4J 將插入二維碼后的 Word 文檔轉(zhuǎn)換為 PDF 文件。
4.主程序調(diào)用
在 Main 類的 main 方法中,依次調(diào)用生成二維碼、在 Word 模板插入二維碼、將 Word 轉(zhuǎn)換為 PDF 的方法,最終生成 PDF 電子憑證文檔。
7. 注意事項(xiàng)
確保 Word 模板文件和輸出文件的路徑正確,并且程序有讀寫(xiě)權(quán)限。
可以根據(jù)需要調(diào)整二維碼的大小和占位符的內(nèi)容。
處理中文等非 ASCII 字符時(shí),要確保字符編碼設(shè)置正確。
通過(guò)以上步驟,你就可以使用 Java 在 Word 模板指定位置貼上二維碼,并將其生成為 PDF 電子憑證文檔啦!
以上就是Java實(shí)現(xiàn)在Word模板指定位置添加二維碼并生成 PDF的詳細(xì)內(nèi)容,更多關(guān)于Java Word添加二維碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android 資源 id詳解及的動(dòng)態(tài)獲取
這篇文章主要介紹了Android 資源 id詳解及的動(dòng)態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2016-12-12使用SpringBoot動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)方式
在我們企業(yè)項(xiàng)目開(kāi)發(fā)的過(guò)程中,有的時(shí)候,一個(gè)項(xiàng)目需要在運(yùn)行時(shí),根據(jù)某種條件選擇使用哪個(gè)數(shù)據(jù)源,那么此時(shí)該怎么進(jìn)行動(dòng)態(tài)切換呢,本文給大家例舉一種常見(jiàn)的實(shí)現(xiàn)方式,文中有詳細(xì)的實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-12-12SpringCloud開(kāi)啟session共享并存儲(chǔ)到Redis的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud開(kāi)啟session共享并存儲(chǔ)到Redis的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02詳解java中List中set方法和add方法的區(qū)別
本文主要介紹了詳解java中List中set方法和add方法的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08重新認(rèn)識(shí)Java中的ThreadLocal
ThreadLocal是JDK包提供的,它提供線程本地變量,如果創(chuàng)建一個(gè)ThreadLocal變量,那么訪問(wèn)這個(gè)變量的每個(gè)線程都會(huì)有這個(gè)變量的一個(gè)副本,在實(shí)際多線程操作的時(shí)候,操作的是自己本地內(nèi)存中的變量,從而規(guī)避了線程安全問(wèn)題2021-05-05IntelliJ IDEA 如何徹底刪除項(xiàng)目的步驟
本篇文章主要介紹了IntelliJ IDEA 如何徹底刪除項(xiàng)目的步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11JavaWeb dbutils執(zhí)行sql命令并遍歷結(jié)果集時(shí)不能查到內(nèi)容的原因分析
這篇文章主要介紹了JavaWeb dbutils執(zhí)行sql命令并遍歷結(jié)果集時(shí)不能查到內(nèi)容的原因分析及簡(jiǎn)單處理方法,文中給大家介紹了javaweb中dbutils的使用,需要的朋友可以參考下2017-12-12