条形码是实现自动化收集和处理数据的关键。条形码可广泛应用于各行各业,从零售业、制造业到医疗保健业和物流业。通过在 pdf 中添加条形码,您可以创建更具互动性的文档,方便扫描和处理。本文将介绍如何使用 spire.pdf for java 通过 java 在 pdf 中添加一维条形码或二维码。
安装 java 库
首先,您需要下载 spire.pdf for java 和 spire.barcode for java 库,然后将相应的 jar 文件作为依赖项添加到您的 java 程序中。如果您使用 maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 jar 文件。
com.e-iceblue
e-iceblue
https://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.pdf
11.2.3
com.e-iceblue
e-iceblue
https://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.barcode
5.1.11
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 文件。
- java
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");
}
}
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 文件。
- java
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");
}
}
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。