We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Generate a PDF471 Barcode from byte array in Java

I need to generate a PDF471 Barcode from a byte array and not from text.
How can this be done in Java?

There exists a solution in .NET:
https://forum.aspose.com/t/generate-a-pdf471-barcode-from-byte-array/229207

Unfortunately this does not work in Java.

The basic way does not function:

String content = new String(data.readAllBytes(), StandardCharsets.UTF_8);

This ends up with additional bytes, which will cause an exception in the BarcodeGenerator like
Maximum characters limit reached for BINARY compaction mode and 2 error correction level.
Encoded data length - 1345. Max available length - 1102.” - Where the initial byte array length is 782 - largely below the limit…

Doing something like:
byte[] dataBA = data.readAllBytes();
char[] charEncoded = new char[dataBA.length];
for (int i = 0; i < dataBA.length; ++i)
{
charEncoded[i] = (char)dataBA[i];
}
String content = new String(charEncoded);

doesn’t help either. The “problem” seems to be in the constructor of String.

@ThoKoc,

Could you please share your complete (runnable) sample code that are using in Java to reproduce the exception, we will check it soon.

Hi,

J1PMH77HBfFOtaTc.png (8.16 KB)

AsposeJavaTest.zip (2.54 KB)

Concerning post above by Thomas_Koch:

From debugging see the difference when stopping at line 23:

see picture in upper mail post

The content string is blown up. Seems to be utf-16.
By consequence the content will be too big for data in normal use.
(of course: the hello world functions, as always…)

Thanks for the samples.

We have logged a ticket with an id “BARCODEJAVA-1334” for your issue/requirements. We will investigate and look into the details and try to sort it out.

Once we have an update on it, we will let you know here.

Additional information about the byte array:
The byte array contains 10 leading bytes with an identification number and a zip. Byte 10 and 11 have the value 80 and 75, makes PK which is the start marker for the zip.
The content of the zip is:
Hello world!

@ThoKoc,

Thanks for sharing the additional information.

I have logged it with your existing ticket. This may help to evaluate your issue thoroughly.

Changing the charset seems to solve the problem:

String content = new String(data.readAllBytes(), StandardCharsets.ISO_8859_1);

@ThoKoc,

Thanks for sharing the workaround and good to know that your issue is sorted out by changing the charset. Should we close the thread/ticket or you need us to continue to evaluate the issue?

The issue can be closed.

@ThoKoc,

I have closed the topic/thread for your needs now.