星空外围权威网站

Spire.PDF系列教程:通过 Java 在 PDF 中添加一维条形码或二维码。

翻译|使用教程|编辑:吉炜炜|2025-01-07 10:58:54.350|阅读 10 次

概述:条形码是实现自动化收集和处理数据的关键。本文将介绍如何使用 Spire.PDF for Java 通过 Java 在 PDF 中添加一维条形码或二维码。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

条形码是实现自动化收集和处理数据的关键。条形码可广泛应用于各行各业,从零售业、制造业到医疗保健业和物流业。通过在 PDF 中添加条形码,您可以创建更具互动性的文档,方便扫描和处理。本文将介绍如何使用 Spire.PDF for Java 通过 Java 在 PDF 中添加一维条形码或二维码

Spire.PDF for Java下载

安装 Java 库

首先,您需要下载 Spire.PDF for Java 和 Spire.Barcode for Java 库,然后将相应的 JAR 文件作为依赖项添加到您的 Java 程序中。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>//repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.12.10</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>//repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.barcode</artifactId>
        <version>5.1.11</version>
    </dependency>
</dependencies>

Java 在 PDF 中添加条形码

Spire.PDF for Java 支持多种由不同类代表的一维条形码类型,如 PdfCodabarBarcode、PdfCode128ABarcode、PdfCode32Barcode、PdfCode39Barcode、PdfCode93Barcode。 

每个类都提供了相应的方法来设置条形码文本、大小、颜色等。以下是在 PDF 页面指定位置绘制常见的 Codabar、Code128 和 Code39 条码的步骤。

  • 创建一个 PdfDocument 类的对象。
  • 使用 PdfDocument.getPages().add() 方法添加 PDF 页面。
  • 创建一个 PdfTextWidget 对象,并使用 PdfTextWidget.draw() 方法在页面上绘制文本。
  • 创建 PdfCodabarBarcode、PdfCode128ABarcode、PdfCode39Barcode 对象。
  • 使用相应类的 setBarcodeToTextGapHeight() 方法设置条形码与显示文本之间的间隙。
  • 使用相应类的 setTextDisplayLocation() 方法设置条形码文本显示位置。
  • 使用相应类的 setTextColor() 方法设置条形码文本颜色。
  • 使用相应类的 draw() 方法在 PDF 页面的指定位置绘制条形码。
  • 使用 PdfDocument.saveToFile() 方法保存生成的 PDF 文件。

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

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

public class DrawBarcode {
    public static void main(String[] args) {

        // 创建PDF文档
        PdfDocument pdf = new PdfDocument();

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

        // 初始化y坐标
        double y = 15;

        // 创建字体
        PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 12));

        // 在页面上绘制文本 "Codebar:"
        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);

        // 在页面上绘制Codabar条码
        PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");
        codebar.setBarcodeToTextGapHeight(1f);
        codebar.setBarHeight(50f);
        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);
        y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5;

        // 在页面上绘制文本 "Code128-A:"
        text.setText("Code128-A:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

        // 在页面上绘制Code128-A条码
        PdfCode128ABarcode code128 = new PdfCode128ABarcode("HELLO 00-123");
        code128.setBarcodeToTextGapHeight(1f);
        code128.setBarHeight(50f);
        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:"
        text.setText("Code39:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

        // 在页面上绘制Code39条码
        PdfCode39Barcode code39 = new PdfCode39Barcode("16-273849");
        code39.setBarcodeToTextGapHeight(1f);
        code39.setBarHeight(50f);
        code39.setTextDisplayLocation(TextLocation.Bottom);
        code39.setTextColor(blue);
        point.setLocation(point.x,y);
        code39.draw(page, point);

        // 保存文档
        pdf.saveToFile("DrawBarcode.pdf");
    }
}
在 PDF 页面上绘制 Codabar、Code128 和 Code39 三种条形码

Java 在 PDF 中添加二维码

要在 PDF 文件中添加二维条形码,首先需要使用 Spire.Barcode for Java 库生成二维码,然后使用 Spire.PDF for Java 库将二维码图像添加到 PDF 文件中。具体步骤如下:

  • 创建一个 PdfDocument 类的对象。
  • 使用 PdfDocument.getPages().add() 方法添加 PDF 页面。
  • 创建一个 BarcodeSettings 类的对象。
  • 调用 BarcodeSettings 类的相应属性来设置条形码类型、数据、纠错级别和宽度等。
  • 根据设置创建 BarCodeGenerator 类的对象。
  • 使用 BarCodeGenerator.generateImage() 方法生成二维码图像。
  • 使用 PdfPageBase.getCanvas().drawImage() 方法在 PDF 页面的指定位置绘制二维码图像。
  • 使用 PdfDocument.saveToFile() 方法保存生成的 PDF 文件。

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

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

public class DrawQRCode {
    public static void main(String[] args) {
        // 创建PDF文档
        PdfDocument pdf = new PdfDocument();

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

        // 创建 BarcodeSettings 类的对象
        BarcodeSettings settings = new BarcodeSettings();
        // 将条码类型设置为二维码QR Code
        settings.setType(BarCodeType.QR_Code);
        // 设置二维码数据
        settings.setData("ABC 123456789");
        settings.setData2D("ABC 123456789");
        // 设置在底部显示文本
        settings.setShowTextOnBottom(true);
        // 设置二维码宽度
        settings.setX(2);
        // 设置二维码的纠错级别
        settings.setQRCodeECL(QRCodeECL.M);

        // 生成二维码图片
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        BufferedImage QRimage = barCodeGenerator.generateImage();

        // 初始化y坐标
        double y = 30;

        // 创建字体
        PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 12));

        // 在页面上绘制文本
        PdfTextWidget text = new PdfTextWidget();
        text.setFont(font);
        text.setText("QRcode:");
        PdfLayoutResult result = text.draw(page, 0, y);
        y =(float)(result.getBounds().getY()+ result.getBounds().getHeight() + 2);

        // 在页面上绘制二维码图片
        PdfImage pdfImage = PdfImage.fromImage(QRimage);
        page.getCanvas().drawImage(pdfImage, 0, y);
        
        // 保存文档
        pdf.saveToFile("DrawQRCode.pdf");
    }
}
在 PDF 页面上绘制二维码

欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询 ;技术交流Q群(767755948)


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@atlvshi.cn

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
Spire.PDF for .NET

Spire.PDF for .NET是独立的PDF控件,用于.NET程序中创建、编辑和操作PDF文档

Spire.PDF for WPF

Spire.PDF for WPF 是一款让你的app能够读取、写入和操作PDF文档的完全独立的组件,不需要任何第三方组件库。

Spire.PDF for Java

独立专业的Java PDF组件,覆盖PDF文档生成、处理、转换等功能。

Spire.PDF for Python

Spire.PDF for Python是一个专业的 PDF 开发组件

Spire.PDF for Silverlight

Spire.PDF for Silverlight是一款PDF组件,让用户能够在Silverlight上开发应用,无需任何第三方软件/库。

title
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP