PDF417 Barcode Generate in Editable PDF

212 Views Asked by At

I am working on the use case where end user fill the pdf document and click the push button to generate the barcode with field values… I am looking for the solution in PDFBox or IText7 or Aspose….

I have created the editable pdf document using the above mentioned references.

I have tried in itext7 and PDFBox but no luck

2

There are 2 best solutions below

0
On

An Adobe Acrobat Reader user can easily import a barcode by several means. However the simplest to program will possibly be via an external import as a comment that they can drag about the page as here.

Attempting to generate such codes inside Acrobat is possible but highly problematic for 2D or QRcodes. (Dynamic Forms methods involving complex scripting should be avoided as depreciating)

enter image description here

The import can be done unattended programmatically same as all form field generations, using the same text file to invoke Acrobat reader.

%FDF-1.2
%âãÏÓ
1 0 obj
<</FDF<</Annots[2 0 R 3 0 R]/F(java - PDF417 Barcode Generate in Editable PDF - Stack Overflow.pdf)/ID[<><>]/UF(java - PDF417 Barcode Generate in Editable PDF - Stack Overflow.pdf)>>/Type/Catalog>>
endobj
2 0 obj
<</AP<</N 4 0 R>>/C[1 0 0]/CreationDate(D:2023)/F 4/IT/Stamp/M(D:20231030005210Z)/NM(417C0DE1-0000-0000-417C0DE100000001)/Name/MyBarCode1/Page 0/Popup 3 0 R/Rect[77.8321 481.059 317.832 562.059]/Subj(BarCode00000001)/Subtype/Stamp/Type/Annot>>
endobj
3 0 obj
<</F 28/Open false/Page 0/Parent 2 0 R/Rect[594.96 467.0 798.96 581.0]/Subtype/Popup/Type/Annot>>
endobj
4 0 obj
<</BBox[0 0 240 81]/Length 29/Resources<</XObject<</Im0 5 0 R>>>>/Subtype/Form/Type/XObject>>stream
q 240 0 0 81 0 0 cm /Im0 Do Q
endstream
endobj
5 0 obj
<</BitsPerComponent 8/ColorSpace/DeviceGray/Height 9/Length 1080/Subtype/Image/Type/XObject/Width 120>>stream
        ÿ ÿ ÿ ÿÿÿ     ÿ ÿ ÿ     ÿÿ  ÿ ÿ  ÿÿÿ   ÿÿÿÿ   ÿÿÿÿ ÿ ÿÿÿ   ÿ ÿÿ ÿÿ  ÿÿÿÿ  ÿÿÿ     ÿ ÿ ÿ     ÿÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ    ÿ ÿ ÿÿÿÿ ÿÿÿÿ ÿ   ÿ  ÿÿ      ÿ  ÿÿ ÿ     ÿ   ÿÿ    ÿ    ÿ    ÿ ÿ    ÿ ÿ ÿÿ ÿÿÿÿÿÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ ÿ ÿ ÿÿ    ÿÿÿÿÿÿ  ÿÿ     ÿ  ÿ  ÿÿ ÿ      ÿÿ   ÿ ÿÿ      ÿ ÿÿÿ  ÿ ÿÿ ÿ ÿ ÿÿÿÿ    ÿÿÿÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ  ÿ ÿ    ÿÿ     ÿ ÿÿÿÿ  ÿ    ÿ   ÿ     ÿ ÿÿ ÿ     ÿ ÿÿÿÿÿ ÿ ÿÿÿ ÿÿÿÿ  ÿ ÿ    ÿÿ     ÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ  ÿ ÿ   ÿÿÿÿÿ ÿÿÿ ÿ ÿ     ÿÿ  ÿÿÿÿ    ÿ ÿ  ÿÿ  ÿÿÿÿ ÿÿÿÿÿ ÿ    ÿ ÿÿÿ    ÿ ÿ   ÿÿ   ÿÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ     ÿ ÿ    ÿ  ÿÿ   ÿ   ÿÿÿ ÿ    ÿ     ÿÿ ÿ   ÿ ÿÿÿ  ÿÿÿ     ÿÿ ÿÿ ÿ    ÿ ÿ     ÿ  ÿÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ   ÿ ÿÿ   ÿ     ÿ ÿÿÿÿ  ÿÿÿ  ÿÿ ÿÿ   ÿÿ  ÿÿÿÿ ÿÿ  ÿ  ÿÿÿÿÿ ÿÿÿÿ ÿ  ÿ   ÿ ÿÿ   ÿ     ÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ      ÿ ÿÿ ÿ   ÿÿ ÿÿ  ÿ     ÿÿ ÿÿÿ  ÿÿ  ÿ    ÿÿ ÿÿÿ ÿÿÿ    ÿ ÿÿ    ÿ ÿ ÿ      ÿ   ÿÿÿ       ÿ ÿÿÿ ÿ ÿÿ         ÿ ÿ ÿ ÿÿÿ      ÿ ÿÿ  ÿÿ ÿÿ ÿ  ÿÿÿÿ ÿÿÿÿ   ÿ  ÿÿ ÿ      ÿÿÿ ÿ ÿÿ   ÿÿÿÿÿ ÿ  ÿÿ     ÿ ÿÿ   ÿ ÿÿÿ       ÿ ÿÿÿ ÿ ÿÿ 
endstream
endobj
trailer
<</Root 1 0 R>>
%%EOF

The biggest programming hurdle with this approach is how to program the image as for black and ÿ for white, based on data content.

Have a play with this data file by changing the filenames and other values then a double click on TEXT.FDF should get Reader to open the two files together, such that the PDF wraps around the image.

You can also modify the text file while PDF is open with a different barcode and the second will replace the first in front of your eyes !! Just beware a wrong image will wipe out the first and throw an error message. enter image description here

So just to prove it works. I shuffled the pixels without any other messing about and we get my bad pixel layout, you of course will use a correct altered image.

enter image description here

0
On

I don't think that these requirements can be achieved using Aspose.PDF directly. You can specify a JavaScript method behind a button to trigger and then Adobe Reader executes that action using its own engine/debugger.

You cannot tell Adobe Reader to use Aspose.Barcode to generate a barcode image and add this in the PDF.

There can be a workaround that can be tried. I am not sure if it is going to work in all cases but its worth a try. i.e.

  • Create a separate console application that produces Barcode using Aspose.Barcode or any other API and save it to some physical path.
  • Create a JavaScript action in PDF for the button that can invoke/execute the barcode application/.exe to generate a barcode image
  • Display the image inside the PDF by reading it from a physical path

Code to generate barcode image using Aspose.Barcode:

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.Pdf417, "Åspóse.Barcóde©");
gen.Parameters.Barcode.XDimension.Pixels = 2;
gen.Parameters.Barcode.Pdf417.Columns = 3;
gen.Save("Pdf417Basic.png", BarCodeImageFormat.Png);

Code to specify a JavaScript Action behind PDF button using Aspose.PDF:

var userInput = this.getField("userInput").value; // Get user input from the text field

// Execute your console application with userInput as an argument
// You can use the 'app.execMenuItem' function to execute an external command

// For example:
var result = app.execMenuItem("App.exec", "your_console_app.exe " + userInput);

if (result == 0) {
    // Success: The console application executed
    // Now, you can add the generated image to the PDF
    var imageRect = this.getPageBox("Crop", this.pageNum);
    this.addImage({
        cName: "GeneratedImage",
        nPage: this.pageNum,
        rCrop: imageRect,
        cPath: "path_to_generated_image.jpg"
    });
}

Above JS Action can be added behind the button using:

field.Actions.OnPressMouseBtn = new JavascriptAction(opendocscript);

You can try above workaround. I hope that it will help you. In case you face any issues, please create a post in official Aspose support forum where you will be assisted accordingly. This is Asad Ali and I am developer evangelist at Aspose.