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