Java 在PDF中添加條形碼的兩種方法
條形碼,是由寬度不等的多個(gè)黑條和空白所組成,用以表達(dá)一組信息的圖形標(biāo)識(shí)符。通過給文檔添加條形碼,可以直觀,快捷地訪問和分享一些重要的信息。本文就將通過使用Java程序來演示如何在PDF文檔中添加Codebar、Code128A和Code39條形碼。除此之外,還可支持創(chuàng)建Code11、Code128B、Code32、Code39 Extended 、Code93和Code93 Extended條形碼。
使用工具:Free Spire.PDF for Java (免費(fèi)版)
Jar文件獲取及導(dǎo)入:
方法1:通過E-iceblue中文官網(wǎng) 下載獲取jar包。解壓后將lib文件夾下的Spire.Pdf.jar文件導(dǎo)入Java程序。(如下圖)
方法2:通過maven倉庫安裝導(dǎo)入。具體安裝教程參見 此網(wǎng)頁 。
代碼示例:
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) { //創(chuàng)建PdfDocument對(duì)象 PdfDocument doc = new PdfDocument(); //添加一頁 PdfPageBase page = doc.getPages().add(); //初始化y變量 double y = 15; //創(chuàng)建字體 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, 0, 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, 0, 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, 0, 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"); } }
添加效果:
總結(jié)
到此這篇關(guān)于Java 在PDF中添加條形碼的兩種方法的文章就介紹到這了,更多相關(guān)java pdf 條形碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 大小寫最快轉(zhuǎn)換方式實(shí)例代碼
這篇文章主要介紹了Java 大小寫最快轉(zhuǎn)換方式實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07Spring中的@Scheduled定時(shí)任務(wù)注解詳解
這篇文章主要介紹了Spring中的@Scheduled定時(shí)任務(wù)注解詳解,要使用@Scheduled注解,首先需要在啟動(dòng)類添加@EnableScheduling,啟用Spring的計(jì)劃任務(wù)執(zhí)行功能,這樣可以在容器中的任何Spring管理的bean上檢測(cè)@Scheduled注解,執(zhí)行計(jì)劃任務(wù),需要的朋友可以參考下2023-09-09Spring Boot中整合Spring Security并自定義驗(yàn)證代碼實(shí)例
本篇文章主要介紹了Spring Boot中整合Spring Security并自定義驗(yàn)證代碼實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04JavaFX實(shí)現(xiàn)UI美觀效果代碼實(shí)例
這篇文章主要介紹了JavaFX實(shí)現(xiàn)UI美觀效果代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Spring七大事務(wù)傳遞機(jī)制深入分析實(shí)現(xiàn)原理
實(shí)際項(xiàng)目開發(fā)中,如果涉及到多張表操作時(shí),為了保證業(yè)務(wù)數(shù)據(jù)的一致性,大家一般都會(huì)采用事務(wù)機(jī)制,好多小伙伴可能只是簡單了解一下,遇到事務(wù)失效的情況,便會(huì)無從下手,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)傳遞機(jī)制的相關(guān)資料,需要的朋友可以參考下2023-03-03Java實(shí)現(xiàn)簡單控制臺(tái)版ATM系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單控制臺(tái)版ATM系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05