PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Nov 12, 2019 10:48 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Wed Jun 09, 2010 3:18 pm 
Offline

Joined: Wed Jun 09, 2010 2:40 pm
Posts: 3
I've been using MigraDoc and PDFSharp for a while now and it's just great.

Though I was a bit dissapointed when I discovered I could not render barcodes, even though PDFSharp has barcode implementations.
As I had to include barcode in my project I created an implementation of BarcodeRenderer.
And of course I would like to share this with the rest of the community.

I don't know why a BarcodeRenderer hasn't been implemented so far, if there's any known issues or traps that I'm unaware of.
Or just simply it hasn't been done yet.

This implementation is pretty basic and there's probably much more one can add. It suits my needs, and this implementation was scratched together within a hour.
If anyone has a example of features needed to be implemented or a bunch of testcases, I'd be happy to try to find the time to implement it.


Also, is there a policy which DocumentObject can parent other elements?
I havn't added any "AddBarcode()" methods in MigraDoc.DocumentObjectModel for this reason.
This patch have been limited to the changes in the MigraDoc.Rendering namespace.

Regards, Adam Brengesjö


Attachments:
File comment: patch and individual files
BarcodeRender.zip [8.5 KiB]
Downloaded 1457 times


Last edited by AdamB on Wed Jun 09, 2010 9:06 pm, edited 1 time in total.
Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 09, 2010 8:11 pm 
Offline

Joined: Thu Feb 25, 2010 2:44 pm
Posts: 14
What exactly do you mean by "could not render barcodes"? My co-worker wrote some code that puts a 3-of-9 barcode into a PDF. Now, it uses a 3-of-9 font, so I wonder if you're talking about rasterizing, i.e., emiting the barcode as a graphic. Or perhaps you're talking about 2D barcodes? Or are you talking about viewing PDFs as opposed to creating them? Sorry, but I'm confused.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 09, 2010 8:57 pm 
Offline

Joined: Wed Jun 09, 2010 2:40 pm
Posts: 3
By rendering I mean that MigraDoc does not call the drawing methods in PdfSharp.
The PdfSharp library's Barcode features are working fine. It's just that MigraDoc does not make use of them.

Using MigraDoc 1.31.3066.0, I can add a Barcode element to the document.
But MigraDoc does not draw the Barcode elements on the PDF-document.

I have never had trouble drawing barcode on PDF:s using the PdfSharp library.
But I wanted to use the MigraDoc functionalities. And I found out that MigraDoc does not simply call the XGraphics.DrawBarCode() method.
I guess this is what you mean with rasterizing.

Do you know if your co-worker used MigraDoc or called the PdfSharp directly?

This does not work as predicted:
Code:
Document doc = new Document();
Section sec = doc.AddSection();
sec.AddParagraph("This is the first line");

Barcode b = sec.Elements.AddBarcode();
b.Type = BarcodeType.Barcode39;
b.Text = "0123456789";
b.Width = "5cm";
b.Height = "1cm";
b.BearersBars = true;
b.LineHeight = 10;
b.LineRatio = 2;

// In MigraDoc 1.31.3066.0 this line is directly below the first line.
// No barcode has been drawn on the PDF document.
// Using my patch the barcode is not ignored.
sec.AddParagraph("This line should be below a barcode");

PdfDocumentRenderer pdf = new PdfDocumentRenderer();
pdf.Document = doc;
pdf.RenderDocument();

String filename = @"C:\test.pdf";
pdf.Save(filename);

System.Diagnostic.Process.Start(filename);


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 09, 2010 9:03 pm 
Offline

Joined: Thu Feb 25, 2010 2:44 pm
Posts: 14
Okay, I understand now: this is all about MigraDoc, which we don't use.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 09, 2010 9:07 pm 
Offline

Joined: Wed Jun 09, 2010 2:40 pm
Posts: 3
Yes. I have updated the subject of this thread to clarify that fact :)


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 22, 2011 6:07 pm 
Offline

Joined: Tue Mar 22, 2011 1:00 pm
Posts: 2
Adam,

Thanks for rendering code!!!

When I try to add a bar code an error. Any Ideas why or how to fix it?

Thanks

--Doug

Error
Code:
System.ArgumentException was unhandled
  Message="'' is not a valid code for a 3 of 9 standard bar code."
  Source="PdfSharp"
  StackTrace:
       at PdfSharp.Drawing.BarCodes.Code3of9Standard.CheckCode(String text)
       at PdfSharp.Drawing.BarCodes.CodeBase.set_Text(String value)
       at MigraDoc.Rendering.BarcodeRenderer.Render()
       at MigraDoc.Rendering.DocumentRenderer.RenderPage(XGraphics gfx, Int32 page, PageRenderOptions options)
       at MigraDoc.Rendering.PdfDocumentRenderer.RenderPages(Int32 startPage, Int32 endPage)
       at MigradocBarCodeTest.HelloWorld.Programm.Main() in N:\PDFSharp-MigraDocFoundation-1_31\MigradocBarCodeTest\Module1.vb:line 46
  InnerException:




Here is the code I'm using.

module1.vb
Code:
Imports System.Diagnostics
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering
Imports MigraDoc.RtfRendering
Imports PdfSharp.Pdf

Namespace HelloWorld
    ''' <summary>
    ''' This sample is the obligatory Hello World program for MigraDoc documents.
    ''' </summary>
    Class Programm
        Public Shared Sub Main()
            Dim document As Document = CreateDocument()
            document.UseCmykColor = True

            Const unicode As Boolean = False
            Const embedding As PdfFontEmbedding = PdfFontEmbedding.Always
            Dim pdfRenderer As New PdfDocumentRenderer(unicode, embedding)
            pdfRenderer.Document = document
            pdfRenderer.RenderDocument()
            Const filename As String = "HelloWorld.pdf"
            pdfRenderer.PdfDocument.Save(filename)
            ' ...and start a viewer.
            Process.Start(filename)
        End Sub

        ''' <summary>
        ''' Creates an absolutely minimalistic document.
        ''' </summary>
        Private Shared Function CreateDocument() As Document
            Dim document As New Document()
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()
            paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50)
            paragraph.AddFormattedText("Hello, World!", TextFormat.Bold)

            Dim b As Shapes.Barcode
            b = section.Elements.AddBarcode()

            b.Type = Shapes.BarcodeType.Barcode39
            b.Text = "0123456789"
            b.Width = "5cm"
            b.Height = "1cm"
            b.BearerBars = True
            b.LineHeight = 10
            b.LineRatio = 2

            Return document
        End Function
    End Class
End Namespace



Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 22, 2011 6:29 pm 
Offline

Joined: Tue Mar 22, 2011 1:00 pm
Posts: 2
OK, I Figured out the problem.

Code:
            b.Text = "0123456789"


Should Be

Code:
            b.Text = True
            b.Code = "0123456789"


Top
 Profile  
Reply with quote  
PostPosted: Thu May 03, 2012 10:28 am 
Offline

Joined: Thu May 03, 2012 10:01 am
Posts: 1
Hello everyone,

sorry for digging out this old thread, but I'm struggling with MigraDoc and barcodes, too.

I tried the sample code above and have the same problem: The barcode does not appear on the document (but the the two lines "This is the first line" and "This line should be below a barcode" appear).

Here is my code:
Code:
using System;
using System.Diagnostics;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering;

namespace MigraDocTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            Section sec = document.AddSection();
            sec.AddParagraph("This is the first line");

            Barcode b = sec.Elements.AddBarcode();
            b.Type = BarcodeType.Barcode25i;
            b.Text = true;
            b.Code = "0123456789";
            b.Width = "5cm";
            b.Height = "1cm";
            b.BearerBars = true;
            b.LineHeight = 10;
            b.LineRatio = 2;

            // In MigraDoc 1.31.3066.0 this line is directly below the first line.
            // No barcode has been drawn on the PDF document.
            // Using my patch the barcode is not ignored.
            sec.AddParagraph("This line should be below a barcode");

            //string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
            //MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            renderer.Document = document;

            renderer.RenderDocument();

            string filename = "MigraDocTest.pdf";
            renderer.PdfDocument.Save(filename);
            Process.Start(filename);
        }
    }
}


I also tried to implement the patch from the top posting, but I am not sure If I've done this correctly because I am a complete newbie ;)


Could anybody help me, maybe also with implementing the patch...

Thanks in advance :)


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 29, 2012 11:14 am 
Offline

Joined: Fri Jun 29, 2012 10:49 am
Posts: 1
Created a account just to say thanks :!:
:lol:
It works perfect for my needs


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 28, 2012 2:27 pm 
Offline

Joined: Fri Sep 28, 2012 2:17 pm
Posts: 2
Location: Warszawa, Polska
Hello, I have other kind of issue. I've added this patch, but unfortunately when I'm trying to add barcode (128) it appears with embedded text (which is correct and as expected), but code after scanning is "123123". Constructor in BarcodeRenderer.cs contains this value - but as far I have no idea how to fix it.

[edit:] I forgot to mention I used also code from here:
viewtopic.php?f=4&t=1089
It's essential for my issue :)

Any help would be appreciated :)

[Edit2:] Of course it was matter of constructor in render method in BarcodeRenderer.cs - I should have look more carefully. Great solution for Barcode 128, thanks :)

_________________
Regards, Piotr Sońta


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 10, 2012 1:42 pm 
Offline

Joined: Wed Oct 10, 2012 1:25 pm
Posts: 1
Here's the code modified to use the specified code in the element of the MigraDoc documents:

I only tested with B Type because is all I need for my bussiness. All the other patches must be applied before. I think that i don't forget any other change. Otherwise, tell me know.

Regards.

Code128.cs:

Code:
namespace PdfSharp.Drawing.BarCodes
{
   using System;
   using System.Collections.Generic;
   /// <summary>
   /// A Class to be able to render a Code 128 bar code
   /// </summary>
   /// <remarks>
   /// For a more detailed explanation of the Code 128, please visit the following web site: http://www.barcodeman.com/info/c128.php3 or http://www.adams1.com/128code.html
   /// </remarks>
   public class Code128 : BarCode
   {
      #region Constants
      #region private
      /// <summary>
      /// The cod e 128_ stopcode.
      /// </summary>
      private const int CODE128_STOPCODE = 106;
      #endregion
      #endregion
      #region Fields
      #region private
      /// <summary>
      /// The code 128 code.
      /// </summary>
      private Code128Type code128Code;
      private Code128Type Code128Code
      {
         get { return this.code128Code; }
         set { this.code128Code = value;
         this.CheckTypeC(this.Text);
         }
      }
      /// <summary>
      /// The values.
      /// </summary>
      private Byte[] Values;
      #endregion
      #region public
      /// <summary>
      /// A static place holder for the patterns to draw the code 128 barcode
      /// </summary>
      public static Dictionary<Byte, Byte[]> Patterns;
      #endregion
      #endregion
      #region Methods
      #region private
      /// <summary>
      /// The calculate parity.
      /// </summary>
      /// <returns>
      /// The calculate parity.
      /// </returns>
      private int CalculateParity()
      {
         long parityValue = (int)this.Code128Code;
         for (int x = 1; x <= this.Values.Length; x++)
            parityValue += (this.Values[x - 1]) * x;
         parityValue %= 103;
         return (int)parityValue;
      }
      /// <summary>
      /// The get pattern.
      /// </summary>
      /// <param name="codeValue">
      /// The code value.
      /// </param>
      /// <returns>
      /// </returns>
      /// <exception cref="ArgumentOutOfRangeException">
      /// </exception>
      /// <exception cref="ArgumentOutOfRangeException">
      /// </exception>
      private byte[] GetPattern(int codeValue)
      {
         if (codeValue < 0)
            throw new ArgumentOutOfRangeException("Parameter ch (int) can not be less than 32 (space).");
         if (codeValue > 106)
            throw new ArgumentOutOfRangeException("Parameter ch (int) can not be greater than 138.");
         return Patterns[(byte)codeValue];
      }
      /// <summary>
      /// Renders a single line of the character. Each character has three lines and three spaces
      /// </summary>
      /// <param name="info">
      /// </param>
      /// <param name="barWidth">
      /// Indicates the thickness of the line/bar to be rendered.
      /// </param>
      /// <param name="brush">
      /// Indicates the brush to use to render the line/bar.
      /// </param>
      private void RenderBar(BarCodeRenderInfo info, double barWidth, XBrush brush)
      {
         double height = this.Size.Height;
         double yPos = info.CurrPos.Y;
         switch (this.TextLocation)
         {
            case TextLocation.Above:
               yPos = info.CurrPos.Y + (height / 5);
               height *= 4.0 / 5;
               break;
            case TextLocation.Below:
               height *= 4.0 / 5;
               break;
            case TextLocation.AboveEmbedded:
            case TextLocation.BelowEmbedded:
            case TextLocation.None:
               break;
         }
         XRect rect = new XRect(info.CurrPos.X, yPos, barWidth, height);
         info.Gfx.DrawRectangle(brush, rect);
         info.CurrPos.X += barWidth;
      }
      /// <summary>
      /// The render start.
      /// </summary>
      /// <param name="info">
      /// The info.
      /// </param>
      private void RenderStart(BarCodeRenderInfo info) { this.RenderValue(info, (int)this.Code128Code); }
      /// <summary>
      /// The render stop.
      /// </summary>
      /// <param name="info">
      /// The info.
      /// </param>
      private void RenderStop(BarCodeRenderInfo info)
      {
         this.RenderValue(info, this.CalculateParity());
         this.RenderValue(info, CODE128_STOPCODE);
      }
      /// <summary>
      /// The render text.
      /// </summary>
      /// <param name="info">
      /// The info.
      /// </param>
      private void RenderText(BarCodeRenderInfo info)
      {
         if (info.Font == null)
            info.Font = new XFont("Courier New", this.Size.Height / 6);
         XPoint center = info.Position + CodeBase.CalcDistance(this.anchor, AnchorType.TopLeft, this.size);
         if (this.TextLocation == TextLocation.Above)
            info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, this.Size), XStringFormats.TopCenter);
         else if (this.TextLocation == TextLocation.AboveEmbedded)
         {
            XSize textSize = info.Gfx.MeasureString(this.text, info.Font);
            textSize.Width += this.Size.Width * .15;
            XPoint point = info.Position;
            point.X += (this.Size.Width - textSize.Width) / 2;
            XRect rect = new XRect(point, textSize);
            info.Gfx.DrawRectangle(XBrushes.White, rect);
            info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, this.Size), XStringFormats.TopCenter);
         }
         else if (this.TextLocation == TextLocation.Below)
            info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, this.Size), XStringFormats.BottomCenter);
         else if (this.TextLocation == TextLocation.BelowEmbedded)
         {
            XSize textSize = info.Gfx.MeasureString(this.text, info.Font);
            textSize.Width += this.Size.Width * .15;
            XPoint point = info.Position;
            point.X += (this.Size.Width - textSize.Width) / 2;
            point.Y += this.Size.Height - textSize.height;
            XRect rect = new XRect(point, textSize);
            info.Gfx.DrawRectangle(XBrushes.White, rect);
            info.Gfx.DrawString(this.text, info.Font, info.Brush, new XRect(center, this.Size), XStringFormats.BottomCenter);
         }
      }
      /// <summary>
      /// The render value.
      /// </summary>
      /// <param name="info">
      /// The info.
      /// </param>
      /// <param name="chVal">
      /// The ch val.
      /// </param>
      private void RenderValue(BarCodeRenderInfo info, int chVal)
      {
         byte[] pattern = this.GetPattern(chVal);
         XBrush space = XBrushes.White;
         for (int idx = 0; idx < pattern.Length; idx++)
            if ((idx % 2) == 0)
               this.RenderBar(info, info.ThinBarWidth * pattern[idx]);
            else
               this.RenderBar(info, info.ThinBarWidth * pattern[idx], space);
      }
      #endregion
      #region internal
      /// <summary>
      /// The init rendering.
      /// </summary>
      /// <param name="info">
      /// The info.
      /// </param>
      /// <exception cref="InvalidOperationException">
      /// </exception>
      /// <exception cref="InvalidOperationException">
      /// </exception>
      internal override void InitRendering(BarCodeRenderInfo info)
      {
         if (this.Values == null)
            throw new InvalidOperationException(BcgSR.BarCodeNotSet);
         if (this.Values.Length == 0)
            throw new InvalidOperationException(BcgSR.EmptyBarCodeSize);
         int numberOfBars = this.Values.Length + 3; // The length of the string plus the start, stop, and parity value
         numberOfBars *= 11; // Each character has 11 bars
         numberOfBars += 2; // Add two more because the stop bit has two extra bars
         // Calculating the width of a bar
         info.ThinBarWidth = this.Size.Width / numberOfBars;
      }
      /// <summary>
      /// Renders a single line of the character. Each character has three lines and three spaces
      /// </summary>
      /// <param name="info">
      /// </param>
      /// <param name="barWidth">
      /// Indicates the thickness of the line/bar to be rendered.
      /// </param>
      internal void RenderBar(BarCodeRenderInfo info, double barWidth) { this.RenderBar(info, barWidth, info.Brush); }
      #endregion
      #region protected internal
      /// <summary>
      /// Renders the content found in Text
      /// </summary>
      /// <param name="gfx">
      /// XGraphics - Instance of the drawing surface
      /// </param>
      /// <param name="brush">
      /// XBrush - Line and Color to draw the bar code
      /// </param>
      /// <param name="font">
      /// XFont - Font to use to draw the text string
      /// </param>
      /// <param name="position">
      /// XPoint - Location to render the bar code
      /// </param>
      protected internal override void Render(XGraphics gfx, XBrush brush, XFont font, XPoint position)
      {
         // Create the array to hold the values to be rendered
         this.Values = this.Code128Code == Code128Type.C ? new byte[this.text.Length / 2] : new byte[this.text.Length];
         String buffer = String.Empty;
         for (Int32 index = 0; index < text.Length; index++)
            switch (this.Code128Code)
            {
               case Code128Type.A:
                  if (text[index] < 32)
                     this.Values[index] = (byte)(text[index] + 64);
                  else if ((text[index] >= 32) && (text[index] < 64))
                     this.Values[index] = (byte)(text[index] - 32);
                  else
                     this.Values[index] = (byte)text[index];
                  break;
               case Code128Type.B:
                  this.Values[index] = (byte)(text[index] - 32);
                  break;
               case Code128Type.C:
                  if ((text[index] >= '0') && (text[index] <= '9'))
                  {
                     buffer += text[index];
                     if (buffer.Length == 2)
                     {
                        this.Values[index / 2] = byte.Parse(buffer);
                        buffer = String.Empty;
                     }
                  }
                  else
                     throw new ArgumentOutOfRangeException("Parameter text (string) can only contain numeric characters for Code 128 - Code C");
                  break;
            }
         if (this.Values == null)
            throw new InvalidOperationException("Text or Values must be set");
         if (this.Values.Length == 0)
            throw new InvalidOperationException("Text or Values must have content");
         for (int x = 0; x < this.Values.Length; x++)
            if (this.Values[x] > 102)
               throw new ArgumentOutOfRangeException(BcgSR.InvalidCode128(x));
         XGraphicsState state = gfx.Save();
         BarCodeRenderInfo info = new BarCodeRenderInfo(gfx, brush, font, position);
         this.InitRendering(info);
         info.CurrPosInString = 0;
         info.CurrPos = position - CodeBase.CalcDistance(AnchorType.TopLeft, this.anchor, this.size);
         this.RenderStart(info);
         foreach (byte c in this.Values)
            this.RenderValue(info, c);
         this.RenderStop(info);
         if (this.TextLocation != TextLocation.None)
            this.RenderText(info);
         gfx.Restore(state);
      }
      #endregion
      #region protected
      /// <summary>
      /// Validates the text string to be coded
      /// </summary>
      /// <param name="text">
      /// String - The text string to be coded
      /// </param>
      protected override void CheckCode(String text)
      {
         if (text == null)
            throw new ArgumentNullException("Parameter text (string) can not be null");
         if (text.Length == 0)
            throw new ArgumentException("Parameter text (string) can not be empty");
         this.CheckTypeC(text);
      }
      #endregion
      #endregion
      #region Constructors
      #region public
      /// <summary>
      /// Initializes a new instance of the <see cref="Code128"/> class.
      /// Constructor
      /// </summary>
      public Code128()
         : base("", XSize.Empty, CodeDirection.LeftToRight)
      {
         if (Patterns == null)
         {
            Patterns = new Dictionary<Byte, Byte[]>();
            Patterns.Add(0, new Byte[] { 2, 1, 2, 2, 2, 2 });
            Patterns.Add(1, new Byte[] { 2, 2, 2, 1, 2, 2 });
            Patterns.Add(2, new Byte[] { 2, 2, 2, 2, 2, 1 });
            Patterns.Add(3, new Byte[] { 1, 2, 1, 2, 2, 3 });
            Patterns.Add(4, new Byte[] { 1, 2, 1, 3, 2, 2 });
            Patterns.Add(5, new Byte[] { 1, 3, 1, 2, 2, 2 });
            Patterns.Add(6, new Byte[] { 1, 2, 2, 2, 1, 3 });
            Patterns.Add(7, new Byte[] { 1, 2, 2, 3, 1, 2 });
            Patterns.Add(8, new Byte[] { 1, 3, 2, 2, 1, 2 });
            Patterns.Add(9, new Byte[] { 2, 2, 1, 2, 1, 3 });
            Patterns.Add(10, new Byte[] { 2, 2, 1, 3, 1, 2 });
            Patterns.Add(11, new Byte[] { 2, 3, 1, 2, 1, 2 });
            Patterns.Add(12, new Byte[] { 1, 1, 2, 2, 3, 2 });
            Patterns.Add(13, new Byte[] { 1, 2, 2, 1, 3, 2 });
            Patterns.Add(14, new Byte[] { 1, 2, 2, 2, 3, 1 });
            Patterns.Add(15, new Byte[] { 1, 1, 3, 2, 2, 2 });
            Patterns.Add(16, new Byte[] { 1, 2, 3, 1, 2, 2 });
            Patterns.Add(17, new Byte[] { 1, 2, 3, 2, 2, 1 });
            Patterns.Add(18, new Byte[] { 2, 2, 3, 2, 1, 1 });
            Patterns.Add(19, new Byte[] { 2, 2, 1, 1, 3, 2 });
            Patterns.Add(20, new Byte[] { 2, 2, 1, 2, 3, 1 });
            Patterns.Add(21, new Byte[] { 2, 1, 3, 2, 1, 2 });
            Patterns.Add(22, new Byte[] { 2, 2, 3, 1, 1, 2 });
            Patterns.Add(23, new Byte[] { 3, 1, 2, 1, 3, 1 });
            Patterns.Add(24, new Byte[] { 3, 1, 1, 2, 2, 2 });
            Patterns.Add(25, new Byte[] { 3, 2, 1, 1, 2, 2 });
            Patterns.Add(26, new Byte[] { 3, 2, 1, 2, 2, 1 });
            Patterns.Add(27, new Byte[] { 3, 1, 2, 2, 1, 2 });
            Patterns.Add(28, new Byte[] { 3, 2, 2, 1, 1, 2 });
            Patterns.Add(29, new Byte[] { 3, 2, 2, 2, 1, 1 });
            Patterns.Add(30, new Byte[] { 2, 1, 2, 1, 2, 3 });
            Patterns.Add(31, new Byte[] { 2, 1, 2, 3, 2, 1 });
            Patterns.Add(32, new Byte[] { 2, 3, 2, 1, 2, 1 });
            Patterns.Add(33, new Byte[] { 1, 1, 1, 3, 2, 3 });
            Patterns.Add(34, new Byte[] { 1, 3, 1, 1, 2, 3 });
            Patterns.Add(35, new Byte[] { 1, 3, 1, 3, 2, 1 });
            Patterns.Add(36, new Byte[] { 1, 1, 2, 3, 1, 3 });
            Patterns.Add(37, new Byte[] { 1, 3, 2, 1, 1, 3 });
            Patterns.Add(38, new Byte[] { 1, 3, 2, 3, 1, 1 });
            Patterns.Add(39, new Byte[] { 2, 1, 1, 3, 1, 3 });
            Patterns.Add(40, new Byte[] { 2, 3, 1, 1, 1, 3 });
            Patterns.Add(41, new Byte[] { 2, 3, 1, 3, 1, 1 });
            Patterns.Add(42, new Byte[] { 1, 1, 2, 1, 3, 3 });
            Patterns.Add(43, new Byte[] { 1, 1, 2, 3, 3, 1 });
            Patterns.Add(44, new Byte[] { 1, 3, 2, 1, 3, 1 });
            Patterns.Add(45, new Byte[] { 1, 1, 3, 1, 2, 3 });
            Patterns.Add(46, new Byte[] { 1, 1, 3, 3, 2, 1 });
            Patterns.Add(47, new Byte[] { 1, 3, 3, 1, 2, 1 });
            Patterns.Add(48, new Byte[] { 3, 1, 3, 1, 2, 1 });
            Patterns.Add(49, new Byte[] { 2, 1, 1, 3, 3, 1 });
            Patterns.Add(50, new Byte[] { 2, 3, 1, 1, 3, 1 });
            Patterns.Add(51, new Byte[] { 2, 1, 3, 1, 1, 3 });
            Patterns.Add(52, new Byte[] { 2, 1, 3, 3, 1, 1 });
            Patterns.Add(53, new Byte[] { 2, 1, 3, 1, 3, 1 });
            Patterns.Add(54, new Byte[] { 3, 1, 1, 1, 2, 3 });
            Patterns.Add(55, new Byte[] { 3, 1, 1, 3, 2, 1 });
            Patterns.Add(56, new Byte[] { 3, 3, 1, 1, 2, 1 });
            Patterns.Add(57, new Byte[] { 3, 1, 2, 1, 1, 3 });
            Patterns.Add(58, new Byte[] { 3, 1, 2, 3, 1, 1 });
            Patterns.Add(59, new Byte[] { 3, 3, 2, 1, 1, 1 });
            Patterns.Add(60, new Byte[] { 3, 1, 4, 1, 1, 1 });
            Patterns.Add(61, new Byte[] { 2, 2, 1, 4, 1, 1 });
            Patterns.Add(62, new Byte[] { 4, 3, 1, 1, 1, 1 });
            Patterns.Add(63, new Byte[] { 1, 1, 1, 2, 2, 4 });
            Patterns.Add(64, new Byte[] { 1, 1, 1, 4, 2, 2 });
            Patterns.Add(65, new Byte[] { 1, 2, 1, 1, 2, 4 });
            Patterns.Add(66, new Byte[] { 1, 2, 1, 4, 2, 1 });
            Patterns.Add(67, new Byte[] { 1, 4, 1, 1, 2, 2 });
            Patterns.Add(68, new Byte[] { 1, 4, 1, 2, 2, 1 });
            Patterns.Add(69, new Byte[] { 1, 1, 2, 2, 1, 4 });
            Patterns.Add(70, new Byte[] { 1, 1, 2, 4, 1, 2 });
            Patterns.Add(71, new Byte[] { 1, 2, 2, 1, 1, 4 });
            Patterns.Add(72, new Byte[] { 1, 2, 2, 4, 1, 1 });
            Patterns.Add(73, new Byte[] { 1, 4, 2, 1, 1, 2 });
            Patterns.Add(74, new Byte[] { 1, 4, 2, 2, 1, 1 });
            Patterns.Add(75, new Byte[] { 2, 4, 1, 2, 1, 1 });
            Patterns.Add(76, new Byte[] { 2, 2, 1, 1, 1, 4 });
            Patterns.Add(77, new Byte[] { 4, 1, 3, 1, 1, 1 });
            Patterns.Add(78, new Byte[] { 2, 4, 1, 1, 1, 2 });
            Patterns.Add(79, new Byte[] { 1, 3, 4, 1, 1, 1 });
            Patterns.Add(80, new Byte[] { 1, 1, 1, 2, 4, 2 });
            Patterns.Add(81, new Byte[] { 1, 2, 1, 1, 4, 2 });
            Patterns.Add(82, new Byte[] { 1, 2, 1, 2, 4, 1 });
            Patterns.Add(83, new Byte[] { 1, 1, 4, 2, 1, 2 });
            Patterns.Add(84, new Byte[] { 1, 2, 4, 1, 1, 2 });
            Patterns.Add(85, new Byte[] { 1, 2, 4, 2, 1, 1 });
            Patterns.Add(86, new Byte[] { 4, 1, 1, 2, 1, 2 });
            Patterns.Add(87, new Byte[] { 4, 2, 1, 1, 1, 2 });
            Patterns.Add(88, new Byte[] { 4, 2, 1, 2, 1, 1 });
            Patterns.Add(89, new Byte[] { 2, 1, 2, 1, 4, 1 });
            Patterns.Add(90, new Byte[] { 2, 1, 4, 1, 2, 1 });
            Patterns.Add(91, new Byte[] { 4, 1, 2, 1, 2, 1 });
            Patterns.Add(92, new Byte[] { 1, 1, 1, 1, 4, 3 });
            Patterns.Add(93, new Byte[] { 1, 1, 1, 3, 4, 1 });
            Patterns.Add(94, new Byte[] { 1, 3, 1, 1, 4, 1 });
            Patterns.Add(95, new Byte[] { 1, 1, 4, 1, 1, 3 });
            Patterns.Add(96, new Byte[] { 1, 1, 4, 3, 1, 1 });
            Patterns.Add(97, new Byte[] { 4, 1, 1, 1, 1, 3 });
            Patterns.Add(98, new Byte[] { 4, 1, 1, 3, 1, 1 });
            Patterns.Add(99, new Byte[] { 1, 1, 3, 1, 4, 1 });
            Patterns.Add(100, new Byte[] { 1, 1, 4, 1, 3, 1 });
            Patterns.Add(101, new Byte[] { 3, 1, 1, 1, 4, 1 });
            Patterns.Add(102, new Byte[] { 4, 1, 1, 1, 3, 1 });
            Patterns.Add(103, new Byte[] { 2, 1, 1, 4, 1, 2 });
            Patterns.Add(104, new Byte[] { 2, 1, 1, 2, 1, 4 });
            Patterns.Add(105, new Byte[] { 2, 1, 1, 2, 3, 2 });
            Patterns.Add(106, new Byte[] { 2, 3, 3, 1, 1, 1, 2 });
         }
         this.code128Code = Code128Type.B;
      }
      /// <summary>
      /// Ensure that the text is an even length.
      /// </summary>
      /// <param name="codeC">Code to check.</param>
      private void CheckTypeC(String codeC)
      {
         if (this.Code128Code == Code128Type.C && (codeC.Length % 2) == 1)
            throw new ArgumentOutOfRangeException("Parameter text (string) must have an even length for Code 128 - Code C");
      }
      #endregion
      #endregion
   }
}


Code128Type.cs:

Code:
namespace PdfSharp.Drawing.BarCodes
{
   /// <summary>
   /// Code types for Code 128 bar code
   /// </summary>
   public enum Code128Type
   {
      /// <summary>
      /// Code A
      /// </summary>
      A = 103,
      /// <summary>
      /// Code B
      /// </summary>
      B = 104,
      /// <summary>
      /// Code buffer
      /// </summary>
      C = 105,
   }
}


BarcodeRenderer.cs:

Code:
#region MigraDoc - Creating Documents on the Fly
//
// Authors:
//   Klaus Potzesny (mailto:Klaus.Potzesny@pdfsharp.com)
//   Adam Brengesjö (mailto:ca.brengesjo@gmail.com)
//
// Copyright (c) 2001-2009 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://www.migradoc.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.IO;
using System.Diagnostics;
using MigraDoc.DocumentObjectModel;
using PdfSharp.Drawing;
using PdfSharp.Drawing.BarCodes;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering.Resources;

namespace MigraDoc.Rendering
{
    /// <summary>
    /// Renders images.
    /// </summary>
    internal class BarcodeRenderer : ShapeRenderer
    {
        internal BarcodeRenderer(XGraphics gfx, Barcode barcode, FieldInfos fieldInfos)
            : base(gfx, barcode, fieldInfos)
        {
            this.barcode = barcode;
            BarcodeRenderInfo renderInfo = new BarcodeRenderInfo();
            renderInfo.shape = this.shape;
            this.renderInfo = renderInfo;
        }

        internal BarcodeRenderer(XGraphics gfx, RenderInfo renderInfo, FieldInfos fieldInfos)
            : base(gfx, renderInfo, fieldInfos)
        {
            this.barcode = (Barcode)renderInfo.DocumentObject;
        }

        internal override void Format(Area area, FormatInfo previousFormatInfo)
        {
            BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this.renderInfo.FormatInfo;

            formatInfo.Height = this.barcode.Height.Point;
            formatInfo.Width = this.barcode.Width.Point;

            base.Format(area, previousFormatInfo);
        }

        protected override XUnit ShapeHeight
        {
            get
            {
                BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this.renderInfo.FormatInfo;
                return formatInfo.Height + this.lineFormatRenderer.GetWidth();
            }
        }

        protected override XUnit ShapeWidth
        {
            get
            {
                BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this.renderInfo.FormatInfo;
                return formatInfo.Width + this.lineFormatRenderer.GetWidth();
            }
        }

      internal override void Render()
      {
         RenderFilling();

         BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this.renderInfo.FormatInfo;
         Area contentArea = this.renderInfo.LayoutInfo.ContentArea;
         XRect destRect = new XRect(contentArea.X, contentArea.Y, formatInfo.Width, formatInfo.Height);

         BarCode gfxBarcode = null;
         Code128 c128;

         switch (this.barcode.Type)
         {
            case BarcodeType.Barcode128:
               c128 = new Code128();
               c128.TextLocation = TextLocation.BelowEmbedded;
               gfxBarcode = c128;
               break;
            case BarcodeType.Barcode25i:
               gfxBarcode = new Code2of5Interleaved();
               break;
            case BarcodeType.Barcode39:
               gfxBarcode = new Code3of9Standard();
               break;
            case BarcodeType.BarcodeEAN13:
               gfxBarcode = new EAN13();
               break;
            default:
               break;
         }

         // if gfxBarcode is null, the barcode type is not supported
         if (gfxBarcode != null)
         {
            gfxBarcode.Text = this.barcode.Code;
            gfxBarcode.Direction = CodeDirection.LeftToRight;
            gfxBarcode.Size = new XSize(ShapeWidth, ShapeHeight);
            this.gfx.DrawBarCode(gfxBarcode, XBrushes.Black, destRect.Location);
         }

         RenderLine();
      }

        Barcode barcode;
    }
}


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 16, 2019 3:43 pm 
Offline

Joined: Tue Jul 16, 2019 3:40 pm
Posts: 1
These posts are from a long time ago, but I recently got this same error and wanted to post the solution that worked for me.

I was using this where accountNo = "a-1223":

var bartext = accountNo;
BarCode barcode = new Code3of9Standard(bartext, new XSize(90, 40));

This is what I had to change it to to work (all letters passed to bar code must be uppercase to work):

var bartext = accountNo.ToUpper();
BarCode barcode = new Code3of9Standard(bartext, new XSize(90, 40));


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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