翻譯|使用教程|編輯:吉煒煒|2025-01-07 10:58:54.350|閱讀 123 次
概述:條形碼是實現(xiàn)自動化收集和處理數(shù)據(jù)的關(guān)鍵。本文將介紹如何使用 Spire.PDF for Java 通過 Java 在 PDF 中添加一維條形碼或二維碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
安裝 Java 庫
首先,您需要下載 Spire.PDF for Java 和 Spire.Barcode for Java 庫,然后將相應(yīng)的 JAR 文件作為依賴項添加到您的 Java 程序中。如果您使用 Maven,則可以將以下代碼添加到項目的 pom.xml 文件中,從而在應(yīng)用程序中導(dǎo)入 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。
每個類都提供了相應(yīng)的方法來設(shè)置條形碼文本、大小、顏色等。以下是在 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) {
// 創(chuàng)建PDF文檔
PdfDocument pdf = new PdfDocument();
// 添加頁面
PdfPageBase page = pdf.getPages().add();
// 初始化y坐標(biāo)
double y = 15;
// 創(chuàng)建字體
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) {
// 創(chuàng)建PDF文檔
PdfDocument pdf = new PdfDocument();
// 添加頁面
PdfPageBase page = pdf.getPages().add();
// 創(chuàng)建 BarcodeSettings 類的對象
BarcodeSettings settings = new BarcodeSettings();
// 將條碼類型設(shè)置為二維碼QR Code
settings.setType(BarCodeType.QR_Code);
// 設(shè)置二維碼數(shù)據(jù)
settings.setData("ABC 123456789");
settings.setData2D("ABC 123456789");
// 設(shè)置在底部顯示文本
settings.setShowTextOnBottom(true);
// 設(shè)置二維碼寬度
settings.setX(2);
// 設(shè)置二維碼的糾錯級別
settings.setQRCodeECL(QRCodeECL.M);
// 生成二維碼圖片
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
BufferedImage QRimage = barCodeGenerator.generateImage();
// 初始化y坐標(biāo)
double y = 30;
// 創(chuàng)建字體
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產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)