コード例 #1
0
ファイル: ImageTypes.cs プロジェクト: andlic/iTextInAction2Ed
// ---------------------------------------------------------------------------

        /**
         * Creates a PDF document.
         */
        public byte[] CreatePdf()
        {
            using (MemoryStream ms = new MemoryStream()) {
                using (Document document = new Document()) {
                    PdfWriter writer = PdfWriter.GetInstance(document, ms);
                    document.Open();
                    Image img = null;
                    for (int i = 0; i < RESOURCES.Length; i++)
                    {
                        img = Image.GetInstance(Path.Combine(Utility.ResourceImage, RESOURCES[i]));
                        if (img.ScaledWidth > 300 || img.ScaledHeight > 300)
                        {
                            img.ScaleToFit(300, 300);
                        }
                        document.Add(new Paragraph(String.Format(
                                                       "{0} is an image of type {1}", RESOURCES[i], img.GetType())
                                                   ));
                        document.Add(img);
                    }

/*
 * you DO NOT want to use classes within the System.Drawing namespace to
 * manipulate image files in ASP.NET applications, see the warning here:
 * http://msdn.microsoft.com/en-us/library/system.drawing.aspx
 */
                    using (System.Drawing.Image dotnet_img =
                               System.Drawing.Image.FromFile(
                                   Path.Combine(Utility.ResourceImage, RESOURCE)
                                   ))
                    {
                        img = Image.GetInstance(
                            dotnet_img, System.Drawing.Imaging.ImageFormat.Png
                            );
                        document.Add(new Paragraph(String.Format(
                                                       "{0} is an image of type {1}", "System.Drawing.Image", img.GetType())
                                                   ));
                        document.Add(img);
                    }

                    BarcodeEAN codeEAN = new BarcodeEAN();
                    codeEAN.CodeType = Barcode.EAN13;
                    codeEAN.Code     = "9781935182610";
                    img = codeEAN.CreateImageWithBarcode(writer.DirectContent, null, null);
                    document.Add(new Paragraph(String.Format(
                                                   "{0} is an image of type {1}", "barcode", img.GetType())
                                               ));
                    document.Add(img);

                    BarcodePDF417 pdf417 = new BarcodePDF417();
                    string        text   = "iText in Action, a book by Bruno Lowagie.";
                    pdf417.SetText(text);
                    img = pdf417.GetImage();
                    document.Add(new Paragraph(String.Format(
                                                   "{0} is an image of type {1}", "barcode", img.GetType())
                                               ));
                    document.Add(img);
                }
                return(ms.ToArray());
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: dborgards/Datamatrix.NET
        private Image GetImage(string text, CodeType codeType, PdfContentByte pcb)
        {
            switch (codeType)
            {
            case CodeType.Code128:
                Barcode128 barcode128 = new Barcode128 {
                    AltText = text, Code = text
                };
                return(barcode128.CreateImageWithBarcode(pcb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK));

            case CodeType.Code39:
                Barcode39 barcode39 = new Barcode39 {
                    Code = text, AltText = text
                };
                return(barcode39.CreateImageWithBarcode(pcb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK));

            case CodeType.Codabar:
                BarcodeCodabar barcodeCodabar = new BarcodeCodabar {
                    Code = text, AltText = text
                };
                return(barcodeCodabar.CreateImageWithBarcode(pcb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK));

            case CodeType.Datamatrix:
                BarcodeDatamatrix barcodeDatamatrix = new BarcodeDatamatrix();
                barcodeDatamatrix.Generate(text);
                return(barcodeDatamatrix.CreateImage());

            case CodeType.EAN:
                BarcodeEAN barcodeEAN = new BarcodeEAN {
                    CodeType = Barcode.EAN13, Code = text, AltText = text
                };
                return(barcodeEAN.CreateImageWithBarcode(pcb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK));

            case CodeType.Inter25:
                BarcodeInter25 barcodeInter25 = new BarcodeInter25
                {
                    AltText = text, Code = text, GenerateChecksum = true
                };
                return(barcodeInter25.CreateImageWithBarcode(pcb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK));

            case CodeType.PDF417:
                BarcodePDF417 barcodePDF417 = new BarcodePDF417();
                barcodePDF417.SetText(text);
                return(barcodePDF417.GetImage());

            case CodeType.Postnet:
                BarcodePostnet barcodePostnet = new BarcodePostnet {
                    AltText = text, Code = text
                };
                return(barcodePostnet.CreateImageWithBarcode(pcb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK));
            }
            return(null);
        }
コード例 #3
0
        /// <summary>
        /// Создать объект штрих кода по типу и самому коду.
        /// </summary>
        /// <param name="type">Тип кода, поддерживаются Barcode.EAN8, Barcode.EAN13, Barcode.CODE128</param>
        /// <param name="code">Содержание кода</param>
        /// <returns></returns>
        private static Barcode CreateBarcode(string type, string code, bool displayText = true, float barHeight = 0)
        {
            Barcode barcode = null;

            switch (type)
            {
            case BarcodeType.Ean8:
            {
                barcode          = new BarcodeEAN();
                barcode.CodeType = Barcode.EAN8;
                barcode.Code     = AddEANCheckSum(code, 8);
                break;
            }

            case BarcodeType.Ean13:
            {
                barcode          = new BarcodeEAN();
                barcode.CodeType = Barcode.EAN13;
                barcode.Code     = AddEANCheckSum(code, 13);
                break;
            }

            case BarcodeType.Code128:
            case BarcodeType.Ean128:
            {
                barcode                  = new Barcode128();
                barcode.Code             = code;
                barcode.ChecksumText     = true;
                barcode.GenerateChecksum = true;
                barcode.StartStopText    = true;
                break;
            }

            default:
                throw new System.Exception("Not supported barcode type");
            }

            // Установим дополнительные параметры
            if (barHeight > 0)
            {
                barcode.BarHeight = barHeight; // Высота одной полоски
            }
            if (displayText == false)
            {
                barcode.Font = null; // no text
            }
            // barcode.X = 5; // длина 1-й полоски
            // barcode.Size = 8; // размер шрифта

            return(barcode);
        }
コード例 #4
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            var retangulo = new iTextSharp.text.Rectangle(765, 765);
            var documento = new Document(retangulo);

            var writer = PdfWriter.GetInstance(documento, new FileStream(@"c:\teste\teste.pdf", FileMode.Create));

            documento.Open();

            var imagemDoTopo = iTextSharp.text.Image.GetInstance(@"c:\teste\imagem.png");

            imagemDoTopo.SetAbsolutePosition(0, 100);
            documento.Add(imagemDoTopo);

            PdfContentByte cb         = writer.DirectContent;
            BaseFont       outraFonte = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false, false);

            cb.BeginText();
            cb.SetFontAndSize(outraFonte, 12);
            cb.SetColorFill(new BaseColor(51, 51, 51));
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "TESTE", 50, 35, 50);
            cb.EndText();

            var paramQR = new Dictionary <EncodeHintType, object>();

            paramQR.Add(EncodeHintType.CHARACTER_SET, CharacterSetECI.GetCharacterSetECIByName("UTF-8"));
            BarcodeQRCode qrCodigo = new BarcodeQRCode("https://www.youtube.com/channel/UCcOGfx3w8BRxkfSyZFsW5fQ",
                                                       150, 150, paramQR);

            iTextSharp.text.Image imgBarCode = qrCodigo.GetImage();
            imgBarCode.SetAbsolutePosition(200, 200);
            documento.Add(imgBarCode);

            BarcodeEAN codeEAN13 = null;

            codeEAN13                  = new BarcodeEAN();
            codeEAN13.CodeType         = Barcode.EAN13;
            codeEAN13.ChecksumText     = true;
            codeEAN13.GenerateChecksum = true;
            codeEAN13.BarHeight        = 12;
            codeEAN13.Code             = "1234567890123";
            imgBarCode                 = codeEAN13.CreateImageWithBarcode(cb, null, null);
            imgBarCode.SetAbsolutePosition(150, 150);
            imgBarCode.Alignment = iTextSharp.text.Image.TEXTWRAP;
            documento.Add(imgBarCode);



            documento.Close();
        }
コード例 #5
0
        /// <summary>
        /// Generar el Barcode EAN
        /// </summary>
        /// <param name="text">Mensaje que será pasado a 25</param>
        /// <param name="pdfDoc">PdfDocument del documento original</param>
        /// <returns>Retornar el EAN en una imágen</returns>
        private Image GetBarcodeEan(PdfDocument pdfDoc)
        {
            BarcodeEAN barcodeEan = new BarcodeEAN(pdfDoc);

            barcodeEan.SetCode("0123456789012");

            Image barcode = new Image(barcodeEan.CreateFormXObject(null, null, pdfDoc));

            barcode.Scale(2, 2);
            barcode.SetHorizontalAlignment(HorizontalAlignment.LEFT);
            barcode.SetMarginLeft(20);
            barcode.SetMarginTop(10);

            return(barcode);
        }
コード例 #6
0
ファイル: RotatedText.cs プロジェクト: anqin0725/i7ns-samples
        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc, new PageSize(60, 140));

            doc.SetMargins(5, 5, 5, 5);

            PdfFont boldFont    = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
            PdfFont regularFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);

            Paragraph p1 = new Paragraph();

            p1.Add(new Text("23").SetFont(boldFont).SetFontSize(12));
            p1.Add(new Text("000").SetFont(boldFont).SetFontSize(6));
            doc.Add(p1);

            Paragraph p2 = new Paragraph("T.T.C.").SetFont(regularFont).SetFontSize(6);

            p2.SetTextAlignment(TextAlignment.RIGHT);
            doc.Add(p2);

            BarcodeEAN barcode = new BarcodeEAN(pdfDoc);

            barcode.SetCodeType(BarcodeEAN.EAN8);
            barcode.SetCode("12345678");

            Rectangle      rect        = barcode.GetBarcodeSize();
            PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(rect.GetWidth(), rect.GetHeight() + 10));
            PdfCanvas      pdfCanvas   = new PdfCanvas(formXObject, pdfDoc);

            new Canvas(pdfCanvas, new Rectangle(rect.GetWidth(), rect.GetHeight() + 10))
            .ShowTextAligned(new Paragraph("DARK GRAY").SetFont(regularFont).SetFontSize(6), 0,
                             rect.GetHeight() + 2, TextAlignment.LEFT);
            barcode.PlaceBarcode(pdfCanvas, ColorConstants.BLACK, ColorConstants.BLACK);

            Image image = new Image(formXObject);

            image.SetRotationAngle(Math.PI / 2f);
            image.SetAutoScale(true);
            doc.Add(image);

            Paragraph p3 = new Paragraph("SMALL").SetFont(regularFont).SetFontSize(6);

            p3.SetTextAlignment(TextAlignment.CENTER);
            doc.Add(p3);

            doc.Close();
        }
コード例 #7
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            FileStream fs     = new FileStream("Barcode.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
            Document   pdfdoc = new Document();
            PdfWriter  writer = PdfWriter.GetInstance(pdfdoc, fs);

            pdfdoc.Open();
            PdfContentByte cb = writer.DirectContent;

            BarcodeEAN ean13 = new BarcodeEAN();

            ean13.ChecksumText = true;
            ean13.CodeType     = BarcodeEAN.EAN13;

            if (textBox1.Text.Length == 12)
            {
                int checkSum = calculateChecksum(textBox1.Text);
                ean13.Code = textBox1.Text + checkSum.ToString();
                iTextSharp.text.Image barcodeImage = ean13.CreateImageWithBarcode(cb, null, null);
                pdfdoc.Add(barcodeImage);
                pdfdoc.Close();
                MessageBox.Show("PDF generated correct!");
            }
            else if (textBox1.Text.Length == 13)
            {
                if (CheckCode(textBox1.Text))
                {
                    ean13.Code = textBox1.Text;
                    iTextSharp.text.Image barcodeImage = ean13.CreateImageWithBarcode(cb, null, null);
                    pdfdoc.Add(barcodeImage);
                    pdfdoc.Close();
                    MessageBox.Show("PDF generated correct!");
                }
                else
                {
                    MessageBox.Show("Control digit is incorrect!");
                    fs.Close();
                }
            }
            else
            {
                MessageBox.Show("Length of correct sum should be 12 or 13!");
                fs.Close();
            }
        }
コード例 #8
0
        private static Cell CreateBarcode(string code, PdfDocument pdfDoc)
        {
            BarcodeEAN barcode = new BarcodeEAN(pdfDoc);

            barcode.SetCodeType(BarcodeEAN.EAN8);
            barcode.SetCode(code);

            // Create barcode object to put it to the cell as image
            PdfFormXObject barcodeObject = barcode.CreateFormXObject(null, null, pdfDoc);
            Cell           cell          = new Cell().Add(new Image(barcodeObject));

            cell.SetPaddingTop(10);
            cell.SetPaddingRight(10);
            cell.SetPaddingBottom(10);
            cell.SetPaddingLeft(10);

            return(cell);
        }
コード例 #9
0
        public virtual void BarcodeEANTest()
        {
            String   outPdf = destinationFolder + "barcodeEANTest.pdf";
            String   cmpPdf = cmpFolder + "cmp_barcodeEANTest.pdf";
            Document doc    = CreatePdfATaggedDocument(outPdf);
            PdfFont  font   = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);

            font.SetSubset(true);
            BarcodeEAN codeEAN = new BarcodeEAN(doc.GetPdfDocument(), font);

            FillBarcode1D(codeEAN, "9781935182610");
            PdfFormXObject barcode = codeEAN.CreateFormXObject(doc.GetPdfDocument());
            Image          img     = new Image(barcode).SetMargins(0, 0, 0, 0);

            img.GetAccessibilityProperties().SetAlternateDescription("hello world!");
            doc.Add(img);
            doc.Close();
            CompareResult(outPdf, cmpPdf);
        }
コード例 #10
0
ファイル: Codigos.cs プロジェクト: NexusSoft/EtiquetasGTIN
        public static Bitmap CodigosBarraUPCA(string _Code, Single Height = 0)
        {
            BarcodeEAN uccEan128 = new BarcodeEAN();

            uccEan128.CodeType = Barcode.UPCA;
            if (Height != 0)
            {
                uccEan128.BarHeight = 20;
            }
            uccEan128.Code = _Code;

            try
            {
                Bitmap bm = new Bitmap(uccEan128.CreateDrawingImage(Color.Black, Color.White));
                return(bm);
            }
            catch (Exception ex)
            {
                throw new Exception("Error al generar el codigo" + ex.ToString());
            }
        }
コード例 #11
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
            {
                PdfPage    pdfPage  = pdfDoc.GetPage(i);
                Rectangle  pageSize = pdfPage.GetPageSize();
                float      x        = pageSize.GetLeft() + 10;
                float      y        = pageSize.GetTop() - 50;
                BarcodeEAN barcode  = new BarcodeEAN(pdfDoc);
                barcode.SetCodeType(BarcodeEAN.EAN8);
                barcode.SetCode(CreateBarcodeNumber(i));

                PdfFormXObject barcodeXObject = barcode.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);
                PdfCanvas      over           = new PdfCanvas(pdfPage);
                over.AddXObjectAt(barcodeXObject, x, y);
            }

            pdfDoc.Close();
        }
コード例 #12
0
ファイル: PDF.cs プロジェクト: Ciraq/PDFCreator
        public static void CreatePdf(string path)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(path, FileMode.Create)));
            Document    doc    = new Document(pdfDoc, PageSize.A4);

            //Create Barcode EAN13
            string     code  = "1234567890128";
            BarcodeEAN EAN13 = new BarcodeEAN(pdfDoc);

            EAN13.SetFont(null);
            EAN13.SetCode(code);
            EAN13.SetCodeType(BarcodeEAN.EAN13);

            Image image13 = new Image(EAN13.CreateFormXObject(pdfDoc));

            image13.SetHorizontalAlignment(HorizontalAlignment.CENTER);

            Paragraph name = new Paragraph("Product Name").SetTextAlignment(TextAlignment.CENTER);

            name.SetFontSize(10);
            Paragraph ProductCode = new Paragraph("1234567890128").SetTextAlignment(TextAlignment.CENTER);

            ProductCode.SetFontSize(10);

            //Create table
            float[] columnWidths = { 5, 5 };
            Table   table        = new Table(columnWidths);

            table.SetWidth(UnitValue.CreatePercentValue(100));

            for (int i = 0; i < 100; i++)
            {
                table.AddCell(new Cell().SetBorder(Border.NO_BORDER).SetTextAlignment(TextAlignment.CENTER).Add(name).Add(image13).Add(ProductCode));
            }

            doc.Add(table);
            doc.Close();
        }
コード例 #13
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (rbt_ean13.Checked)
            {
                ean          = new BarcodeEAN();
                regex        = new Regex("^[0-9]{13}");
                ean.CodeType = BarcodeEAN.EAN13;
                FileName     = "EAN13_" + TextBox1.Text + ".pdf";
                RegularExpressionValidator2.Display = ValidatorDisplay.Dynamic;
                RegularExpressionValidator1.Display = ValidatorDisplay.None;
                RegularExpressionValidator2.Enabled = true;
                RegularExpressionValidator1.Enabled = false;
                TextBox1.MaxLength   = 13;
                TextBox1.Width       = 255;
                cbxTextAbove.Enabled = true;
                cbxGuardBars.Enabled = true;
                LinkButton1.Visible  = true;
            }
            else if (rbt_ean8.Checked)
            {
                ean          = new BarcodeEAN();
                regex        = new Regex("^[0-9]{8}");
                ean.CodeType = BarcodeEAN.EAN8;
                FileName     = "EAN8_" + TextBox1.Text + ".pdf";
                RegularExpressionValidator2.Display = ValidatorDisplay.None;
                RegularExpressionValidator1.Display = ValidatorDisplay.Dynamic;
                RegularExpressionValidator2.Enabled = false;
                RegularExpressionValidator1.Enabled = true;
                TextBox1.MaxLength   = 8;
                TextBox1.Width       = 157;
                cbxTextAbove.Enabled = true;
                cbxGuardBars.Enabled = true;
                LinkButton1.Visible  = true;
            }
            else if (rbt_qr.Checked)
            {
                regex    = new Regex("^[0-9a-zA-Z:/.]+");
                FileName = TextBox1.Text;
                FileName = FileName.Substring(0, Math.Min(24, FileName.Length));
                string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
                foreach (char c in invalid)
                {
                    FileName = FileName.Replace(c.ToString(), "");
                }

                FileName = "QR_" + FileName + ".pdf";
                RegularExpressionValidator2.Display = ValidatorDisplay.None;
                RegularExpressionValidator1.Display = ValidatorDisplay.None;
                RegularExpressionValidator2.Enabled = false;
                RegularExpressionValidator1.Enabled = false;
                TextBox1.MaxLength   = 1852;
                TextBox1.Width       = 255;
                cbxTextAbove.Enabled = false;
                cbxGuardBars.Enabled = false;
                LinkButton1.Visible  = false;
            }

            if (regex.Match(TextBox1.Text).Success)
            {
                if (rbt_qr.Checked)
                {
                    qr = new BarcodeQRCode(TextBox1.Text, 200, 200, null);
                }
                else
                {
                    ean.ChecksumText     = true;
                    ean.GenerateChecksum = true;
                    ean.StartStopText    = true;
                    BaseFont ocrb = BaseFont.CreateFont(AppPath + "Ocrb.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
                    ean.Font      = ocrb;
                    ean.GuardBars = cbxGuardBars.Checked;
                    ean.Code      = TextBox1.Text;
                    if (cbxTextAbove.Checked && !cbxGuardBars.Checked)
                    {
                        ean.Baseline = -1f;
                    }
                }
            }
        }
コード例 #14
0
        public void barcodeEAN13Test()
        {
            // arrange
            BarcodeEAN    barcode   = new BarcodeEAN();
            Bitmap        image_1   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\1.gif");
            Bitmap        image_2   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\2.gif");
            Bitmap        image_3   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\3.gif");
            Bitmap        image_4   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\4.gif");
            Bitmap        image_5   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\5.jpg");
            Bitmap        image_6   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\6.jpg");
            Bitmap        image_7   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\7.jpg");
            Bitmap        image_8   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\8.gif");
            Bitmap        image_9   = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\9.gif");
            Bitmap        image_10  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\10.gif");
            Bitmap        image_11  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\11.gif");
            Bitmap        image_12  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\12.gif");
            Bitmap        image_13  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\13.gif");
            Bitmap        image_14  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\14.gif");
            Bitmap        image_15  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\15.gif");
            Bitmap        image_16  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\16.gif");
            Bitmap        image_17  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\17.gif");
            Bitmap        image_18  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\18.gif");
            Bitmap        image_19  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\19.gif");
            Bitmap        image_20  = new Bitmap("D:\\ИАД Курсач\\image\\EAN13\\20.gif");
            List <Bitmap> imageList = new List <Bitmap>();

            imageList.Add(image_1);
            imageList.Add(image_2);
            imageList.Add(image_3);
            imageList.Add(image_4);
            imageList.Add(image_5);
            imageList.Add(image_6);
            imageList.Add(image_7);
            imageList.Add(image_8);
            imageList.Add(image_9);
            imageList.Add(image_10);
            imageList.Add(image_11);
            imageList.Add(image_12);
            imageList.Add(image_13);
            imageList.Add(image_14);
            imageList.Add(image_15);
            imageList.Add(image_16);
            imageList.Add(image_17);
            imageList.Add(image_18);
            imageList.Add(image_19);
            imageList.Add(image_20);
            int count = 0;

            string[] barcodeOtvet = { "9780201379624", "9780202279626", "8880202279624", "8870211279616", "8870202279625", "1870211279613", "1870211272003", "1440211272008", "9940211272005", "9940211272012",
                                      "9702230379628", "9802230379625", "9812230379624", "981223037978",  "9780278379626", "1110278379629", "1110108379638", "1220108379634", "8220108379637", "8221978379635" };
            string   barcodeEAN = "";

            string[] result;
            for (int t = 0; t < 20; t++)
            {
                string otvet = "";
                Color[,] All = new Color[imageList[t].Width - 1, imageList[t].Height - 1];
                for (int i = 0; i < (imageList[t].Width - 1); i++)
                {
                    for (int j = 0; j < (imageList[t].Height - 1); j++)
                    {
                        All[i, j] = imageList[t].GetPixel(i, j);
                    }
                }
                // act
                barcodeEAN = "";
                result     = barcode.barcodeEAN13(imageList[t], All);
                for (int i = 0; i < 13; i++)
                {
                    barcodeEAN += result[i];
                }

                // assert

                if (barcodeEAN == barcodeOtvet[t])
                {
                    otvet = "    Функція повернула вірне значення"; count++;
                }
                else
                {
                    otvet = "   Функція повертає невірне значення";
                }
                Debug.WriteLine("Правильна відповідь має бути: " + barcodeOtvet[t] + "   Функція повернула значення: " + barcodeEAN + otvet);
            }
            Debug.Write("Кількість правильно розпізнаних штрих-кодів: " + count);
        }
コード例 #15
0
        public void barcodeEAN8Test()
        {    // arrange
            BarcodeEAN barcode = new BarcodeEAN();
            //Bitmap image= new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\EAN8.gif");

            Bitmap        image_1   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\1.gif");
            Bitmap        image_2   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\2.gif");
            Bitmap        image_3   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\3.gif");
            Bitmap        image_4   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\4.gif");
            Bitmap        image_5   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\5.jpg");
            Bitmap        image_6   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\6.gif");
            Bitmap        image_7   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\7.gif");
            Bitmap        image_8   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\8.gif");
            Bitmap        image_9   = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\9.gif");
            Bitmap        image_10  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\10.gif");
            Bitmap        image_11  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\11.gif");
            Bitmap        image_12  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\12.gif");
            Bitmap        image_13  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\13.gif");
            Bitmap        image_14  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\14.gif");
            Bitmap        image_15  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\15.gif");
            Bitmap        image_16  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\16.gif");
            Bitmap        image_17  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\17.gif");
            Bitmap        image_18  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\18.gif");
            Bitmap        image_19  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\19.gif");
            Bitmap        image_20  = new Bitmap("D:\\ИАД Курсач\\image\\EAN8\\20.gif");
            List <Bitmap> imageList = new List <Bitmap>();

            imageList.Add(image_1);
            imageList.Add(image_2);
            imageList.Add(image_3);
            imageList.Add(image_4);
            imageList.Add(image_5);
            imageList.Add(image_6);
            imageList.Add(image_7);
            imageList.Add(image_8);
            imageList.Add(image_9);
            imageList.Add(image_10);
            imageList.Add(image_11);
            imageList.Add(image_12);
            imageList.Add(image_13);
            imageList.Add(image_14);
            imageList.Add(image_15);
            imageList.Add(image_16);
            imageList.Add(image_17);
            imageList.Add(image_18);
            imageList.Add(image_19);
            imageList.Add(image_20);
            int count = 0;

            string [] barcodeOtvet = { "90311017", "80211013", "90333330", "12345670", "88345673", "81005673", "18762471", "22762474", "22112477", "22112118",
                                       "92211209", "97211204", "97211105", "97288107", "77288103", "77988102", "67988105", "12088102", "19088105", "99088101" };
            string    barcodeEAN = "";

            string[] result;
            for (int t = 0; t < 20; t++)
            {
                string otvet = "";

                Color[,] All = new Color[imageList[t].Width - 1, imageList[t].Height - 1];
                for (int i = 0; i < (imageList[t].Width - 1); i++)
                {
                    for (int j = 0; j < (imageList[t].Height - 1); j++)
                    {
                        All[i, j] = imageList[t].GetPixel(i, j);
                    }
                }
                // act
                barcodeEAN = "";
                result     = barcode.barcodeEAN8(imageList[t], All);
                for (int i = 0; i < 8; i++)
                {
                    barcodeEAN += result[i];
                }

                // assert
                // Assert.AreEqual(barcodeEAN, barcoderesult[t]);
                if (barcodeEAN == barcodeOtvet[t])
                {
                    otvet = "    Функція повернула вірне значення"; count++;
                }
                else
                {
                    otvet = "   Функція повертає невірне значення";
                }
                Debug.WriteLine("Правильна відповідь має бути: " + barcodeOtvet[t] + "   Функція повернула значення: " + barcodeEAN + otvet);
            }
            Debug.Write("Кількість правильно розпізнаних штрих-кодів: " + count);
        }
コード例 #16
0
// ===========================================================================
        public void Write(Stream stream)
        {
            // step 1
            using (Document document = new Document(new Rectangle(340, 842))) {
                // step 2
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                // step 3
                document.Open();
                // step 4
                PdfContentByte cb = writer.DirectContent;

                // EAN 13
                document.Add(new Paragraph("Barcode EAN.UCC-13"));
                BarcodeEAN codeEAN = new BarcodeEAN();
                codeEAN.Code = "4512345678906";
                document.Add(new Paragraph("default:"));
                document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
                codeEAN.GuardBars = false;
                document.Add(new Paragraph("without guard bars:"));
                document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
                codeEAN.Baseline  = -1f;
                codeEAN.GuardBars = true;
                document.Add(new Paragraph("text above:"));
                document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
                codeEAN.Baseline = codeEAN.Size;

                // UPC A
                document.Add(new Paragraph("Barcode UCC-12 (UPC-A)"));
                codeEAN.CodeType = Barcode.UPCA;
                codeEAN.Code     = "785342304749";
                document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));

                // EAN 8
                document.Add(new Paragraph("Barcode EAN.UCC-8"));
                codeEAN.CodeType  = Barcode.EAN8;
                codeEAN.BarHeight = codeEAN.Size * 1.5f;
                codeEAN.Code      = "34569870";
                document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));

                // UPC E
                document.Add(new Paragraph("Barcode UPC-E"));
                codeEAN.CodeType = Barcode.UPCE;
                codeEAN.Code     = "03456781";
                document.Add(codeEAN.CreateImageWithBarcode(cb, null, null));
                codeEAN.BarHeight = codeEAN.Size * 3f;

                // EANSUPP
                document.Add(new Paragraph("Bookland"));
                document.Add(new Paragraph("ISBN 0-321-30474-8"));
                codeEAN.CodeType = Barcode.EAN13;
                codeEAN.Code     = "9781935182610";
                BarcodeEAN codeSUPP = new BarcodeEAN();
                codeSUPP.CodeType = Barcode.SUPP5;
                codeSUPP.Code     = "55999";
                codeSUPP.Baseline = -2;
                BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
                document.Add(eanSupp.CreateImageWithBarcode(cb, null, BaseColor.BLUE));

                // CODE 128
                document.Add(new Paragraph("Barcode 128"));
                Barcode128 code128 = new Barcode128();
                code128.Code = "0123456789 hello";
                document.Add(code128.CreateImageWithBarcode(cb, null, null));
                code128.Code     = "0123456789\uffffMy Raw Barcode (0 - 9)";
                code128.CodeType = Barcode.CODE128_RAW;
                document.Add(code128.CreateImageWithBarcode(cb, null, null));

                // Data for the barcode :
                String        code402 = "24132399420058289";
                String        code90  = "3700000050";
                String        code421 = "422356";
                StringBuilder data    = new StringBuilder(code402);
                data.Append(Barcode128.FNC1);
                data.Append(code90);
                data.Append(Barcode128.FNC1);
                data.Append(code421);
                Barcode128 shipBarCode = new Barcode128();
                shipBarCode.X             = 0.75f;
                shipBarCode.N             = 1.5f;
                shipBarCode.Size          = 10f;
                shipBarCode.TextAlignment = Element.ALIGN_CENTER;
                shipBarCode.Baseline      = 10f;
                shipBarCode.BarHeight     = 50f;
                shipBarCode.Code          = data.ToString();
                document.Add(shipBarCode.CreateImageWithBarcode(
                                 cb, BaseColor.BLACK, BaseColor.BLUE
                                 ));

                // it is composed of 3 blocks whith AI 01, 3101 and 10
                Barcode128 uccEan128 = new Barcode128();
                uccEan128.CodeType = Barcode.CODE128_UCC;
                uccEan128.Code     = "(01)00000090311314(10)ABC123(15)060916";
                document.Add(uccEan128.CreateImageWithBarcode(
                                 cb, BaseColor.BLUE, BaseColor.BLACK
                                 ));
                uccEan128.Code = "0191234567890121310100035510ABC123";
                document.Add(uccEan128.CreateImageWithBarcode(
                                 cb, BaseColor.BLUE, BaseColor.RED
                                 ));
                uccEan128.Code = "(01)28880123456788";
                document.Add(uccEan128.CreateImageWithBarcode(
                                 cb, BaseColor.BLUE, BaseColor.BLACK
                                 ));

                // INTER25
                document.Add(new Paragraph("Barcode Interleaved 2 of 5"));
                BarcodeInter25 code25 = new BarcodeInter25();
                code25.GenerateChecksum = true;
                code25.Code             = "41-1200076041-001";
                document.Add(code25.CreateImageWithBarcode(cb, null, null));
                code25.Code = "411200076041001";
                document.Add(code25.CreateImageWithBarcode(cb, null, null));
                code25.Code         = "0611012345678";
                code25.ChecksumText = true;
                document.Add(code25.CreateImageWithBarcode(cb, null, null));

                // POSTNET
                document.Add(new Paragraph("Barcode Postnet"));
                BarcodePostnet codePost = new BarcodePostnet();
                document.Add(new Paragraph("ZIP"));
                codePost.Code = "01234";
                document.Add(codePost.CreateImageWithBarcode(cb, null, null));
                document.Add(new Paragraph("ZIP+4"));
                codePost.Code = "012345678";
                document.Add(codePost.CreateImageWithBarcode(cb, null, null));
                document.Add(new Paragraph("ZIP+4 and dp"));
                codePost.Code = "01234567890";
                document.Add(codePost.CreateImageWithBarcode(cb, null, null));

                document.Add(new Paragraph("Barcode Planet"));
                BarcodePostnet codePlanet = new BarcodePostnet();
                codePlanet.Code     = "01234567890";
                codePlanet.CodeType = Barcode.PLANET;
                document.Add(codePlanet.CreateImageWithBarcode(cb, null, null));

                // CODE 39
                document.Add(new Paragraph("Barcode 3 of 9"));
                Barcode39 code39 = new Barcode39();
                code39.Code = "ITEXT IN ACTION";
                document.Add(code39.CreateImageWithBarcode(cb, null, null));

                document.Add(new Paragraph("Barcode 3 of 9 extended"));
                Barcode39 code39ext = new Barcode39();
                code39ext.Code          = "iText in Action";
                code39ext.StartStopText = false;
                code39ext.Extended      = true;
                document.Add(code39ext.CreateImageWithBarcode(cb, null, null));

                // CODABAR
                document.Add(new Paragraph("Codabar"));
                BarcodeCodabar codabar = new BarcodeCodabar();
                codabar.Code          = "A123A";
                codabar.StartStopText = true;
                document.Add(codabar.CreateImageWithBarcode(cb, null, null));

                // PDF417
                document.Add(new Paragraph("Barcode PDF417"));
                BarcodePDF417 pdf417 = new BarcodePDF417();
                String        text   = "Call me Ishmael. Some years ago--never mind how long "
                                       + "precisely --having little or no money in my purse, and nothing "
                                       + "particular to interest me on shore, I thought I would sail about "
                                       + "a little and see the watery part of the world."
                ;
                pdf417.SetText(text);
                Image img = pdf417.GetImage();
                img.ScalePercent(50, 50 * pdf417.YHeight);
                document.Add(img);

                document.Add(new Paragraph("Barcode Datamatrix"));
                BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
                datamatrix.Generate(text);
                img = datamatrix.CreateImage();
                document.Add(img);

                document.Add(new Paragraph("Barcode QRCode"));
                BarcodeQRCode qrcode = new BarcodeQRCode(
                    "Moby Dick by Herman Melville", 1, 1, null
                    );
                img = qrcode.GetImage();
                document.Add(img);
            }
        }
コード例 #17
0
        private void btPdf_Click(object sender, EventArgs e)
        {
            try
            {
                /* ============ PRIMEIRA FORMA ============
                 * //var retangulo = new iTextSharp.text.Rectangle(765, 765);
                 * var documento = new Document(PageSize.A4, 10, 10, 10, 10);
                 *
                 * var writer = PdfWriter.GetInstance(documento, new FileStream(@"c:\teste\teste.pdf", FileMode.Create));
                 *
                 * documento.Open();
                 *
                 * var imagem = iTextSharp.text.Image.GetInstance(@"C:\teste\robin.png");
                 * imagem.SetAbsolutePosition(10,60);
                 * documento.Add(imagem);
                 *
                 * PdfContentByte cb = writer.DirectContent;
                 * BaseFont fonte = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false, false);
                 *
                 * cb.BeginText();
                 * cb.SetFontAndSize(fonte, 12);
                 * cb.SetColorFill(new BaseColor(51, 51, 51));
                 * cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Damian Wayne\nTeste", 20, 800, 0);
                 *
                 * int linhas = dgvDados.RowCount;
                 * int colunas = dgvDados.ColumnCount;
                 *
                 * for (int i = 0; i < linhas; i++)
                 * {
                 *  for (int x = 0; x < colunas; x++)
                 *  {
                 *      cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Convert.ToString(dgvDados.Rows[i].Cells[x].Value), 20, 400, 0);
                 *  }
                 * }
                 *
                 * cb.EndText();
                 * documento.Close();
                 *
                 * MessageBox.Show("PDF gerado com sucesso!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 */

                /*========== SEGUNDA FORMA ==========
                 * CHUNK: é a menor parte de um texto a ser inserido (palavra)
                 * PHRASE: é um conjunto de palavras (frases)
                 * PARAGRAPH: é um paragrafo (conjunto de frases)
                 */
                var documento = new Document(PageSize.A4, 10, 10, 10, 10);

                Stream str;

                SaveFileDialog salvarRelatorioPDF = new SaveFileDialog();

                salvarRelatorioPDF.Filter = "PDF (*.pdf) | *.pdf";

                salvarRelatorioPDF.FilterIndex      = 2;
                salvarRelatorioPDF.Title            = "Salvar Relatorio em PDF";
                salvarRelatorioPDF.RestoreDirectory = true;
                var salvar_dir = "";


                if (salvarRelatorioPDF.ShowDialog() == DialogResult.OK)
                {
                    if ((str = salvarRelatorioPDF.OpenFile()) != null)
                    {
                        salvar_dir = salvarRelatorioPDF.FileName;
                    }
                    else
                    {
                        DialogResult x = MessageBox.Show("Não Foi possivel salvar", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    str.Close();
                }


                var writer = PdfWriter.GetInstance(documento, new FileStream(salvar_dir, FileMode.Create));

                documento.Open();

                var imagem = iTextSharp.text.Image.GetInstance(@"../../../image.png");
                imagem.SetAbsolutePosition(20, 775);
                documento.Add(imagem);

                var imagem2 = iTextSharp.text.Image.GetInstance(@"../../../image2.png");
                imagem2.SetAbsolutePosition(500, 775);
                documento.Add(imagem2);


                // Definindo as fontes
                var titulo = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16, 2, BaseColor.RED);

                var fonte = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 12, 0, BaseColor.BLACK);

                var letra = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 10, 0, BaseColor.BLACK);

                var rodape = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.SYMBOL, 13, 0, BaseColor.BLUE);

                documento.Add(new Paragraph(""));
                documento.Add(new Paragraph(""));
                documento.Add(new Paragraph(""));
                //criando parágrafo
                var paragrafo1 = new Paragraph("R E L A T O R Í O", titulo);
                paragrafo1.Alignment = Element.ALIGN_CENTER;
                documento.Add(paragrafo1);

                var nome1 = new Paragraph("Lucas Anselmo", fonte);
                nome1.Alignment = Element.ALIGN_CENTER;
                documento.Add(nome1);

                var nome2 = new Paragraph("João Vinicius" + "\n" + "_____________________________________________________________________________", fonte);
                nome2.Alignment = Element.ALIGN_CENTER;
                documento.Add(nome2);

                //data do relatorio
                var paragrafo2 = new Paragraph("ITU, " + DateTime.Now.ToLongDateString());
                paragrafo2.Alignment = Element.ALIGN_RIGHT;
                documento.Add(paragrafo2);

                int  linhas  = dgvDados.RowCount;
                int  colunas = dgvDados.ColumnCount;
                var  frase   = new Phrase();
                bool flag    = false;

                for (int i = 0; i < linhas; i++)
                {
                    for (int x = 0; x < colunas; x++)
                    {
                        frase = new Phrase(Convert.ToString(dgvDados.Rows[i].Cells[x].Value) + " - ", letra);
                        //documento.Add(frase);
                        flag = documento.Add(frase);
                    }
                    frase = new Phrase("\n");
                    flag  = documento.Add(frase);
                }

                //gerar codigo de barras
                PdfContentByte cb = writer.DirectContent;
                cb.BeginText();
                iTextSharp.text.Image imgBarCode;

                BarcodeEAN codeEAN = null;
                codeEAN                  = new BarcodeEAN();
                codeEAN.CodeType         = Barcode.EAN13;
                codeEAN.ChecksumText     = true;
                codeEAN.GenerateChecksum = true;
                codeEAN.BarHeight        = 12;
                codeEAN.Code             = "1234567890123";
                imgBarCode               = codeEAN.CreateImageWithBarcode(cb, null, null);
                imgBarCode.SetAbsolutePosition(20, 350);
                imgBarCode.Alignment = iTextSharp.text.Image.TEXTWRAP;
                documento.Add(imgBarCode);
                cb.EndText();

                //Gerar QRCODE
                var paramQR = new Dictionary <EncodeHintType, object>();
                paramQR.Add(EncodeHintType.CHARACTER_SET, CharacterSetECI.GetCharacterSetECIByName("UTF-8"));
                BarcodeQRCode         qrcodigo  = new BarcodeQRCode("http://www.etecitu.com.br", 150, 150, paramQR);
                iTextSharp.text.Image imgQrCode = qrcodigo.GetImage();
                imgQrCode.SetAbsolutePosition(410, 300);
                documento.Add(imgQrCode);

                documento.Close();

                if (flag == true)
                {
                    DialogResult d = MessageBox.Show("Relatorio gerado com sucesso!\nDeseja abri-lo?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (d.ToString() == "Yes")
                    {
                        System.Diagnostics.Process.Start(@salvar_dir);
                    }
                }
                else
                {
                    MessageBox.Show("Não foi gerado");
                }
            }
            catch (Exception erro)
            {
                MessageBox.Show(erro.Message);
            }
        }
コード例 #18
0
        public void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc, new PageSize(340, 842));

            // The default barcode EAN 13 type
            doc.Add(new Paragraph("Barcode EAN.UCC-13"));
            BarcodeEAN codeEAN = new BarcodeEAN(pdfDoc);

            codeEAN.SetCode("4512345678906");
            doc.Add(new Paragraph("default:"));
            codeEAN.FitWidth(250);
            doc.Add(new Image(codeEAN.CreateFormXObject(pdfDoc)));
            codeEAN.SetGuardBars(false);
            doc.Add(new Paragraph("without guard bars:"));
            doc.Add(new Image(codeEAN.CreateFormXObject(pdfDoc)));
            codeEAN.SetBaseline(-1);
            codeEAN.SetGuardBars(true);
            doc.Add(new Paragraph("text above:"));
            doc.Add(new Image(codeEAN.CreateFormXObject(pdfDoc)));
            codeEAN.SetBaseline(codeEAN.GetSize());

            // Barcode EAN UPC A type
            doc.Add(new Paragraph("Barcode UCC-12 (UPC-A)"));
            codeEAN.SetCodeType(BarcodeEAN.UPCA);
            codeEAN.SetCode("785342304749");
            doc.Add(new Image(codeEAN.CreateFormXObject(pdfDoc)));

            // Barcode EAN 8 type
            doc.Add(new Paragraph("Barcode EAN.UCC-8"));
            codeEAN.SetCodeType(BarcodeEAN.EAN8);
            codeEAN.SetBarHeight(codeEAN.GetSize() * 1.5f);
            codeEAN.SetCode("34569870");
            codeEAN.FitWidth(250);
            doc.Add(new Image(codeEAN.CreateFormXObject(pdfDoc)));

            // Barcode UPC E type
            doc.Add(new Paragraph("Barcode UPC-E"));
            codeEAN.SetCodeType(BarcodeEAN.UPCE);
            codeEAN.SetCode("03456781");
            codeEAN.FitWidth(250);
            doc.Add(new Image(codeEAN.CreateFormXObject(pdfDoc)));
            codeEAN.SetBarHeight(codeEAN.GetSize() * 3);

            // Barcode EANSUPP type
            doc.Add(new Paragraph("Bookland - BarcodeEANSUPP"));
            doc.Add(new Paragraph("ISBN 0-321-30474-8"));
            codeEAN = new BarcodeEAN(pdfDoc);
            codeEAN.SetCodeType(BarcodeEAN.EAN13);
            codeEAN.SetCode("9781935182610");
            BarcodeEAN codeSUPP = new BarcodeEAN(pdfDoc);

            codeSUPP.SetCodeType(BarcodeEAN.SUPP5);
            codeSUPP.SetCode("55999");
            codeSUPP.SetBaseline(-2);
            BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);

            doc.Add(new Image(eanSupp.CreateFormXObject(null, ColorConstants.BLUE, pdfDoc)));

            // Barcode CODE 128 type
            doc.Add(new Paragraph("Barcode 128"));
            Barcode128 code128 = new Barcode128(pdfDoc);

            code128.SetCode("0123456789 hello");
            code128.FitWidth(250);
            doc.Add(new Image(code128.CreateFormXObject(pdfDoc))
                    .SetRotationAngle(Math.PI / 2)
                    .SetMargins(10, 10, 10, 10));
            code128.SetCode("0123456789\uffffMy Raw Barcode (0 - 9)");
            code128.SetCodeType(Barcode128.CODE128_RAW);
            code128.FitWidth(250);
            doc.Add(new Image(code128.CreateFormXObject(pdfDoc)));

            // Data for the barcode
            String        code402 = "24132399420058289";
            String        code90  = "3700000050";
            String        code421 = "422356";
            StringBuilder data    = new StringBuilder(code402);

            data.Append(Barcode128.FNC1);
            data.Append(code90);
            data.Append(Barcode128.FNC1);
            data.Append(code421);

            Barcode128 shipBarCode = new Barcode128(pdfDoc);

            shipBarCode.SetX(0.75f);
            shipBarCode.SetN(1.5f);
            shipBarCode.SetSize(10f);
            shipBarCode.SetTextAlignment(Barcode1D.ALIGN_CENTER);
            shipBarCode.SetBaseline(10f);
            shipBarCode.SetBarHeight(50f);
            shipBarCode.SetCode(data.ToString());
            shipBarCode.FitWidth(250);
            doc.Add(new Image(shipBarCode.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLUE, pdfDoc)));

            // CODE 128 type barcode, which is composed of 3 blocks with AI 01, 3101 and 10
            Barcode128 uccEan128 = new Barcode128(pdfDoc);

            uccEan128.SetCodeType(Barcode128.CODE128_UCC);
            uccEan128.SetCode("(01)00000090311314(10)ABC123(15)060916");
            uccEan128.FitWidth(250);
            doc.Add(new Image(uccEan128.CreateFormXObject(ColorConstants.BLUE, ColorConstants.BLACK, pdfDoc)));
            uccEan128.SetCode("0191234567890121310100035510ABC123");
            uccEan128.FitWidth(250);
            doc.Add(new Image(uccEan128.CreateFormXObject(ColorConstants.BLUE, ColorConstants.RED, pdfDoc)));
            uccEan128.SetCode("(01)28880123456788");
            uccEan128.FitWidth(250);
            doc.Add(new Image(uccEan128.CreateFormXObject(ColorConstants.BLUE, ColorConstants.BLACK, pdfDoc)));

            // Barcode INTER25 type
            doc.Add(new Paragraph("Barcode Interrevealed 2 of 5"));
            BarcodeInter25 code25 = new BarcodeInter25(pdfDoc);

            code25.SetGenerateChecksum(true);
            code25.SetCode("41-1200076041-001");
            code25.FitWidth(250);
            doc.Add(new Image(code25.CreateFormXObject(pdfDoc)));
            code25.SetCode("411200076041001");
            code25.FitWidth(250);
            doc.Add(new Image(code25.CreateFormXObject(pdfDoc)));
            code25.SetCode("0611012345678");
            code25.SetChecksumText(true);
            code25.FitWidth(250);
            doc.Add(new Image(code25.CreateFormXObject(pdfDoc)));

            // Barcode POSTNET type
            doc.Add(new Paragraph("Barcode Postnet"));
            BarcodePostnet codePost = new BarcodePostnet(pdfDoc);

            doc.Add(new Paragraph("ZIP"));
            codePost.SetCode("01234");
            codePost.FitWidth(250);
            doc.Add(new Image(codePost.CreateFormXObject(pdfDoc)));
            doc.Add(new Paragraph("ZIP+4"));
            codePost.SetCode("012345678");
            codePost.FitWidth(250);
            doc.Add(new Image(codePost.CreateFormXObject(pdfDoc)));
            doc.Add(new Paragraph("ZIP+4 and dp"));
            codePost.SetCode("01234567890");
            codePost.FitWidth(250);
            doc.Add(new Image(codePost.CreateFormXObject(pdfDoc)));

            // Barcode PLANET type
            doc.Add(new Paragraph("Barcode Planet"));
            BarcodePostnet codePlanet = new BarcodePostnet(pdfDoc);

            codePlanet.SetCode("01234567890");
            codePlanet.SetCodeType(BarcodePostnet.TYPE_PLANET);
            codePlanet.FitWidth(250);
            doc.Add(new Image(codePlanet.CreateFormXObject(pdfDoc)));

            // Barcode CODE 39 type
            doc.Add(new Paragraph("Barcode 3 of 9"));
            Barcode39 code39 = new Barcode39(pdfDoc);

            code39.SetCode("ITEXT IN ACTION");
            code39.FitWidth(250);
            doc.Add(new Image(code39.CreateFormXObject(pdfDoc)));

            doc.Add(new Paragraph("Barcode 3 of 9 extended"));
            Barcode39 code39ext = new Barcode39(pdfDoc);

            code39ext.SetCode("iText in Action");
            code39ext.SetStartStopText(false);
            code39ext.SetExtended(true);
            code39ext.FitWidth(250);
            doc.Add(new Image(code39ext.CreateFormXObject(pdfDoc)));

            // Barcode CODABAR type
            doc.Add(new Paragraph("Codabar"));
            BarcodeCodabar codabar = new BarcodeCodabar(pdfDoc);

            codabar.SetCode("A123A");
            codabar.SetStartStopText(true);
            codabar.FitWidth(250);
            doc.Add(new Image(codabar.CreateFormXObject(pdfDoc)));

            doc.Add(new AreaBreak());

            // Barcode PDF417 type
            doc.Add(new Paragraph("Barcode PDF417"));
            BarcodePDF417 pdf417 = new BarcodePDF417();
            String        text   = "Call me Ishmael. Some years ago--never mind how long "
                                   + "precisely --having little or no money in my purse, and nothing "
                                   + "particular to interest me on shore, I thought I would sail about "
                                   + "a little and see the watery part of the world.";

            pdf417.SetCode(text);

            PdfFormXObject xObject = pdf417.CreateFormXObject(pdfDoc);
            Image          img     = new Image(xObject);

            doc.Add(img.SetAutoScale(true));

            doc.Add(new Paragraph("Barcode Datamatrix"));
            BarcodeDataMatrix datamatrix = new BarcodeDataMatrix();

            datamatrix.SetCode(text);
            Image imgDM = new Image(datamatrix.CreateFormXObject(pdfDoc));

            doc.Add(imgDM.ScaleToFit(250, 250));

            // Barcode QRCode type
            doc.Add(new Paragraph("Barcode QRCode"));
            BarcodeQRCode qrcode = new BarcodeQRCode("Moby Dick by Herman Melville");

            img = new Image(qrcode.CreateFormXObject(pdfDoc));
            doc.Add(img.ScaleToFit(250, 250));

            doc.Close();
        }
コード例 #19
0
        public Chap0907()
        {
            Console.WriteLine("Chapter 9 example 7: Barcodes without ttf");

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a PDF-stream to a file

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0907.pdf", FileMode.Create));

                // step 3: we open the document
                document.Open();

                // step 4: we add content to the document
                PdfContentByte cb     = writer.DirectContent;
                Barcode39      code39 = new Barcode39();
                code39.Code          = "CODE39-1234567890";
                code39.StartStopText = false;
                Image     image39   = code39.CreateImageWithBarcode(cb, null, null);
                Barcode39 code39ext = new Barcode39();
                code39ext.Code          = "The willows.";
                code39ext.StartStopText = false;
                code39ext.Extended      = true;
                Image      image39ext = code39ext.CreateImageWithBarcode(cb, null, null);
                Barcode128 code128    = new Barcode128();
                code128.Code = "1Z234786 hello";
                Image      image128 = code128.CreateImageWithBarcode(cb, null, null);
                BarcodeEAN codeEAN  = new BarcodeEAN();
                codeEAN.CodeType = BarcodeEAN.EAN13;
                codeEAN.Code     = "9780201615883";
                Image          imageEAN = codeEAN.CreateImageWithBarcode(cb, null, null);
                BarcodeInter25 code25   = new BarcodeInter25();
                code25.GenerateChecksum = true;
                code25.Code             = "41-1200076041-001";
                Image          image25  = code25.CreateImageWithBarcode(cb, null, null);
                BarcodePostnet codePost = new BarcodePostnet();
                codePost.Code = "12345";
                Image          imagePost  = codePost.CreateImageWithBarcode(cb, null, null);
                BarcodePostnet codePlanet = new BarcodePostnet();
                codePlanet.Code     = "50201402356";
                codePlanet.CodeType = BarcodePostnet.PLANET;
                Image       imagePlanet = codePlanet.CreateImageWithBarcode(cb, null, null);
                PdfTemplate tp          = cb.CreateTemplate(0, 0);
                PdfTemplate ean         = codeEAN.CreateTemplateWithBarcode(cb, null, new Color(System.Drawing.Color.Blue));
                BarcodeEAN  codeSUPP    = new BarcodeEAN();
                codeSUPP.CodeType = BarcodeEAN.SUPP5;
                codeSUPP.Code     = "54995";
                codeSUPP.Baseline = -2;
                BarcodeEANSUPP eanSupp      = new BarcodeEANSUPP(codeEAN, codeSUPP);
                Image          imageEANSUPP = eanSupp.CreateImageWithBarcode(cb, null, new Color(System.Drawing.Color.Blue));
                PdfPTable      table        = new PdfPTable(2);
                table.WidthPercentage    = 100;
                table.DefaultCell.Border = Rectangle.NO_BORDER;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                table.DefaultCell.VerticalAlignment   = Element.ALIGN_MIDDLE;
                table.DefaultCell.FixedHeight         = 70;
                table.AddCell("CODE 39");
                table.AddCell(new Phrase(new Chunk(image39, 0, 0)));
                table.AddCell("CODE 39 EXTENDED");
                table.AddCell(new Phrase(new Chunk(image39ext, 0, 0)));
                table.AddCell("CODE 128");
                table.AddCell(new Phrase(new Chunk(image128, 0, 0)));
                table.AddCell("CODE EAN");
                table.AddCell(new Phrase(new Chunk(imageEAN, 0, 0)));
                table.AddCell("CODE EAN\nWITH\nSUPPLEMENTAL 5");
                table.AddCell(new Phrase(new Chunk(imageEANSUPP, 0, 0)));
                table.AddCell("CODE INTERLEAVED");
                table.AddCell(new Phrase(new Chunk(image25, 0, 0)));
                table.AddCell("CODE POSTNET");
                table.AddCell(new Phrase(new Chunk(imagePost, 0, 0)));
                table.AddCell("CODE PLANET");
                table.AddCell(new Phrase(new Chunk(imagePlanet, 0, 0)));
                document.Add(table);
            }
            catch (Exception de)
            {
                Console.Error.WriteLine(de.StackTrace);
            }

            // step 5: we close the document
            document.Close();
        }