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

Free Support Forum - aspose.com

Improving CODE39 barcode rendering in PDF

Dear Aspose Support,
I’m having some issues trying to configure the output rendering of a CODE39 barcode in a PDF starting from a DOCX template.
I already attached for reference a test case which includes:

  • Word file with a DISPLAYBARCODE field
  • The simplified Java for taking the word and converting it to PDF
  • A simplified TestBarCodeGenerator class with some of my configurations.TestBarCodes.zip (13.5 KB)

My questions are:

  1. Barcode width: I’ve tryed many parameters combinations but I cannot manage to obtain a rendered barcode (in PDF) with (approximately) the same width as the barcode rendered by Word. This is my high priority issue, since I’m including barcodes within templates with “busy” layout and a 1cm difference in barcode width breaks things.
  2. Image quality: word rendering looks sharper then in the generated PDF. This can be seen on screen but even better if you print out the examples. Not blocking cause barcodes can be read on PDF, but I ask if there is some configuration that can improve output quality.
  3. Aspect ratio: I was not able to achieve in PDF the same height/width dimensions as Word’s rendering. My priority is to have the width same as in Word, but just wondering why how can I possibly have more control on aspect ratio.
  4. Text caption style: The font size property for the text caption (below the barcode) is apparently ignored. This is low priority as I can add my own text with some manual image manipulation; I’m just curious to know if I’m missing something and if there are parameters to also make the caption look similar to the one rendered by Word (e.g. augmented spacing between characters).

Thanks in advance for your attention,
Kind regards,
FV

@fabrizio.vaglia,
We are analyzing your requirements and will share our feedback after detailed analysis.

@fabrizio.vaglia,
We have reviewed your requirement but we need to investigate it more. This requirement is logged in our database for detailed analysis. You will be notified here once any update is ready for sharing.

This requirement is logged as:
BARCODEJAVA-1213 - Generate identical barcode as read by Aspose.Words

We analyzed the questions and prepared the feedback:
1. Barcode width: I’ve tryed many parameters combinations but I cannot manage to obtain a rendered barcode (in PDF) with (approximately) the same width as the barcode rendered by Word. This is my high priority issue, since I’m including barcodes within templates with “busy” layout and a 1cm difference in barcode width breaks things.

I propose to estimate the quality of image derived from method BarcodeGenerator.generateBarcodeImage().
If you’re satisfied with quality of BufferedImage object, then we should collaborate with Aspose.Word team to found cause of issue.

2.mage quality: word rendering looks sharper then in the generated PDF. This can be seen on screen but even better if you print out the examples.
Not blocking cause barcodes can be read on PDF, but I ask if there is some configuration that can improve output quality.

You can try to increase the size and change the font style of text.
Actually, the investigation task regarding the quality of text was created.

3.Aspect ratio: I was not able to achieve in PDF the same height/width dimensions as Word’s rendering. My priority is to have the width same as in Word, but just wondering why how can I possibly have more control on aspect ratio.

Method baseGenerationParameters.getBarcode().getBarHeight() influence height of barcode bars.
So to adjust the width and height of Barcode image, you should use following:

  1. Parameter BaseGenerationParameters.setAutoSizeMode() should be AutoSizeMode.NEAREST or AutoSizeMode.INTERPOLATION.
  2. Use next parameters to change image width and height:
    baseGenerationParameters.getImageHeight().setMillimeters(100);
    baseGenerationParameters.getImageWidth().setMillimeters(300);

4.Text caption style: The font size property for the text caption (below the barcode) is apparently ignored. This is low priority as I can add my own text with some manual image manipulation; I’m just curious to know if I’m missing something and if there are parameters to also make the caption look similar to the one rendered by Word (e.g. augmented spacing between characters).

Option baseGenerationParameters.getCaptionBelow() is intended for configuring additional caption. For this purpose you should use it in such way:

      baseGenerationParameters.getCaptionBelow().getFont().setStyle(FontStyle.UNDERLINE);
      baseGenerationParameters.getCaptionBelow().setVisible(true);

But for your case you should utilize option:

baseGenerationParameters.getBarcode().getCodeTextParameters():
baseGenerationParameters.getBarcode().getCodeTextParameters().setFontMode(FontMode.MANUAL);
baseGenerationParameters.getBarcode().getCodeTextParameters().getFont().getSize().setPixels(20);

This is a proper example of code as a whole (without using Word):

    String barcodeValue = "12345";
    String symbolHeight = "1000";
    SymbologyEncodeType encodeType = EncodeTypes.CODE_39_STANDARD;
    BarcodeGenerator generator = new BarcodeGenerator(encodeType);
    generator.setCodeText(barcodeValue);

    BaseGenerationParameters genParams = generator.getParameters();
    genParams.getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE);
    genParams.getBarcode().getCodeTextParameters().setLocation(CodeLocation.BELOW);
    genParams.getCaptionAbove().setText("");

     float scale = 1.0f;
    float xdim = 1.0f;

    scale = 0.82f;
    xdim = 0.2f;
    
    //TODO For For customizing the barcode aspect ratio
    genParams.setAutoSizeMode(AutoSizeMode.INTERPOLATION);
    generator.setCodeText('' + barcodeValue + '');

    genParams.getBarcode().getPadding().getLeft().setMillimeters(0);
    genParams.getBarcode().getPadding().getRight().setMillimeters(0);
    genParams.getBarcode().getPadding().getTop().setMillimeters(0);
    genParams.getBarcode().getPadding().getBottom().setMillimeters(0);

    // TODO For customizing the width and height of barcode image use genParams.getImageHeight() and  genParams.getImageWidth()
    genParams.getImageHeight().setMillimeters(100);
    genParams.getImageWidth().setMillimeters(300);
    genParams.getBarcode().getXDimension().setMillimeters(xdim);
    
    // TODO For customizing the barcode codetext style use genParams.getBarcode().getCodeTextParameters() instead genParams.getCaptionBelow()
    genParams.getBarcode().getCodeTextParameters().setFontMode(FontMode.MANUAL);
    genParams.getBarcode().getCodeTextParameters().getFont().getSize().setPixels(20);
    genParams.getBarcode().getCodeTextParameters().getFont().setStyle(FontStyle.STRIKEOUT | FontStyle.UNDERLINE);// TODO instead 12


    // TODO genParams.getCaptionBelow() is additional caption. If you want to use CaptionBelow, set genParams.getCaptionBelow().setVisible(true);
    // genParams.getCaptionBelow().getFont().setStyle(FontStyle.UNDERLINE);
    // genParams.getCaptionBelow().setVisible(true);

    BufferedImage im = generator.generateBarCodeImage();