简体   繁体   中英

How to add Code128 Barcode image to existing pdf using pdfbox(1.8.12) with barcode4j library?

I am trying to generate the barcode from barcode4j library(code128bean, other barcode beans) and try to add to the existing pdf. The barcode image is getting created locally using the below code.

//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();
}
  1. My problem is I don't want to create an image and save it locally in filesystem and then add it as image to pdf. I just want to create dynamically i mean just create the barcode image dynamically and add it to the pdf.
  2. How do I set the pagesize (like PDPage.PAGE_SIZE_A4 ) to the existing PDPages which I retrieved from catalog.getAllPages() method, like ( List<PDPage> pages = catalog.getAllPages(); )

Can somebody help on this?

Thank you so much for your help Tilman. Here is what i did

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"));

The barcode is getting created the but it is created in vertical manner. i would like to see in horizontal manner. Thanks again for your help.

1) add an image to an existing page while keeping the content:

BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
    dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
code128Bean.generateBarcode(canvas1, "123456");
canvas1.finish();
BufferedImage bim = canvas1.getBufferedImage();

PDXObjectImage img = new PDPixelMap(doc, bim);
PDPageContentStream contents = new PDPageContentStream(doc, page, true, true, true);
contents.drawXObject(img, 100, 600, bim.getWidth(), bim.getHeight());
contents.close();

2) set the media box to A4 on an existing page:

page.setMediaBox(PDPage.PAGE_SIZE_A4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM