PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Barode Not Appearing
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3460
Page 1 of 1

Author:  chairmanmao [ Thu Sep 15, 2016 9:31 pm ]
Post subject:  Barode Not Appearing

I just discovered MigraDoc the other day, and I love it! But I'm having difficulty getting a barcode to show up in a PDF I'm creating. Here's the relevant code (c# 6):

Code:
            Table voterTable = CurrentSection.AddTable();

            foreach( Unit colWidth in ColumnWidths )
            {
                voterTable.AddColumn( colWidth );
            }

            foreach( Voter voter in voters
                .OrderBy( v => v.LastName )
                .ThenBy( v => v.FirstName ) )
            {
                // create name
                StringBuilder sbVoter = new StringBuilder();
                if( voter.BallotType != BallotType.PollingPlace ) sbVoter.Append( "@" );
                sbVoter.AppendWithSeparator( voter.LastName, "" );
                sbVoter.AppendWithSeparator( voter.FirstName, ", " );

                Row voterRow = voterTable.AddRow();

                voterRow.Cells[ 0 ].AddParagraph( sbVoter.ToString() );
                voterRow.Cells[ 1 ].AddParagraph( $"{voter.GetAge():n0}" );

                foreach( string factor in voter.GetFactors() )
                {
                    voterRow.Cells[ 2 ].AddParagraph( factor );
                }

                voterRow.Cells[ 3 ].AddParagraph( "Y  N  U  AB  LA" ).Format = GetParagraphFormat(BoldBaseFont, ptsAfter: 0, ptsBefore: 0);

                Barcode bcVoter = voterRow.Cells[ 4 ].Elements.AddBarcode();
                bcVoter.Code = voter.VoterID.ToString();
                bcVoter.Text = false;
                bcVoter.Type = BarcodeType.Barcode39;
                bcVoter.Orientation = TextOrientation.Horizontal;
                bcVoter.Width = Unit.FromInch(1.5);
                bcVoter.Height = Unit.FromInch( 0.25 );
            }


This creates a table, with one row for each voter. The table has five columns, the last one (4) should contain the barcode. No errors are reported during execution, and the PDF gets created by PdfRenderer. The Free 3 of 9 barcode font is installed on the machine.

PdfRenderer is created like this:

Code:
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer( false, PdfFontEmbedding.Always );


I'm obviously missing something simple. Suggestions?

- Mark

Author:  Thomas Hoevel [ Mon Sep 19, 2016 12:54 pm ]
Post subject:  Re: Barode Not Appearing

Hi, Mark,

Barcodes are not my area of expertise.

But AFAIK you do not have to install any true type fonts to get them working.

To start simple I would create a page that only contains the barcode (without a surrounding table).
Please use the Issue Submission Template if that does not work.

Author:  joabelfa [ Fri Oct 06, 2017 7:30 am ]
Post subject:  Re: Barode Not Appearing

Hello, I have the same problem. How did you solve this problem?

Thanks in advance.
Regards.

Author:  chairmanmao [ Fri Oct 06, 2017 3:26 pm ]
Post subject:  Re: Barode Not Appearing

I ended up using a 3rd party library, Zen.Barcode (https://www.nuget.org/packages/Zen.Barcode.Rendering.Framework/), to create the barcodes as images which I then passed off to PDFSharp/MigraDoc. Here's the code I used to generate the images:

Code:
        /// <summary>
        /// Transforms a specified string into a Code39Barcode, encoded as a Base64 string
        /// prefixed with 'base64:' so that it will be compatible with MigraDoc.
        /// </summary>
        /// <param name="text">the text to convert</param>
        /// <param name="maxHeight">the height of the barcode; defaults to 20 pixels</param>
        /// <returns>a Base64-encoded string representation of the barcode image, prefixed
        /// with 'base64:' to work with MigraDoc</returns>
        protected string GetBarcode( string text, int maxHeight = 20 )
        {
            Code39BarcodeDraw barcode = BarcodeDrawFactory.Code39WithoutChecksum;

            System.Drawing.Image img = barcode.Draw( text, maxHeight );

            byte[] buffer = null;

            using( MemoryStream ms = new MemoryStream() )
            {
                img.Save( ms, System.Drawing.Imaging.ImageFormat.Gif );

                buffer = ms.ToArray();
            }

            return "base64:" + Convert.ToBase64String( buffer );
        }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/