Place BarCode in existing PDF file


#1

I would like to know if it is possible to place a Barcode on an existing PDF form.
Perhaps there may be a sample code, please let me know.

Here is my project:

  1. I have several fillable PDF forms files in my folder
  2. i need to open one at the time and place a BarCode (whichs represents the account# and state)
  3. then save the PDF.

My Environment: Windows 10, Eclipse 2019, Aspose.total for Java

Thank you very much in advance.
Marc


#2

@mromero_rubinrothman_com,
Your requirement can be easily fulfilled using Aspose.Barcode and Aspose.PDF as follows:

  1. Generate barcode using Aspose.Barcode as per your requirement and save it as an image.
  2. Open the existing PDF using Aspose.PDF
  3. Place the image in the existing PDF and save the file

You may please give a try to the sample code in the above mentioned articles and share your feedback.


#3

Hello Aspose tech,
Can you please provide a link of code snippet, On how to add a barcode to an existing PDF file, previously you provided a link but i could not use it, i need to create the barcode on the fly in memory then insert into an existing PDF file, i look everywhere in aspose but can not find any sample. please help.
thank you in advance,
Marc


#4

@mromero_rubinrothman_com,
I have created below code snippet which is combination from suggested documents. Let us know your feedback.

ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(EncodeTypes.GS_1_CODE_128);
String fileFolder = "D:\\barcode\\";
String filename = "GS_1_CODE_128.png";
SetBarCodeGenerator(barCodeGenerator, 0, 1f, 0f, 0f, false);
barCodeGenerator.getCodeTextStyle().setLocation(CodeLocation.BELOW);                
barCodeGenerator.setCodeText("(01)30012345678906");
try
{
    barCodeGenerator.save(imageStream, BarCodeImageFormat.PNG);
}
catch (Exception ex)
{
}

byte[] bytes = imageStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
Document pdfDocument = new Document(fileFolder + "Test.pdf");
// create image stamp
ImageStamp imageStamp = new ImageStamp(inputStream);
imageStamp.setBackground(true);
imageStamp.setXIndent(100);
imageStamp.setYIndent(100);
imageStamp.setHeight(123);
imageStamp.setWidth(214);
// add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(imageStamp);
// save output document
pdfDocument.save(fileFolder + "Stamp_output.pdf");

private static void SetBarCodeGenerator(BarCodeGenerator barCodeGenerator, float width, float height, float left, float right, boolean setWidth)
{
    barCodeGenerator.setAutoSizeMode(AutoSizeMode.INTERPOLATION);
    barCodeGenerator.getBarCodeHeight().setInches(height);
    barCodeGenerator.getMargins().getLeft().setPixels(left);
    barCodeGenerator.getMargins().getRight().setPixels(right);

    if (setWidth)
        barCodeGenerator.getBarCodeWidth().setInches(width);
}

#5

Hello Ahsandiq,
I tried the sample code you provided it is great however, label (letters below the bar-code) are very small.
can you please let me know how can i increase the size of this caption. (see attached sample)
thank you very much,
Marcos

bc212_StampOutput.pdf (372.3 KB)


#6

@mromero_rubinrothman_com,
You may please visit the following article where caption can be configured by setting the font. Please give it a try in your scenario and share the feedback.

Manage the Barcode Caption