本文介紹如何通過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();
}
}
條碼、二維碼添加效果圖:
(完)