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();
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.
barcode after (29.6 KB)


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:
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");
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.

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);"\\Downloads\\emftestbarcode.emf", BarCodeImageFormat.EMF);

(BufferedImage → EMF not working, baos is empty)

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


@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 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


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

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