PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 6:29 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Barcode Text Location
PostPosted: Thu Apr 01, 2010 1:27 pm 
Offline

Joined: Thu Apr 01, 2010 12:44 pm
Posts: 4
I've been looking for something similar to pdfsharp for a while for a project at work. It fits almost perfectly (no spot color as yet) but it can embed other pdf documents and is much easier to work with for what I need it to do. The only problem I was having was with text position in barcodes.

In the code I downloaded it only allows for the text to be at the bottom of the barcode or to remove it but if you increased the font it overlaped the bars. So I set about fixing it and came up with the solution below which works with the code39 and 2of5 types.

The fix replaces two routines in ThickThinBarcodeRenderer.cs, the replacements are:

Code:
    internal void RenderBar(BarCodeRenderInfo info, bool isThick)
    {
      double barWidth = GetBarWidth(info, isThick);
      double height = Size.Height;
      double XPos = info.CurrPos.X;
      double YPos = info.CurrPos.Y;

        switch (TextLocation)
        {
            case TextLocation.AboveEmbedded:
                height -= info.Gfx.MeasureString(this.Text, info.Font).Height;
                YPos += info.Gfx.MeasureString(this.Text, info.Font).Height;
                break;
            case TextLocation.BelowEmbedded:
                height -= info.Gfx.MeasureString(this.Text, info.Font).Height;
                break;
        }

      XRect rect = new XRect(XPos, YPos, barWidth, height);
      info.Gfx.DrawRectangle(info.Brush, rect);
      info.CurrPos.X += barWidth;
    }


and

Code:
    internal void RenderText(BarCodeRenderInfo info)
    {
      if (info.Font == null)
        info.Font = new XFont("Courier New", Size.Height / 6);
      XPoint center = info.Position + CodeBase.CalcDistance(this.anchor, AnchorType.TopLeft, this.size);

      switch (TextLocation)
      {
          case TextLocation.Above:
              center = new XPoint(center.X, center.Y - info.Gfx.MeasureString(this.Text, info.Font).Height);
              info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.TopCenter);
              break;
          case TextLocation.AboveEmbedded:
              info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.TopCenter);
              break;
          case TextLocation.Below:
              center = new XPoint(center.X, info.Gfx.MeasureString(this.Text, info.Font).Height + center.Y);
              info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.BottomCenter);
              break;
          case TextLocation.BelowEmbedded:
              info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.BottomCenter);
              break;
      }
    }


I used measurestring as I found that font.height produced too big a gap and I just didn't like how it looked, the disadvantage is the increased time to render a barcode as its called each time a bar is rendered.

With these two routines in place TextLocation works as it is described. Above and Below place the text outside of the barcode area so a barcode height of 10mm is still 10mm. AboveEmbedded and BelowEmbedded place the text within the confines of the barcode area and decreases the height of the bars accordingly.


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 01, 2010 2:46 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Thank you for the feedback.
I haven't tried your patch yet, but it's on our task list for the next version.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 368 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group