关联产品html
经过jPDFProcess,将PDF文档添加到PDF文档中的方法:app
方法1ide
使用条形码字体,其字符看起来像条形码,而后使用此字体向文档添加文本。当显示文档时,它将显示条形码(使用字体绘制),并具备将条形码值保持在文本内容中的附加优点。 这时候须要在运行的同一文件夹中的free3of9.ttf文件,该文件是TrueType Code39字体,示例程序将此字体嵌入到PDF中,而后使用它来绘制字符串。字体
代码示例开放源代码
String barcodeMSG = "0123456789";
// Create a blank document and add a page
PDFDocument pdf = new PDFDocument();
PDFPage newPage = pdf.appendNewPage(8.5 * 72, 11 * 72);
Graphics2D pageG2 = newPage.createGraphics();
// Embed the font
Font code39Font = pdf.embedFont("free3of9.ttf", Font.TRUETYPE_FONT);
pageG2.setFont(code39Font.deriveFont(64f));
pageG2.drawString(barcodeMSG, 72, 108);
// Save the document
pdf.saveDocument("barcode.pdf");
方法2code
第二种方法是使用条形码库建立条形码图像,而后使用jPDFProcess将该图像绘制到PDF上。 barcode4j.jar文件是一个开放源代码库,它实现了许多条形码生成类,包括代码39.样例程序使用这个jar文件中的类来生成条形码图像,而后将图像添加到 PDF文件。htm
代码示例文档
String barcodeMSG = "0123456789";
// Create a blank document and add a page
PDFDocument 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 page
Code39Bean 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 page
pageG2.drawImage(barcodeImage, posX, posY, null);
// Save the document
pdf.saveDocument("barcode.pdf");