Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jul 15, 2014 2:32 pm

I'm considering your products and was wondering how I can create bar-codes on the fly to be added to a PDF file. I need to create a list of items and then next to each item will be the ID and next to that will be a 2d bar-code that includes the Sample ID. Do I need to save each barcode before I add it to the PDF document or can I create it in memory and just add it?

morgenweck
 
Posts: 1
Joined: Tue Jul 15, 2014 2:27 pm

Wed Jul 16, 2014 6:10 am

Hello,

Thanks for your inquiry.
You could use Spire.PDF component and Free Spire.Barcode component to achieve it.
Share sample code, you can get the output pdf from the link:http://www.e-iceblue.com/downloads/attachment/Output.pdf.
Code: Select all
 static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

            string ID = "0012345898";
            float y = 10;
            page.Canvas.DrawString(ID, font, brush, new PointF(10, y));
            PdfStringFormat format = new PdfStringFormat();
            SizeF size = font.MeasureString(ID, format);

            Image barcode1 = CreateBarcode(ID);
            y += size.Height+3;
            page.Canvas.DrawImage(PdfImage.FromImage(barcode1),new PointF(10,y));

            y += barcode1.Height + 10;
            ID = "0347623220";
            page.Canvas.DrawString(ID, font, brush, new PointF(10, y));
            size = font.MeasureString(ID, format);

            Image barcode2 = CreateBarcode(ID);
            y += size.Height + 3;
            page.Canvas.DrawImage(PdfImage.FromImage(barcode2), new PointF(10, y));

            y += barcode2.Height + 10;
            ID = "4984593583";
            page.Canvas.DrawString(ID, font, brush, new PointF(10, y));
            size = font.MeasureString(ID, format);

            Image barcode3 = CreateBarcode(ID);
            y += size.Height + 3;
            page.Canvas.DrawImage(PdfImage.FromImage(barcode3), new PointF(10, y));


            string output = "Output.pdf";
            doc.SaveToFile(output);
            System.Diagnostics.Process.Start(output);
        }
        static Image CreateBarcode(string ID)
        {
            BarcodeSettings settings = new BarcodeSettings();     
            settings.Type = BarCodeType.QRCode;
            settings.Data2D = ID;
            settings.Data = ID;
            settings.ShowText = false;
            BarCodeGenerator generator = new BarCodeGenerator(settings);

            return generator.GenerateImage();
        }

If the code doesn't meet your need, please tell us and better provide us a output what you desire.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Jul 21, 2014 10:11 am

Hello,

Have you tried the code? Did it fulfill your requirement?
Thanks for your feedback.

Please feel free to contact us if you have any problems.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF

cron