0

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.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
venkata
  • 3
  • 1
  • 3

1 Answers1

0

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);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • Thank you so much for your help. As per the line1 from your code, still the canvas1 object is using the output stream where the outputstream intern being created by outputfile. – venkata Jul 05 '16 at 16:11
  • @venkata Sorry! I missed that. I have adjusted my answer, there's another constructor. – Tilman Hausherr Jul 05 '16 at 16:22
  • I have found the another constructor. Is there any way i can make barcode in horizontal mode? – venkata Jul 05 '16 at 16:43
  • @venkata Yes, with an AffineTransform as parameter, see here: https://stackoverflow.com/questions/26119794/apache-pdfbox-rotate-pdimagexobject – Tilman Hausherr Jul 05 '16 at 16:45
  • Yes. Tilman now barcode image is in horizantal mode. you saved my day. – venkata Jul 05 '16 at 17:06