java完整的利用itext5制作pdf、二维码图片插入pdf,并解析pdf中的二维码信息

本文介绍了如何使用Java的itext5库生成包含二维码的PDF,以及结合ZXing和QRCore库来解析PDF中的二维码信息。通过示例代码展示了从PDF中提取图片并识别二维码的过程,同时提供了源码下载链接和作者的交流群信息。
摘要由CSDN通过智能技术生成

利用itext5、zxing、QRCore制作pdf、二维码图片插入pdf,并解析pdf中的二维码信息,手机可以实现扫描获取二维码的信息,并进行验证你的解析是否正确。

先是生成二维码图片并插入pdf中

package com.parseImage;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class CreateBarcodePdf {

    public static void main(String... args) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new 
//pdf输出地址
FileOutputStream("./src/test/resources/scanner.pdf"));

        document.open();
        PdfContentByte cb = writer.getDirectContent();
        
        BarcodeQRCode barcodeQRCode = new BarcodeQRCode("http://memorynotfound.com", 1000, 1000, null);
        Image codeQrImage = barcodeQRCode.getImage();
        codeQrImage.scaleAbsolute(100, 100);
        
        document.add(codeQrImage);

        document.close();
    }
}

效果图
这里写图片描述

抽取pdf中二维码图片

package com.parseImage;

import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;

/**

  • Extracts images from a PDF file.
    */
    public class ExtractImages {

    /**

    • PDF to extract images from
      */
      //pdf路径
      public static final String SOURCE_PDF = “./src/test/resources/scanner.pdf”;

    /**

    • Parses a PDF and extracts all the images.

    • @param filename the source PDF

    • @param destination the directory to save images
      */
      public void extractImages(String filename, String destination)
      throws IOException, DocumentException {
      System.out.println("Processing PDF at " + filename);
      System.out.println("Saving images to " + destination);

      PdfReader reader = new PdfReader(filename);
      PdfReaderContentParser parser = new PdfReaderContentParser(reader);
      ImageRenderListener listener = new ImageRenderListener(destination + “/Img”);
      for (int i = 1; i <= reader.getNumberOfPages(); i++) {
      parser.processContent(i, listener);
      }
      reader.close();
      }

    /**</

要在iTextPDF的右上角插入二维码图片,我们需要按照以下步骤进行操作。 首先,我们需要导入相关的iTextPDF库文件,确保我们可以使用相关的类和方法。 接下来,我们需要创建一个Document对象,用于表示我们要创建的PDF文件。在创建Document对象时,我们可以指定要使用的页面大小和边距。 然后,我们可以使用PdfWriter类将Document对象写入一个PDF文件,以便我们可以将内容添加到PDF文件。 在文档插入内容时,我们需要使用PdfContentByte类的实例来操作内容的位置和样式。我们可以使用该类的方法来插入文本、图片等。 要在右上角插入二维码图片,我们可以使用PdfContentByte类的addImage方法来添加图片。我们需要提供要插入图片的路径或URL,并指定图片的位置和大小。 在添加图片之前,我们可以使用setRGBColorFill方法来设置要插入图片的位置。我们可以使用setRGBColorFill方法传入一个颜色参数,例如Color.BLACK,来设置坐标的颜色。 最后,我们可以调用Document对象的close方法来关闭文档,以确保PDF文件的正确生成。 总结起来,我们需要导入相关库文件,创建Document对象并使用PdfWriter写入文件。然后,使用PdfContentByte类的实例来添加图片并设置其位置。最后关闭文档以生成PDF文件。以上就是在iTextPDF右上角插入二维码图片的步骤和方法。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值