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

作者:Tina_Tang 时间:2023-05-29 19:10:23 

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

添加效果:

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

来源:https://segmentfault.com/a/1190000022137925

标签:java,pdf,条形码
0
投稿

猜你喜欢

  • Spring JDK动态 代理实现过程详解

    2023-11-16 19:42:15
  • java9迁移注意问题总结

    2022-07-19 11:26:30
  • idea 创建 maven web 工程流程(图文教程)

    2022-06-29 04:51:21
  • Android编程实现系统重启与关机的方法

    2022-01-09 20:56:49
  • java的基本数据类型及属性

    2021-08-10 19:31:49
  • 浅析Java中接口和抽象类的七大区别

    2022-01-16 21:09:36
  • Java IO之File 类详解

    2023-08-07 20:02:12
  • Springboot整合PageOffice 实现word在线编辑保存功能

    2022-12-03 22:38:18
  • c#学习之30分钟学会XAML

    2022-02-08 03:09:52
  • Java结构性设计模式中的装饰器模式介绍使用

    2022-04-08 06:30:00
  • SpringBoot通过源码探究静态资源的映射规则实现

    2022-03-26 19:05:53
  • struts2 validation.xml 验证规则代码解析

    2021-09-14 22:01:27
  • C# MVC模式中应该怎样区分应用程序逻辑(Controller层)和业务逻辑(Model层)?

    2022-06-25 12:34:55
  • C#/VB.NET实现HTML转为XML的示例代码

    2021-08-13 20:46:48
  • pagehelper插件显示total为-1或1的问题

    2021-11-04 01:02:39
  • Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 实现分库分表功能

    2023-08-28 16:52:09
  • Java持久层框架MyBatis简单实例

    2023-01-09 08:43:16
  • Java面向对象编程的三大特征

    2023-09-19 06:20:34
  • Java二维数组实现数字拼图效果

    2021-11-21 20:39:17
  • Android自定义View实现支付宝咻一咻效果

    2022-08-06 11:02:14
  • asp之家 软件编程 m.aspxhome.com