我想從barcode4j庫(code128bean,其他條碼豆)生成條碼,並嘗試添加到現有的PDF。條形碼圖像使用下面的代碼在本地創建。如何使用pdfbox(1.8.12)將barcode4j庫添加到現有的pdf中Code128 Barcode圖像?
//Create the barcode bean
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f/dpi)); //makes the narrow bar
//width exactly one pixel
//bean.setCodeset(2);
code128Bean.doQuietZone(false);
//Open output file
File outputFile = new File("D:/barcode4jcod128.png"); //I dont want to create it
OutputStream code128Stream = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
code128Stream, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
code128Bean.generateBarcode(canvas1, "123456");
//Signal end of generation
canvas1.finish();
} finally {
code128Stream.close();
}
- 我的問題是,我不希望創建一個圖像,並保存到本地文件系統中,然後將其作爲圖像添加到PDF。我只是想創建動態我的意思是動態創建條碼圖像並將其添加到PDF。
- 如何設置頁面大小的(如
PDPage.PAGE_SIZE_A4
),而我從catalog.getAllPages()
方法檢索,像(List<PDPage> pages = catalog.getAllPages();
)
有人可以在此幫助現有PDPages?
非常感謝你的幫助Tilman。這是我做的
public static BufferedImage geBufferedImageForCode128Bean(String barcodeString) {
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f/dpi)); //makes the narrow bar
code128Bean.doQuietZone(false);
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
//Generate the barcode
code128Bean.generateBarcode(canvas1, barcodeString);
return canvas1.getBufferedImage();
}
// main code
PDDocument finalDoc = new PDDocument();
BufferedImage bufferedImage = geBufferedImageForCode128Bean("12345");
PDXObjectImage pdImage = new PDPixelMap(doc, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(
finalDoc, pdPage, true, true, true
);
contentStream.drawXObject(pdImage, 100, 600, 50, 20);
contentStream.close();
finalDoc.addPage(pdPage);
finalDoc.save(new File("D:/Test75.pdf"));
條碼正在創建,但它是以垂直方式創建的。我想以水平方式看到。再次感謝你的幫助。
這是kindof無糖否,編輯問題以添加新問題。此外,無論如何,我在評論中回答了這個問題。 –