how do I customize barcode width?

I've seen some examples in forum, but I can't resolve the issue. It only change with the WideBarRatio property, but it isn't valid.
Always I want the width to be equal, 8cm; but... depending on text, barcode have a different width.

I'm using Code128 and Ean128.

thx a lot

Comments

  • edited 11:09AM
    I've seen some examples in forum, but I can't resolve the issue. It only change with the WideBarRatio property, but it isn't valid.
    Always I want the width to be equal, 8cm; but... depending on text, barcode have a different width.

    I'm using Code128 and Ean128.

    thx a lot

    Depending on how much data is placed inside the barcode, it grows. I did something like this. It is not perfect but works in the situations where I needed fixed width barcodes. Important remark, do not make the required width too small or the barcode will not be scannable:
    TLocusBarcodeView = class(TfrxBarCodeView)
      private
        FRequiredWidth: extended;
      private
        procedure CalculateZoom;
      protected
        procedure setRequiredWidth(const aValue: extended);
      public
        constructor Create(AOwner: TComponent); override;
        procedure GetData; override;
      published
        property RequiredWidth: extended read FRequiredWidth write SetRequiredWidth;
      end;
    
    constructor TLocusBarcodeView.Create(AOwner: TComponent);
    begin
      inherited;
    
      FRequiredWidth := 0.0;
    end;
    
    
    procedure TLocusBarcodeView.CalculateZoom;
    
      procedure Calculate;
      var
        r : TfrxRect;
        RealWidth: extended;
      begin
        r := GetRealBounds;
        RealWidth := (r.Right - r.Left);
        if (RealWidth > 0.01) and (CompareValue(RealWidth, RequiredWidth, 0.01)<>EqualsValue) then
        begin
          Zoom := RequiredWidth/RealWidth;
        end;
      end;
    
    begin
      Calculate;
    end;
    
    
    procedure TLocusBarcodeView.GetData;
    
    function AddLeadingZeroes(const aNumber: string; const Length : integer) : string;
    begin
      try
        Result := Format('%.*d', [Length, strtoint(aNumber)]);
      except
        Result := aNumber;
      end;
    end;
    
    begin
      inherited;
    
      case BarType of
        bcCodeEAN8:
        begin
          Text := AddLeadingZeroes(Text,8);
        end;
      end;
    
      if (RequiredWidth > 0.0) then
      begin
        Zoom := 1.0;
        CalculateZoom;
      end;
    end;
    
    
    procedure TLocusBarcodeView.setRequiredWidth(const aValue: extended);
    begin
      FRequiredWidth := aValue;
    end;
    

Leave a Comment