Barcode generated image is blurry after mailmerge compared to generated image

We are having issues with resolution (DPI/PPI) when mailmerging a generated barcode image.
We have tried several options but they don’t seem to work and we always generate a barcode less than 600 DPI.
aspose-words 22.5.0
aspose-barcode 23.5

Please see the sample code below and attached files for reference:

barcode settings:

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
generator.getParameters().setAutoSizeMode(AutoSizeMode.NONE);
generator.getParameters().setResolution(600f);
generator.getParameters().getBarcode().getBarHeight().setMillimeters(5f);
generator.getParameters().getBarcode().getXDimension().setMillimeters(1f);
generator.getParameters().getBarcode().getPadding().getBottom().setMillimeters(1f);
ImageIO.write(generator.generateBarCodeImage(), "png", baos);

Image callback:

DocumentBuilder builder = new DocumentBuilder(e.getDocument());
builder.moveToField(e.getField(), true);
builder.insertImage((byte[]) e.getFieldValue(), ConvertUtil.millimeterToPoint(47.3f), ConvertUtil.millimeterToPoint(8.0f));

Kindly advise if we are doing something wrong to achieve 600 DPI/PPI after mailmerge.
Thanks.
barcode after mailmerge.zip (29.6 KB)

@barcodehelpme,

I did test your scenario/case for Aspose.BarCode part. I simply used the following sample code to test it from Aspose.BarCode perspective and it works fine. The output image resolution is ok (your output barcode image “testbarcode.png” by Aspose.BarCode in the zipped archive is also fine) and I don’t see any blurry effect:
e.g.
Sample code:

com.aspose.barcode.generation.BarcodeGenerator generator = new com.aspose.barcode.generation.BarcodeGenerator(com.aspose.barcode.generation.EncodeTypes.CODE_128);
generator.setCodeText("My barcode test");
generator.getParameters().setAutoSizeMode(com.aspose.barcode.generation.AutoSizeMode.NONE);
generator.getParameters().setResolution(600f);
generator.getParameters().getBarcode().getBarHeight().setMillimeters(5f);
generator.getParameters().getBarcode().getXDimension().setMillimeters(1f);
generator.getParameters().getBarcode().getPadding().getBottom().setMillimeters(1f);
File outputfile = new File("f:\\files\\result.png");
ImageIO.write(generator.generateBarCodeImage(),"png", outputfile);

I am moving your thread thread to Aspose.Words forum where our fellow colleagues from Aspose.Words team will evaluate your issue soon.

@barcodehelpme Actually, the barcode image is inserted into the document as is, i.e. as it was generated by Aspose.BarCode. If you unzip the generated DOCX document, you will fine the same image as generated by Aspose.BarCode. It looks like image blurring is a peculiarity of MS Word UI.

Hi Amjad and Alexey,

Thanks for looking into this.
Just an additional confirmation if my understanding about DPI is correct.
Is DPI just some information in the image metadata?
I tried using GIMP to change the DPI to 600, but when it is used in MS word, it will default to 300 at max.
I also used some other code to do this and it has the same results, is it really possible to set images at 600 DPI in MS word?
I have also set the document to high fidelity to retain the original image format.

@barcodehelpme To retain the quality of barcode image on any scale it would be better to use vector image format, such as EMF or SVG instead of raster format, like PNG.
https://docs.aspose.com/barcode/java/generate-barcodes/

Aspose.Words supports inserting both EMF and SVG images into the document.

Hi Alexey,

Thank you for the suggestion, is there a way to generate the barcode as EMF without saving it?

@barcodehelpme You can save the generated barcode image as EMF into a stream and insert it into the document.

Hi Alexey,

Apologies but I am not familiar with how EMF works especially with barcode generation, could you show me a snippet on how do I produce the barcode EMF without saving it?

(working, but it physically saves the file)

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128);
generator.save("\\Downloads\\emftestbarcode.emf", BarCodeImageFormat.EMF);

(BufferedImage → EMF not working, baos is empty)

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(generator.generateBarCodeImage(), "emf", baos);

Thanks.

@barcodehelpme I will move your request into Aspose.Total category, my colleagues from Aspose.BarCode team will help you shortly.
I think, in your case you should use BarcodeGenerator.save overload that takes output stream as a parameter.

Hi Alexey,

Yes you are right, I have tried it and it worked.
I believe that this has solved our issue.

Thanks for the support, highly appreciated!

1 Like

@barcodehelpme,

It is nice to know that your issue is sorted out.

Also, as @alexey.noskov suggested, you may use the overload BarcodeGenerator.save that can serve for the purpose.