private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            PdfSection section = doc.Sections.Add();

            section.PageSettings.Margins = margin;
            section.PageSettings.Size    = PdfPageSize.A4;

            // Create one page
            PdfPageBase page = section.Pages.Add();
            float       y    = 10;

            PdfBrush               brush1 = PdfBrushes.Black;
            PdfTrueTypeFont        font1  = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true);
            RectangleF             rctg   = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
            PdfLinearGradientBrush brush2
                = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical);

            //draw Codabar
            PdfTextWidget text = new PdfTextWidget();

            text.Font = font1;
            text.Text = "Codabar:";
            PdfLayoutResult result = text.Draw(page, 0, y);

            page = result.Page;
            y    = result.Bounds.Bottom + 2;

            PdfCodabarBarcode barcode1 = new PdfCodabarBarcode("00:12-3456/7890");

            barcode1.BarcodeToTextGapHeight = 1f;
            barcode1.EnableCheckDigit       = true;
            barcode1.ShowCheckDigit         = true;
            barcode1.TextDisplayLocation    = TextLocation.Bottom;
            barcode1.TextColor = Color.Blue;
            barcode1.Draw(page, new PointF(0, y));
            y = barcode1.Bounds.Bottom + 5;


            //draw Code11Barcode
            text.Text = "Code11:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode11Barcode barcode2 = new PdfCode11Barcode("123-4567890");

            barcode2.BarcodeToTextGapHeight = 1f;
            barcode2.TextDisplayLocation    = TextLocation.Bottom;
            barcode2.TextColor = Color.Blue;
            barcode2.Draw(page, new PointF(0, y));
            y = barcode2.Bounds.Bottom + 5;


            //draw Code128-A
            text.Text = "Code128-A:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode128ABarcode barcode3 = new PdfCode128ABarcode("HELLO 00-123");

            barcode3.BarcodeToTextGapHeight = 1f;
            barcode3.TextDisplayLocation    = TextLocation.Bottom;
            barcode3.TextColor = Color.Blue;
            barcode3.Draw(page, new PointF(0, y));
            y = barcode3.Bounds.Bottom + 5;


            //draw Code128-B
            text.Text = "Code128-B:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode128BBarcode barcode4 = new PdfCode128BBarcode("Hello 00-123");

            barcode4.BarcodeToTextGapHeight = 1f;
            barcode4.TextDisplayLocation    = TextLocation.Bottom;
            barcode4.TextColor = Color.Blue;
            barcode4.Draw(page, new PointF(0, y));
            y = barcode4.Bounds.Bottom + 5;


            //draw Code32
            text.Text = "Code32:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode32Barcode barcode5 = new PdfCode32Barcode("16273849");

            barcode5.BarcodeToTextGapHeight = 1f;
            barcode5.TextDisplayLocation    = TextLocation.Bottom;
            barcode5.TextColor = Color.Blue;
            barcode5.Draw(page, new PointF(0, y));
            y = barcode5.Bounds.Bottom + 5;

            page = section.Pages.Add();
            y    = 10;


            //draw Code39
            text.Text = "Code39:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode39Barcode barcode6 = new PdfCode39Barcode("16-273849");

            barcode6.BarcodeToTextGapHeight = 1f;
            barcode6.TextDisplayLocation    = TextLocation.Bottom;
            barcode6.TextColor = Color.Blue;
            barcode6.Draw(page, new PointF(0, y));
            y = barcode6.Bounds.Bottom + 5;


            //draw Code39-E
            text.Text = "Code39-E:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode39ExtendedBarcode barcode7 = new PdfCode39ExtendedBarcode("16-273849");

            barcode7.BarcodeToTextGapHeight = 1f;
            barcode7.TextDisplayLocation    = TextLocation.Bottom;
            barcode7.TextColor = Color.Blue;
            barcode7.Draw(page, new PointF(0, y));
            y = barcode7.Bounds.Bottom + 5;


            //draw Code93
            text.Text = "Code93:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode93Barcode barcode8 = new PdfCode93Barcode("16-273849");

            barcode8.BarcodeToTextGapHeight = 1f;
            barcode8.TextDisplayLocation    = TextLocation.Bottom;
            barcode8.TextColor        = Color.Blue;
            barcode8.QuietZone.Bottom = 5;
            barcode8.Draw(page, new PointF(0, y));
            y = barcode8.Bounds.Bottom + 5;


            //draw Code93-E
            text.Text = "Code93-E:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode93ExtendedBarcode barcode9 = new PdfCode93ExtendedBarcode("16-273849");

            barcode9.BarcodeToTextGapHeight = 1f;
            barcode9.TextDisplayLocation    = TextLocation.Bottom;
            barcode9.TextColor = Color.Blue;
            barcode9.Draw(page, new PointF(0, y));
            y = barcode9.Bounds.Bottom + 5;


            //Save pdf file.
            doc.SaveToFile("Barcode.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("Barcode.pdf");
        }
Esempio n. 2
0
        private static void DrawGenericBarcodes(PdfPage page, PdfFont titleFont, PdfFont barcodeFont)
        {
            PdfBrush brush = new PdfBrush();
            PdfPen lightGrayPen = new PdfPen(PdfRgbColor.LightGray, 0.5);

            page.Graphics.DrawString("Generic barcodes", titleFont, brush, 40, 20);
            for (int i = 0; i < 7; i++)
            {
                page.Graphics.DrawLine(lightGrayPen, 40, 50 + 100 * i, 570, 50 + 100 * i);
            }
            page.Graphics.DrawLine(lightGrayPen, 306, 50, 306, 750);

            string[] barcodes = new string[] { "Codabar", "Code 11", "Code 25", "Code 25 Interleaved", "Code 39", "Code 39 Extended",
                "Code 93", "Code 93 Extended", "Code 128 A", "Code 128 B", "Code 128 C", "COOP 25", "Matrix 25", "MSI/Plessey" };
            PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions();
            sao.Brush = brush;
            sao.Font = barcodeFont;
            PdfStringLayoutOptions slo = new PdfStringLayoutOptions();
            slo.HorizontalAlign = PdfStringHorizontalAlign.Center;
            slo.VerticalAlign = PdfStringVerticalAlign.Top;

            slo.X = 173;
            int sign = 1;
            for (int i = 0; i < barcodes.Length; i++)
            {
                slo.Y = 55 + 100 * (i / 2);

                page.Graphics.DrawString(barcodes[i], sao, slo);

                slo.X = slo.X + sign * 266;
                sign = -sign;
            }

            // Codabar
            PdfCodabarBarcode codabarBarcode = new PdfCodabarBarcode();
            codabarBarcode.Data = "523408943724";
            codabarBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(codabarBarcode, 173 - codabarBarcode.Width / 2, 70);

            // Code 11
            PdfCode11Barcode code11Barcode = new PdfCode11Barcode();
            code11Barcode.Data = "42376524534";
            code11Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code11Barcode, 173 + 266 - code11Barcode.Width / 2, 70);

            // Code 25
            PdfCode25Barcode code25Barcode = new PdfCode25Barcode();
            code25Barcode.Data = "857621354312";
            code25Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code25Barcode, 173 - code25Barcode.Width / 2, 170);

            // Code 25 Interleaved
            PdfCode25InterleavedBarcode code25InterleavedBarcode = new PdfCode25InterleavedBarcode();
            code25InterleavedBarcode.Data = "42376524534";
            code25InterleavedBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code25InterleavedBarcode, 173 + 266 - code25InterleavedBarcode.Width / 2, 170);

            // Code 39
            PdfCode39Barcode code39Barcode = new PdfCode39Barcode();
            code39Barcode.Data = "6430784327";
            code39Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code39Barcode, 173 - code39Barcode.Width / 2, 270);

            // Code 39 Extended
            PdfCode39ExtendedBarcode code39ExtendedBarcode = new PdfCode39ExtendedBarcode();
            code39ExtendedBarcode.Data = "8990436322";
            code39ExtendedBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code39ExtendedBarcode, 173 + 266 - code39ExtendedBarcode.Width / 2, 270);

            // Code 93
            PdfCode93Barcode code93Barcode = new PdfCode93Barcode();
            code93Barcode.Data = "6345212344";
            code93Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code93Barcode, 173 - code93Barcode.Width / 2, 370);

            // Code 39 Extended
            PdfCode93ExtendedBarcode code93ExtendedBarcode = new PdfCode93ExtendedBarcode();
            code93ExtendedBarcode.Data = "125436732";
            code93ExtendedBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code93ExtendedBarcode, 173 + 266 - code93ExtendedBarcode.Width / 2, 370);

            // Code 128 A
            PdfCode128ABarcode code128ABarcode = new PdfCode128ABarcode();
            code128ABarcode.Data = "XFINIUM.PDF";
            code128ABarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code128ABarcode, 173 - code128ABarcode.Width / 2, 470);

            // Code 128 B
            PdfCode128BBarcode code128BBarcode = new PdfCode128BBarcode();
            code128BBarcode.Data = "xfinium.pdf";
            code128BBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code128BBarcode, 173 + 266 - code128BBarcode.Width / 2, 470);

            // Code 128 C
            PdfCode128CBarcode code128CBarcode = new PdfCode128CBarcode();
            code128CBarcode.Data = "423409865432";
            code128CBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(code128CBarcode, 173 - code128CBarcode.Width / 2, 570);

            // COOP 25
            PdfCoop25Barcode coop25Barcode = new PdfCoop25Barcode();
            coop25Barcode.Data = "43256565543";
            coop25Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(coop25Barcode, 173 + 266 - coop25Barcode.Width / 2, 570);

            // Matrix 25
            PdfMatrix25Barcode matrix25Barcode = new PdfMatrix25Barcode();
            matrix25Barcode.Data = "500540024300";
            matrix25Barcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(matrix25Barcode, 173 - matrix25Barcode.Width / 2, 670);

            // MSI/Plessey
            PdfMsiPlesseyBarcode msiPlesseyBarcode = new PdfMsiPlesseyBarcode();
            msiPlesseyBarcode.Data = "1124332556";
            msiPlesseyBarcode.BarcodeTextPosition = PdfBarcodeTextPosition.Bottom;
            page.Graphics.DrawBarcode(msiPlesseyBarcode, 173 + 266 - msiPlesseyBarcode.Width / 2, 670);

            page.Graphics.CompressAndClose();
        }
        private async void BarcodeSample()
        {
            MemoryStream stream = new MemoryStream();

            //Create a new instance of PdfDocument class.
            using (PdfDocument document = new PdfDocument())
            {
                //Add a new page to the document.
                PdfPage page = document.Pages.Add();

                //Create Pdf graphics for the page
                PdfGraphics g = page.Graphics;

                //Create a solid brush
                PdfBrush brush = PdfBrushes.Black;

                #region 2D Barcode

                //Set the font
                PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 15f, PdfFontStyle.Bold);

                PdfPen pen   = new PdfPen(brush, 0.5f);
                float  width = page.GetClientSize().Width;

                float xPos = page.GetClientSize().Width / 2;

                PdfStringFormat format = new PdfStringFormat();
                format.Alignment = PdfTextAlignment.Center;

                // Draw String
                g.DrawString("2D Barcodes", font, brush, new PointF(xPos, 10), format);

                #region QR Barcode
                font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
                g.DrawString("QR Barcode", font, brush, new PointF(10, 90));

                //Create QR barcode
                PdfQRBarcode qrBarcode = new PdfQRBarcode();

                //Set input mode
                qrBarcode.InputMode = InputMode.BinaryMode;

                //Set error correction level
                qrBarcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;

                //Set dimension
                qrBarcode.XDimension = 4;

                //Add data
                qrBarcode.Text = "Syncfusion Essential Studio Enterprise edition $995";

                //Draw barcode on Pdf page
                qrBarcode.Draw(page, new PointF(25, 120));

                font = new PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Regular);

                g.DrawString("Input Type :   Eight Bit Binary", font, brush, new PointF(250, 160));
                g.DrawString("Encoded Data : Syncfusion Essential Studio Enterprise edition $995", font, brush, new PointF(250, 180));

                g.DrawLine(pen, new PointF(0, 320), new PointF(width, 320));

                #endregion

                #region Datamatrix
                font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
                g.DrawString("DataMatrix Barcode", font, brush, new PointF(10, 340));

                PdfDataMatrixBarcode dataMatrixBarcode = new PdfDataMatrixBarcode("5575235 Win7 4GB 64bit 7Jun2010");

                // Set dimension for each block
                dataMatrixBarcode.XDimension = 4;

                // Draw the barcode
                dataMatrixBarcode.Draw(page, new PointF(25, 540));

                font = new PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Regular);

                g.DrawString("Symbol Type : Square", font, brush, new PointF(250, 590));
                g.DrawString("Encoded Data : 5575235 Win7 4GB 64bit 7Jun2010", font, brush, new PointF(250, 610));

                pen = new PdfPen(brush, 0.5f);
                g.DrawLine(pen, new PointF(0, 700), new PointF(width, 700));

                string text = "TYPE 3523 - ETWS/N FE- SDFHW 06/08";

                dataMatrixBarcode = new PdfDataMatrixBarcode(text);

                // rectangular matrix
                dataMatrixBarcode.Size = PdfDataMatrixSize.Size16x48;

                dataMatrixBarcode.XDimension = 4;

                dataMatrixBarcode.Draw(page, new PointF(25, 400));

                g.DrawString("Symbol Type : Rectangle", font, brush, new PointF(250, 420));
                g.DrawString("Encoded Data : " + text, font, brush, new PointF(250, 440));

                pen = new PdfPen(brush, 0.5f);
                #endregion
                #endregion



                page = document.Pages.Add();
                g    = page.Graphics;

                //Set the font
                font = new PdfStandardFont(PdfFontFamily.Helvetica, 15f, PdfFontStyle.Bold);

                // Draw String
                g.DrawString("1D/Linear Barcodes", font, brush, new PointF(150, 10));

                // Set string format.
                format          = new PdfStringFormat();
                format.WordWrap = PdfWordWrapType.Word;

                #region Code39
                // Drawing Code39 barcode
                PdfCode39Barcode barcode = new PdfCode39Barcode();

                // Setting height of the barcode
                barcode.BarHeight = 45;
                barcode.Text      = "CODE39$";

                //Printing barcode on to the Pdf.
                barcode.Draw(page, new PointF(25, 70));

                font = new PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Regular);

                g.DrawString("Type : Code39", font, brush, new PointF(200, 80));
                g.DrawString("Allowed Characters : 0-9, A-Z,a dash(-),a dot(.),$,/,+,%, SPACE", font, brush, new PointF(200, 100));

                g.DrawLine(pen, new PointF(0, 150), new PointF(width, 150));
                #endregion

                #region Code39Extended
                // Drawing Code39Extended barcode
                PdfCode39ExtendedBarcode barcodeExt = new PdfCode39ExtendedBarcode();

                // Setting height of the barcode
                barcodeExt.BarHeight = 45;
                barcodeExt.Text      = "CODE39Ext";

                //Printing barcode on to the Pdf.
                barcodeExt.Draw(page, new PointF(25, 200));

                font = new PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Regular);

                g.DrawString("Type : Code39Ext", font, brush, new PointF(200, 210));
                g.DrawString("Allowed Characters : 0-9 A-Z a-z ", font, brush, new PointF(200, 230));

                g.DrawLine(pen, new PointF(0, 270), new PointF(width, 270));
                #endregion

                #region Code11Barcode
                // Drawing Code11  barcode
                PdfCode11Barcode barcode11 = new PdfCode11Barcode();

                // Setting height of the barcode
                barcode11.BarHeight = 45;
                barcode11.Text      = "012345678";
                barcode11.EncodeStartStopSymbols = true;

                //Printing barcode on to the Pdf.
                barcode11.Draw(page, new PointF(25, 300));

                font = new PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Regular);

                g.DrawString("Type : Code 11", font, brush, new PointF(200, 310));
                g.DrawString("Allowed Characters : 0-9, a dash(-) ", font, brush, new PointF(200, 330));

                g.DrawLine(pen, new PointF(0, 370), new PointF(width, 370));
                #endregion

                #region Codabar
                // Drawing CodaBarcode
                PdfCodabarBarcode codabar = new PdfCodabarBarcode();

                // Setting height of the barcode
                codabar.BarHeight = 45;
                codabar.Text      = "0123";

                //Printing barcode on to the Pdf.
                codabar.Draw(page, new PointF(25, 400));

                font = new PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Regular);

                g.DrawString("Type : Codabar", font, brush, new PointF(200, 410));
                g.DrawString("Allowed Characters : A,B,C,D,0-9,-$,:,/,a dot(.),+ ", font, brush, new PointF(200, 430));

                g.DrawLine(pen, new PointF(0, 470), new PointF(width, 470));
                #endregion

                #region Code32
                PdfCode32Barcode code32 = new PdfCode32Barcode();

                code32.Font = font;

                // Setting height of the barcode
                code32.BarHeight           = 45;
                code32.Text                = "01234567";
                code32.TextDisplayLocation = TextLocation.Bottom;
                code32.EnableCheckDigit    = true;
                code32.ShowCheckDigit      = true;

                //Printing barcode on to the Pdf.
                code32.Draw(page, new PointF(25, 500));

                g.DrawString("Type : Code32", font, brush, new PointF(200, 500));
                g.DrawString("Allowed Characters : 1 2 3 4 5 6 7 8 9 0 ", font, brush, new PointF(200, 520));

                g.DrawLine(pen, new PointF(0, 580), new PointF(width, 580));

                #endregion

                #region Code93
                PdfCode93Barcode code93 = new PdfCode93Barcode();

                // Setting height of the barcode
                code93.BarHeight = 45;
                code93.Text      = "ABC 123456789";

                //Printing barcode on to the Pdf.
                code93.Draw(page, new PointF(25, 600));

                g.DrawString("Type : Code93", font, brush, new PointF(200, 600));
                g.DrawString("Allowed Characters : 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z - . $ / + % SPACE ", font, brush, new RectangleF(200, 620, 300, 200), format);

                g.DrawLine(pen, new PointF(0, 680), new PointF(width, 680));
                #endregion

                #region Code93Extended
                PdfCode93ExtendedBarcode code93ext = new PdfCode93ExtendedBarcode();

                //Setting height of the barcode
                code93ext.BarHeight = 45;
                code93ext.EncodeStartStopSymbols = true;
                code93ext.Text = "(abc) 123456789";

                //Printing barcode on to the Pdf.
                page = document.Pages.Add();
                code93ext.Draw(page, new PointF(25, 50));

                g = page.Graphics;
                g.DrawString("Type : Code93 Extended", font, brush, new PointF(200, 50));
                g.DrawString("Allowed Characters : All 128 ASCII characters ", font, brush, new PointF(200, 70));

                g.DrawLine(pen, new PointF(0, 120), new PointF(width, 120));
                #endregion

                #region Code128
                PdfCode128ABarcode barcode128A = new PdfCode128ABarcode();

                // Setting height of the barcode
                barcode128A.BarHeight              = 45;
                barcode128A.Text                   = "ABCD 12345";
                barcode128A.EnableCheckDigit       = true;
                barcode128A.EncodeStartStopSymbols = true;
                barcode128A.ShowCheckDigit         = true;

                //Printing barcode on to the Pdf.
                barcode128A.Draw(page, new PointF(25, 135));

                g.DrawString("Type : Code128 A", font, brush, new PointF(200, 135));
                g.DrawString("Allowed Characters : NUL (0x00) SOH (0x01) STX (0x02) ETX (0x03) EOT (0x04) ENQ (0x05) ACK (0x06) BEL (0x07) BS (0x08) HT (0x09) LF (0x0A) VT (0x0B) FF (0x0C) CR (0x0D) SO (0x0E) SI (0x0F) DLE (0x10) DC1 (0x11) DC2 (0x12) DC3 (0x13) DC4 (0x14) NAK (0x15) SYN (0x16) ETB (0x17) CAN (0x18) EM (0x19) SUB (0x1A) ESC (0x1B) FS (0x1C) GS (0x1D) RS (0x1E) US (0x1F) SPACE (0x20) \" ! # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ / ]^ _  ", font, brush, new RectangleF(200, 155, 300, 200), format);

                g.DrawLine(pen, new PointF(0, 250), new PointF(width, 250));

                PdfCode128BBarcode barcode128B = new PdfCode128BBarcode();

                // Setting height of the barcode
                barcode128B.BarHeight              = 45;
                barcode128B.Text                   = "12345 abcd";
                barcode128B.EnableCheckDigit       = true;
                barcode128B.EncodeStartStopSymbols = true;
                barcode128B.ShowCheckDigit         = true;

                //Printing barcode on to the Pdf.
                barcode128B.Draw(page, new PointF(25, 280));

                g.DrawString("Type : Code128 B", font, brush, new PointF(200, 280));
                g.DrawString("Allowed Characters : SPACE (0x20) !  \" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ / ]^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ DEL (\x7F)  ", font, brush, new RectangleF(200, 300, 300, 200), format);

                g.DrawLine(pen, new PointF(0, 350), new PointF(width, 350));

                PdfCode128CBarcode barcode128C = new PdfCode128CBarcode();

                // Setting height of the barcode
                barcode128C.BarHeight              = 45;
                barcode128C.Text                   = "001122334455";
                barcode128C.EnableCheckDigit       = true;
                barcode128C.EncodeStartStopSymbols = true;
                barcode128C.ShowCheckDigit         = true;

                //Printing barcode on to the Pdf.
                barcode128C.Draw(page, new PointF(25, 370));

                g.DrawString("Type : Code128 C", font, brush, new PointF(200, 370));
                g.DrawString("Allowed Characters : 0 1 2 3 4 5 6 7 8 9 ", font, brush, new PointF(200, 390));

                g.DrawLine(pen, new PointF(0, 440), new PointF(width, 440));

                #endregion

                //Save the PDF document
                document.Save(stream);
            }

            stream.Position = 0;

            if (IsToggled)
            {
                PdfViewerUI pdfViewer = new SampleBrowser.PdfViewerUI();
                pdfViewer.PdfDocumentStream = stream;
                if (Device.Idiom != TargetIdiom.Phone && Device.OS == TargetPlatform.Windows)
                {
                    await PDFViewModel.Navigation.PushModalAsync(new NavigationPage(pdfViewer));
                }
                else
                {
                    await PDFViewModel.Navigation.PushAsync(pdfViewer);
                }
            }
            else
            {
                if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
                {
                    Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("Barcode.pdf", "application/pdf", stream);
                }
                else
                {
                    Xamarin.Forms.DependencyService.Get <ISave>().Save("Barcode.pdf", "application/pdf", stream);
                }
            }
        }
Esempio n. 4
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;

            PdfSection section = doc.Sections.Add();
            section.PageSettings.Margins = margin;
            section.PageSettings.Size = PdfPageSize.A4;

            // Create one page
            PdfPageBase page = section.Pages.Add();
            float y = 10;

            PdfBrush brush1 = PdfBrushes.Black;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true);
            RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
            PdfLinearGradientBrush brush2
                = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical);

            //draw Codabar
            PdfTextWidget text = new PdfTextWidget();
            text.Font = font1;
            text.Text = "Codabar:";
            PdfLayoutResult result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCodabarBarcode barcode1 = new PdfCodabarBarcode("00:12-3456/7890");
            barcode1.BarcodeToTextGapHeight = 1f;
            barcode1.EnableCheckDigit = true;
            barcode1.ShowCheckDigit = true;
            barcode1.TextDisplayLocation = TextLocation.Bottom;
            barcode1.TextColor = Color.Blue;
            barcode1.Draw(page, new PointF(0, y));
            y = barcode1.Bounds.Bottom + 5;


            //draw Code11Barcode
            text.Text = "Code11:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode11Barcode barcode2 = new PdfCode11Barcode("123-4567890");
            barcode2.BarcodeToTextGapHeight = 1f;
            barcode2.TextDisplayLocation = TextLocation.Bottom;
            barcode2.TextColor = Color.Blue;
            barcode2.Draw(page, new PointF(0, y));
            y = barcode2.Bounds.Bottom + 5;


            //draw Code128-A
            text.Text = "Code128-A:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode128ABarcode barcode3 = new PdfCode128ABarcode("HELLO 00-123");
            barcode3.BarcodeToTextGapHeight = 1f;
            barcode3.TextDisplayLocation = TextLocation.Bottom;
            barcode3.TextColor = Color.Blue;
            barcode3.Draw(page, new PointF(0, y));
            y = barcode3.Bounds.Bottom + 5;


            //draw Code128-B
            text.Text = "Code128-B:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode128BBarcode barcode4 = new PdfCode128BBarcode("Hello 00-123");
            barcode4.BarcodeToTextGapHeight = 1f;
            barcode4.TextDisplayLocation = TextLocation.Bottom;
            barcode4.TextColor = Color.Blue;
            barcode4.Draw(page, new PointF(0, y));
            y = barcode4.Bounds.Bottom + 5;


            //draw Code32
            text.Text = "Code32:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode32Barcode barcode5 = new PdfCode32Barcode("16273849");
            barcode5.BarcodeToTextGapHeight = 1f;
            barcode5.TextDisplayLocation = TextLocation.Bottom;
            barcode5.TextColor = Color.Blue;
            barcode5.Draw(page, new PointF(0, y));
            y = barcode5.Bounds.Bottom + 5;

            page = section.Pages.Add();
            y = 10;


            //draw Code39
            text.Text = "Code39:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode39Barcode barcode6 = new PdfCode39Barcode("16-273849");
            barcode6.BarcodeToTextGapHeight = 1f;
            barcode6.TextDisplayLocation = TextLocation.Bottom;
            barcode6.TextColor = Color.Blue;
            barcode6.Draw(page, new PointF(0, y));
            y = barcode6.Bounds.Bottom + 5;


            //draw Code39-E
            text.Text = "Code39-E:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode39ExtendedBarcode barcode7 = new PdfCode39ExtendedBarcode("16-273849");
            barcode7.BarcodeToTextGapHeight = 1f;
            barcode7.TextDisplayLocation = TextLocation.Bottom;
            barcode7.TextColor = Color.Blue;
            barcode7.Draw(page, new PointF(0, y));
            y = barcode7.Bounds.Bottom + 5;


            //draw Code93
            text.Text = "Code93:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode93Barcode barcode8 = new PdfCode93Barcode("16-273849");
            barcode8.BarcodeToTextGapHeight = 1f;
            barcode8.TextDisplayLocation = TextLocation.Bottom;
            barcode8.TextColor = Color.Blue;
            barcode8.QuietZone.Bottom = 5;
            barcode8.Draw(page, new PointF(0, y));
            y = barcode8.Bounds.Bottom + 5;


            //draw Code93-E
            text.Text = "Code93-E:";
            result = text.Draw(page, 0, y);
            page = result.Page;
            y = result.Bounds.Bottom + 2;

            PdfCode93ExtendedBarcode barcode9 = new PdfCode93ExtendedBarcode("16-273849");
            barcode9.BarcodeToTextGapHeight = 1f;
            barcode9.TextDisplayLocation = TextLocation.Bottom;
            barcode9.TextColor = Color.Blue;
            barcode9.Draw(page, new PointF(0, y));
            y = barcode9.Bounds.Bottom + 5;


            //Save pdf file.
            doc.SaveToFile("Barcode.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("Barcode.pdf");
        }