【教程】将Java条形码添加到PDF文档的两种方法
来源:互联网 发布:淘宝联盟点击赚钱 编辑:程序博客网 时间:2024/06/04 18:48
jPDFProcess是用于PDF文件的Java库,可用作向用户传递自定义PDF内容或者对引入的PDF内容进行处理和操作。本教程演示了使用jPDFProcess将Java条形码添加到PDF文档的两种方法。
关联产品
- jPDFProcess
通过jPDFProcess,将PDF文档添加到PDF文档中的方法:
方法1
使用条形码字体,其字符看起来像条形码,然后使用此字体向文档添加文本。当显示文档时,它将显示条形码(使用字体绘制),并具有将条形码值保持在文本内容中的附加优势。 这时候需要在运行的同一文件夹中的free3of9.ttf文件,该文件是TrueType Code39字体,示例程序将此字体嵌入到PDF中,然后使用它来绘制字符串。
代码示例
String barcodeMSG = "0123456789"; // Create a blank document and add a pagePDFDocument pdf = new PDFDocument();PDFPage newPage = pdf.appendNewPage(8.5 * 72, 11 * 72);Graphics2D pageG2 = newPage.createGraphics(); // Embed the fontFont code39Font = pdf.embedFont("free3of9.ttf", Font.TRUETYPE_FONT);pageG2.setFont(code39Font.deriveFont(64f));pageG2.drawString(barcodeMSG, 72, 108); // Save the documentpdf.saveDocument("barcode.pdf");
方法2
第二种方法是使用条形码库创建条形码图像,然后使用jPDFProcess将该图像绘制到PDF上。 barcode4j.jar文件是一个开放源代码库,它实现了许多条形码生成类,包括代码39.样例程序使用这个jar文件中的类来生成条形码图像,然后将图像添加到 PDF文件。
代码示例
String barcodeMSG = "0123456789"; // Create a blank document and add a pagePDFDocument pdf = new PDFDocument();PDFPage newPage = pdf.appendNewPage(8.5 * 72, 11 * 72);Graphics2D pageG2 = newPage.createGraphics(); // This code creates a barcode image using Barcode39 and then adds the image to the pageCode39Bean code39 = new Code39Bean();code39.setModuleWidth(2);code39.setBarHeight(50);code39.setWideFactor(2);BarcodeDimension dim = code39.calcDimensions(barcodeMSG); BufferedImage barcodeImage = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_RGB);Graphics2D imageG2 = barcodeImage.createGraphics();imageG2.setColor(Color.white);imageG2.fillRect(0, 0, barcodeImage.getWidth(), barcodeImage.getHeight());imageG2.setColor(Color.black);code39.generateBarcode(new Java2DCanvasProvider(imageG2, 0), "0123456789"); // Add the image to the pagepageG2.drawImage(barcodeImage, posX, posY, null); // Save the documentpdf.saveDocument("barcode.pdf");
查看原文
阅读全文
0 0
- 【教程】将Java条形码添加到PDF文档的两种方法
- 【PDF文档加密保护的两种方法】
- ppt转为pdf的两种文档转换方法
- 将word文件转换成PDF的两种方法
- 将pdf转换成jpg格式的两种方法
- 将条形码插入到word文档中
- 将图片转为PDF文档的方法
- 将view添加到UIWidow两种方式的比较
- 将XML文档写入数据库的两种方法
- 使用java将多种类型的文件如Word、PDF、JPG汇总到一个文档中(Word或者PDF)
- 开发人员在使用Aspose.Pdf for .NET时如何用C#和VB两种代码添加附件到PDF文档?
- 将图片保存到系统相册的两种方法
- 将图片保存到系统相册的两种方法
- 两种添加数据到WEB DropDownList 控件的方法
- 如何将word文档转换成pdf的方法
- 轻松将pdf转成word文档的方法
- 将Word转换成PDF格式文档的方法
- 将PDF转换成Word文档的方法
- 非法字符空字符的过滤
- CountdownTimer倒计时
- 如何操作代码托管到GitHub
- Vue2.0子父组件通信
- 产生25个int类型的随机数。对于每一个随机值,使用if-else语句来将其分类为大于、小于或等于紧随它而随机生成的值。
- 【教程】将Java条形码添加到PDF文档的两种方法
- Android vector标签 PathData 画图超详解
- 深入理解MVC
- Vlc部分功能
- Vuejs(15)——Vue的异步组件
- IntelliJ IDEA开发Spark、打JAR包
- idea使用心得
- staruml文字换行
- Linux虚拟机下载和安装