Java 添加条码、二维码到PDF文档

 时间:2024-10-13 16:34:16

以下经验内容介绍如何在PDF文档中添加条形码、二维码。

工具/原料

Free Spire.Office for Java (免费版)

工具下载及jar导入:

1、jar导入,可通过创建Maven程序项目,并在pom.xml中配置Maven仓库路径,并指定Free Spire.Office for Java的Maven依赖,点击“Import Changes”即可导入JAR包。(如果使用的Eclipse, 点击保存按钮导入),配置如下:<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository></repositories><dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.office.free</artifactId> <version>3.1.1</version></dependency></dependencies>另外,也可通过在E-iceblue官网下载jar包,手动导入Spire.Office.jar到Java程序。

Java代码

1、import com.spire.barcode.*; import com.spire.pdf.*; import com.spire.pdf.barcode.*; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; public class AddBarcodeInPDF { public static void main(String[] args) { //创建PdfDocument对象 PdfDocument pdf = new PdfDocument(); //添加一页 PdfPageBase page = pdf.getPages().add(); //初始化y变量 double y = 15; //创建字体 PdfFont font= new PdfFont(PdfFontFamily.Helvetica, 12,PdfFontStyle.Bold); // 绘制文本“Codebar:”到PDF,并绘制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); PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");//创建条码 codebar.setBarcodeToTextGapHeight(1f); codebar.setBarHeight(25f); 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);//绘制条码到PDF页面 y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5; //绘制文本“Code128-A:”到PDF,并绘制Code128A条码到PDF text.setText("Code128-A:"); result = text.draw(page, 0, y); page = result.getPage(); y =result.getBounds().getY()+ result.getBounds().getHeight() + 2; PdfCode128ABarcode code128 = new PdfCode128ABarcode("HELLO 00-123"); code128.setBarcodeToTextGapHeight(1f); code128.setBarHeight(25f); 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,绘制Code39条形码到PDF text.setText("Code39:"); result = text.draw(page, 0, y); page = result.getPage(); y =result.getBounds().getY()+ result.getBounds().getHeight() + 2; PdfCode39Barcode code39 = new PdfCode39Barcode("16-273849");//绘制条码 code39.setBarcodeToTextGapHeight(1f); code39.setBarHeight(25f); code39.setTextDisplayLocation(TextLocation.Bottom); code39.setTextColor(blue); point.setLocation(point.x,y); code39.draw(page, point);//绘制条码到PDF页面 //生成二维码图片,绘制到PDF页面 text.setText("QRCode:");//绘制文本“QR Code:”到PDF result = text.draw(page, 200, 0); page = result.getPage(); BarcodeSettings settings = new BarcodeSettings();//创建二维码图形 settings.setType(BarCodeType.QR_Code); settings.setData("123456789"); settings.setData2D("123456789"); settings.setX(1f); settings.setLeftMargin(0); settings.setShowTextOnBottom(true); settings.setQRCodeECL(QRCodeECL.Q); settings.setQRCodeDataMode(QRCodeDataMode.Numeric); BarCodeGenerator generator = new BarCodeGenerator(settings); Image image = generator.generateImage(); PdfImage pdfImage = PdfImage.fromImage((BufferedImage)image);//绘制二维码图片到PDF y = result.getBounds().getY()+ result.getBounds().getHeight() + 2; page.getCanvas().drawImage(pdfImage,200,y); //保存PDF文档 pdf.saveToFile("添加条码、二维码.pdf"); pdf.dispose(); } }

  • WORD中如何设置打印背景色和图像
  • Excel通过VBA实现滚动条控制图片缩放
  • 怎样炒莲米好吃?
  • Word编辑技巧:[34]设置或修改签名的方法
  • 幸福企业的五项指标
  • 热门搜索
    怀孕初期胸部胀痛是什么原因 cruise是什么意思 英雄联盟是什么游戏 report是什么意思 白子是什么 冠状病毒是什么 匿名是什么意思 皮肤黑穿什么颜色显白 抛货是什么意思 人寿年丰是什么意思