没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:吉炜炜|2025-01-07 10:58:54.350|阅读 10 次
概述:条形码是实现自动化收集和处理数据的关键。本文将介绍如何使用 Spire.PDF for Java 通过 Java 在 PDF 中添加一维条形码或二维码。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
安装 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 条码的步骤。
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 文件中。具体步骤如下:
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"); } }
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@atlvshi.cn
文章转载自:慧都网锁定 Excel 中的单元格对于数据完整性至关重要。在这篇教程中,我们将探讨如何使用 C# 锁定 Excel 中的单元格。
VMProtect 是一款强大的代码保护工具,支持虚拟化、混淆和加密等多种保护技术。本文将详细介绍如何将 VMProtect 集成到 Visual Studio,并提供实用技巧。
在 Word 文档中,图片和形状可以添加超链接,用于跳转到外部网站、文件,或文档中的特定位置。这篇文章将介绍如何使用 Spire.Doc for .NET 在 C# 中以编程方式修改 Word 文档中图片和形状的超链接。
本文将演示如何使用DevExpress WPF Grid控件实现列绑定到数字源字段,欢迎下载最新版组件体验!
Spire.PDF for .NET是独立的PDF控件,用于.NET程序中创建、编辑和操作PDF文档
Spire.PDF for WPFSpire.PDF for WPF 是一款让你的app能够读取、写入和操作PDF文档的完全独立的组件,不需要任何第三方组件库。
Spire.PDF for Java独立专业的Java PDF组件,覆盖PDF文档生成、处理、转换等功能。
Spire.PDF for PythonSpire.PDF for Python是一个专业的 PDF 开发组件
Spire.PDF for SilverlightSpire.PDF for Silverlight是一款PDF组件,让用户能够在Silverlight上开发应用,无需任何第三方软件/库。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@atlvshi.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢