1. 程式人生 > 程式設計 >Java 在PDF中新增條形碼的兩種方法

Java 在PDF中新增條形碼的兩種方法

條形碼,是由寬度不等的多個黑條和空白所組成,用以表達一組資訊的圖形識別符號。通過給文件新增條形碼,可以直觀,快捷地訪問和分享一些重要的資訊。本文就將通過使用Java程式來演示如何在PDF文件中新增Codebar、Code128A和Code39條形碼。除此之外,還可支援建立Code11、Code128B、Code32、Code39 Extended 、Code93和Code93 Extended條形碼。

使用工具:Free Spire.PDF for Java (免費版)

Jar檔案獲取及匯入:

方法1:通過E-iceblue中文官網 下載獲取jar包。解壓後將lib資料夾下的Spire.Pdf.jar檔案匯入Java程式。(如下圖)

Java 在PDF中新增條形碼的兩種方法

方法2:通過maven倉庫安裝匯入。具體安裝教程參見 此網頁 。

程式碼示例:

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.barcode.*;
import com.spire.pdf.graphics.*;
import static com.spire.pdf.graphics.PdfFontStyle.Bold;
import java.awt.*;
import java.awt.geom.Point2D;
import java.util.EnumSet;

public class DrawBarcode {
  public static void main(String[] args) {
    //建立PdfDocument物件
    PdfDocument doc = new PdfDocument();

    //新增一頁
    PdfPageBase page = doc.getPages().add();

    //初始化y變數
    double y = 15;

    //建立字型
    PdfFont font= new PdfFont(PdfFontFamily.Helvetica,12,EnumSet.of(Bold));

    // 繪製文字“Codebar:”到PDF
    PdfTextWidget text = new PdfTextWidget();
    text.setFont(font);
    text.setText("Codebar:");
    PdfLayoutResult result = text.draw(page,y);
    y =(float)(result.getBounds().getY()+ result.getBounds().getHeight() + 2);

    //繪製Codebar條碼到PDF
    PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");
    codebar.setBarcodeToTextGapHeight(1f);
    codebar.setBarHeight(50f);
    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);
    y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5;

    //繪製文字“Code128-A:”到PDF
    text.setText("Code128-A:");
    result = text.draw(page,y);
    page = result.getPage();
    y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

    //繪製Code128A條碼到PDF
    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”到PDF
    text.setText("Code39:");
    result = text.draw(page,y);
    page = result.getPage();
    y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

    //繪製Code39條形碼到PDF
    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文件
    doc.saveToFile("output/DrawBarcode.pdf");
  }
}

新增效果:

Java 在PDF中新增條形碼的兩種方法

總結

到此這篇關於Java 在PDF中新增條形碼的兩種方法的文章就介紹到這了,更多相關java pdf 條形碼內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!