PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Oct 20, 2017 2:14 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Barode Not Appearing
PostPosted: Thu Sep 15, 2016 9:31 pm 
Offline

Joined: Thu Sep 15, 2016 9:21 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Barode Not Appearing
PostPosted: Mon Sep 19, 2016 12:54 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2720
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Barode Not Appearing
PostPosted: Fri Oct 06, 2017 7:30 am 
Offline

Joined: Fri Oct 06, 2017 7:27 am
Posts: 1
Hello, I have the same problem. How did you solve this problem?

Thanks in advance.
Regards.


Top
 Profile  
Reply with quote  
 Post subject: Re: Barode Not Appearing
PostPosted: Fri Oct 06, 2017 3:26 pm 
Offline

Joined: Thu Sep 15, 2016 9:21 pm
Posts: 2
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 );
        }


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 14 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:  
Powered by phpBB® Forum Software © phpBB Group