Java 在PDF中添加条形码的两种方法

所属分类: 软件编程 / java 阅读数: 41
收藏 0 赞 0 分享

条形码,是由宽度不等的多个黑条和空白所组成,用以表达一组信息的图形标识符。通过给文档添加条形码,可以直观,快捷地访问和分享一些重要的信息。本文就将通过使用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程序。(如下图)

方法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, 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");
  }
}

添加效果:

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多