Java 添加条码、二维码到PDF文档

本文介绍如何通过Java程序在PDF文档中添加条码和二维码。创建条码时,可创建多种不同类型的条码,包括Codebar、Code11、Code128A、Code128B、Code32、Code39、Code39 Extended 、Code93和Code93...

本文介绍如何通过Java程序在PDF文档中添加条码和二维码。创建条码时,可创建多种不同类型的条码,包括Codebar、Code11、Code128A、Code128B、Code32、Code39、Code39 Extended 、Code93和Code93 Extended等等,本文以其中的Codebar、Code128A和Code39为例介绍创建方法,可通过参考此方法创建其他类型的条码。

 

本文中的程序测试环境包括:

  • IDEA
  • JDK 1.8.0
  • Spire.Office.jar

:jar导入,可通过创建Maven程序项目,并在pom.xml中配置Maven仓库路径,并指定Free Spire.Office for Java的Maven依赖,点击“Import Changes”即可导入JAR包。(如果使用的Eclipse, 点击保存按钮导入),配置如下:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
</repositories>

<dependencies>
    <dependency>
  <groupId>e-iceblue</groupId>
  <artifactId>spire.office.free</artifactId>
  <version>3.1.1</version>
</dependency>
</dependencies>

 另外,也可通过下载jar包,手动导入Spire.Office.jar到Java程序。

 

Java代码

import com.spire.barcode.*;
import com.spire.pdf.*;
import com.spire.pdf.barcode.*;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;


public class AddBarcodeInPDF {
    public static void main(String[] args) {
        //创建PdfDocument对象
        PdfDocument pdf = new PdfDocument();

        //添加一页
        PdfPageBase page = pdf.getPages().add();

        //初始化y变量
        double y = 15;

        //创建字体
        PdfFont font= new PdfFont(PdfFontFamily.Helvetica, 12,PdfFontStyle.Bold);

        // 绘制文本“Codebar:”到PDF,并绘制Codebar条码到PDF
        PdfTextWidget text = new PdfTextWidget();
        text.setFont(font);
        text.setText("Codebar:");
        PdfLayoutResult result = text.draw(page, 0, y);
        y =(float)(result.getBounds().getY()+ result.getBounds().getHeight() + 2);
        PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");//创建条码
        codebar.setBarcodeToTextGapHeight(1f);
        codebar.setBarHeight(25f);
        codebar.setEnableCheckDigit(true);
        codebar.setShowCheckDigit(true);
        codebar.setTextDisplayLocation(TextLocation.Bottom);
        PdfRGBColor blue = new PdfRGBColor(Color.blue);
        codebar.setTextColor(blue);
        Point2D.Float point = new Point2D.Float();
        point.setLocation(0,y);
        codebar.draw(page,point);//绘制条码到PDF页面
        y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5;

        //绘制文本“Code128-A:”到PDF,并绘制Code128A条码到PDF
        text.setText("Code128-A:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;
        PdfCode128ABarcode code128 = new PdfCode128ABarcode("HELLO 00-123");
        code128.setBarcodeToTextGapHeight(1f);
        code128.setBarHeight(25f);
        code128.setTextDisplayLocation(TextLocation.Bottom);
        code128.setTextColor(blue);
        point.setLocation(point.x,y);
        code128.draw(page, point);
        y =code128.getBounds().getY()+ code128.getBounds().getHeight() + 5;

        //绘制文本“Code39”到PDF,绘制Code39条形码到PDF
        text.setText("Code39:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;
        PdfCode39Barcode code39 = new PdfCode39Barcode("16-273849");//绘制条码
        code39.setBarcodeToTextGapHeight(1f);
        code39.setBarHeight(25f);
        code39.setTextDisplayLocation(TextLocation.Bottom);
        code39.setTextColor(blue);
        point.setLocation(point.x,y);
        code39.draw(page, point);//绘制条码到PDF页面

        //生成二维码图片,绘制到PDF页面
        text.setText("QRCode:");//绘制文本“QR Code:”到PDF
        result = text.draw(page, 200, 0);
        page = result.getPage();
        BarcodeSettings settings = new BarcodeSettings();//创建二维码图形
        settings.setType(BarCodeType.QR_Code);
        settings.setData("123456789");
        settings.setData2D("123456789");
        settings.setX(1f);
        settings.setLeftMargin(0);
        settings.setShowTextOnBottom(true);
        settings.setQRCodeECL(QRCodeECL.Q);
        settings.setQRCodeDataMode(QRCodeDataMode.Numeric);
        BarCodeGenerator generator = new BarCodeGenerator(settings);
        Image image = generator.generateImage();
        PdfImage pdfImage = PdfImage.fromImage((BufferedImage)image);//绘制二维码图片到PDF
        y = result.getBounds().getY()+ result.getBounds().getHeight() + 2;
        page.getCanvas().drawImage(pdfImage,200,y);

        //保存PDF文档
        pdf.saveToFile("添加条码、二维码.pdf");
        pdf.dispose();
    }
}

 

 

  • 发表于 2020-08-28 11:40
  • 阅读 ( 134 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除