这篇文章将为大家详细讲解有关使用Java怎么在PDF中添加条形码,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
方法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");
}
}
1.SpringMVC,Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架。2.Shiro,Apache Shiro是Java的一个安全框架。3.Mybatis,MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。4.Dubbo,Dubbo是一个分布式服务框架。5.Maven,Maven是个项目管理和构建自动化工具。6.RabbitMQ,RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。7.Ehcache,EhCache 是一个纯Java的进程内缓存框架。
关于使用Java怎么在PDF中添加条形码就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。