Java实现在Word模板指定位置添加二维码并生成 PDF

 更新时间:2025年02月12日 09:04:47   作者:五行星辰  
在实际业务场景中,我们常常需要在 Word 模板的指定位置贴上二维码,然后将其转换为 PDF 电子凭证文档,下面我们就来看看具体实现方法吧

在实际业务场景中,我们常常需要在 Word 模板的指定位置贴上二维码,然后将其转换为 PDF 电子凭证文档。下面将详细介绍如何使用 Java 完成这一任务,我们会借助 Apache POI 处理 Word 文档,ZXing 生成二维码,以及 Docx4J 将 Word 文档转换为 PDF。

1. 引入依赖

如果你使用 Maven 管理项目,在 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 转换为 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 库生成二维码的代码如下:

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 模板的指定位置插入二维码,这里假设模板中使用特定占位符来标记二维码的插入位置。

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 文档转换为 PDF

使用 Docx4J 将插入二维码后的 Word 文档转换为 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. 主程序调用示例

import java.io.IOException;
 
import com.google.zxing.WriterException;
 
public class Main {
    public static void main(String[] args) {
        try {
            // 要生成二维码的内容
            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 文档转换为 PDF
            WordToPdfConverter.convertWordToPdf(wordOutputPath, pdfOutputPath);
 
            System.out.println("PDF 电子凭证文档生成成功!");
        } catch (WriterException | IOException | Exception e) {
            e.printStackTrace();
        }
    }
}

6. 代码解释

1.生成二维码

QRCodeGenerator 类使用 ZXing 库根据指定的文本内容生成二维码的 BufferedImage 对象。

2.在 Word 模板插入二维码

WordQRCodeInserter 类遍历 Word 文档的段落和运行对象,查找包含占位符的文本,清除占位符文本后插入二维码图片。

3.Word 转 PDF

WordToPdfConverter 类使用 Docx4J 将插入二维码后的 Word 文档转换为 PDF 文件。

4.主程序调用

在 Main 类的 main 方法中,依次调用生成二维码、在 Word 模板插入二维码、将 Word 转换为 PDF 的方法,最终生成 PDF 电子凭证文档。

7. 注意事项

确保 Word 模板文件和输出文件的路径正确,并且程序有读写权限。

可以根据需要调整二维码的大小和占位符的内容。

处理中文等非 ASCII 字符时,要确保字符编码设置正确。

通过以上步骤,你就可以使用 Java 在 Word 模板指定位置贴上二维码,并将其生成为 PDF 电子凭证文档啦!

以上就是Java实现在Word模板指定位置添加二维码并生成 PDF的详细内容,更多关于Java Word添加二维码的资料请关注脚本之家其它相关文章!

相关文章

  • java web如何解决瞬间高并发

    java web如何解决瞬间高并发

    这篇文章主要为大家详细介绍了java web解决瞬间高并发的策略,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android 资源 id详解及的动态获取

    Android 资源 id详解及的动态获取

    这篇文章主要介绍了Android 资源 id详解及的动态获取的相关资料,需要的朋友可以参考下
    2016-12-12
  • 使用SpringBoot动态切换数据源的实现方式

    使用SpringBoot动态切换数据源的实现方式

    在我们企业项目开发的过程中,有的时候,一个项目需要在运行时,根据某种条件选择使用哪个数据源,那么此时该怎么进行动态切换呢,本文给大家例举一种常见的实现方式,文中有详细的实现步骤,需要的朋友可以参考下
    2023-12-12
  • Eclipse内置浏览器打开方法

    Eclipse内置浏览器打开方法

    这篇文章主要介绍了Eclipse内置浏览器打开方法,需要的朋友可以了解下。
    2017-09-09
  • SpringCloud开启session共享并存储到Redis的实现

    SpringCloud开启session共享并存储到Redis的实现

    这篇文章主要介绍了SpringCloud开启session共享并存储到Redis的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • 详解java中List中set方法和add方法的区别

    详解java中List中set方法和add方法的区别

    本文主要介绍了详解java中List中set方法和add方法的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • java虚拟机多线程进阶篇总结

    java虚拟机多线程进阶篇总结

    在本篇内容里小编给大家整理了关于java虚拟机多线程进阶篇的相关知识点内容,有兴趣的朋友们跟着参考下。
    2019-06-06
  • 重新认识Java中的ThreadLocal

    重新认识Java中的ThreadLocal

    ThreadLocal是JDK包提供的,它提供线程本地变量,如果创建一个ThreadLocal变量,那么访问这个变量的每个线程都会有这个变量的一个副本,在实际多线程操作的时候,操作的是自己本地内存中的变量,从而规避了线程安全问题
    2021-05-05
  • IntelliJ IDEA 如何彻底删除项目的步骤

    IntelliJ IDEA 如何彻底删除项目的步骤

    本篇文章主要介绍了IntelliJ IDEA 如何彻底删除项目的步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • JavaWeb dbutils执行sql命令并遍历结果集时不能查到内容的原因分析

    JavaWeb dbutils执行sql命令并遍历结果集时不能查到内容的原因分析

    这篇文章主要介绍了JavaWeb dbutils执行sql命令并遍历结果集时不能查到内容的原因分析及简单处理方法,文中给大家介绍了javaweb中dbutils的使用,需要的朋友可以参考下
    2017-12-12

最新评论