Пример #1
0
        public void Encode_Barcoder_ShouldEncodePdf417CodeCorrectly()
        {
            // Arrange
            var content          = "Barcoder";
            var expectedDataBits = ImageStringToBools(@"
                ++++++++.+.+.+...+++.+.+.+++......+++.+.+...+++....+++.+..++...++++.++++.+.++..+++++.+++++.+.+.+++++..+++++++.+...+.+..+
                ++++++++.+.+.+...+++.+.+.+++......+++.+.+...+++....+++.+..++...++++.++++.+.++..+++++.+++++.+.+.+++++..+++++++.+...+.+..+
                ++++++++.+.+.+...++++++.+.+...+++.++++.+..+....+...+++.+..+++..++...+..++.+++++..+...++++++.+.+.+++...+++++++.+...+.+..+
                ++++++++.+.+.+...++++++.+.+...+++.++++.+..+....+...+++.+..+++..++...+..++.+++++..+...++++++.+.+.+++...+++++++.+...+.+..+
                ++++++++.+.+.+...+.+.+..++++......++...+++++..+..+.+...++..+....+++.+....++..++.++++.+++.+.+...++++++.+++++++.+...+.+..+
                ++++++++.+.+.+...+.+.+..++++......++...+++++..+..+.+...++..+....+++.+....++..++.++++.+++.+.+...++++++.+++++++.+...+.+..+
                ++++++++.+.+.+...++.+.++++.+++++..+.++....+++...++.+++.+..+..+++....++....++....+..+.++.+.++++..+++++.+++++++.+...+.+..+
                ++++++++.+.+.+...++.+.++++.+++++..+.++....+++...++.+++.+..+..+++....++....++....+..+.++.+.++++..+++++.+++++++.+...+.+..+
                ++++++++.+.+.+...+++.+.+++....++..+++..+.++..+.....+++.+...++.....+.+.++++++..+.++...+++.+.+++..++....+++++++.+...+.+..+
                ++++++++.+.+.+...+++.+.+++....++..+++..+.++..+.....+++.+...++.....+.+.++++++..+.++...+++.+.+++..++....+++++++.+...+.+..+
            ");

            // Act
            IBarcode pdf417 = Pdf417Encoder.Encode(content, 2);

            // Assert
            pdf417.Should().NotBeNull();
            expectedDataBits.Length.Should().Be(pdf417.Bounds.X * pdf417.Bounds.Y);
            for (int i = 0; i < expectedDataBits.Length; i++)
            {
                int x = i % pdf417.Bounds.X;
                int y = i / pdf417.Bounds.X;
                pdf417.At(x, y).Should().Be(expectedDataBits[i], $"of expected bit on index {i}");
            }
        }
Пример #2
0
        ////////////////////////////////////////////////////////////////////
        // Draw Barcode
        ////////////////////////////////////////////////////////////////////

        private void DrawPdf417Barcode()
        {
            // save graphics state
            Contents.SaveGraphicsState();

            // create PDF417 barcode
            Pdf417Encoder Pdf417 = new Pdf417Encoder();

            Pdf417.DefaultDataColumns = 3;
            Pdf417.Encode(ArticleLink);
            Pdf417.WidthToHeightRatio(2.5);

            // convert Pdf417 to black and white image
            PdfImage BarcodeImage = new PdfImage(Document);

            BarcodeImage.LoadImage(Pdf417);

            // draw image
            Contents.DrawImage(BarcodeImage, 1.1, 5.2, 2.5);

            // define a web link area coinsiding with the qr code
            double Height = 2.5 * Pdf417.ImageHeight / Pdf417.ImageWidth;

            Page.AddWebLink(1.1, 5.2, 1.1 + 2.5, 5.2 + Height, ArticleLink);

            // restore graphics sate
            Contents.RestoreGraphicsState();
            return;
        }
Пример #3
0
        public void Test
        (
            bool Debug,
            string InputFileName
        )
        {
            // create document
            using (Document = new PdfDocument(PaperType.Letter, false, UnitOfMeasure.Inch, InputFileName))
            {
                // set document page mode to open the layers panel
                Document.InitialDocDisplay = InitialDocDisplay.UseLayers;

                // define font
                ArialFont = PdfFont.CreatePdfFont(Document, "Arial", FontStyle.Bold);

                // open layer control object (in PDF terms optional content object)
                PdfLayers Layers = new PdfLayers(Document, "PDF layers group");

                // set layer panel to incluse all layers including ones that are not visible
                Layers.ListMode = ListMode.AllPages;

                // Add new page
                PdfPage Page = new PdfPage(Document);

                // Add contents to page
                PdfContents Contents = new PdfContents(Page);

                // heading
                Contents.DrawText(ArialFont, 24, 4.25, 10, TextJustify.Center, "PDF File Writer Layer Test/Demo");

                // define layers
                PdfLayer DrawingTest    = new PdfLayer(Layers, "Drawing Test");
                PdfLayer Rectangle      = new PdfLayer(Layers, "Rectangle");
                PdfLayer HorLines       = new PdfLayer(Layers, "Horizontal Lines");
                PdfLayer VertLines      = new PdfLayer(Layers, "Vertical Lines");
                PdfLayer QRCodeLayer    = new PdfLayer(Layers, "QRCode barcode");
                PdfLayer Pdf417Layer    = new PdfLayer(Layers, "PDF417 barcode");
                PdfLayer NoBarcodeLayer = new PdfLayer(Layers, "No barcode");

                // combine three layers into one group of radio buttons
                QRCodeLayer.RadioButton    = "Barcode";
                Pdf417Layer.RadioButton    = "Barcode";
                NoBarcodeLayer.RadioButton = "Barcode";

                // set the order of layers in the layer pane
                Layers.DisplayOrder(DrawingTest);
                Layers.DisplayOrder(Rectangle);
                Layers.DisplayOrder(HorLines);
                Layers.DisplayOrder(VertLines);
                Layers.DisplayOrderStartGroup("Barcode group");
                Layers.DisplayOrder(QRCodeLayer);
                Layers.DisplayOrder(Pdf417Layer);
                Layers.DisplayOrder(NoBarcodeLayer);
                Layers.DisplayOrderEndGroup();

                // start a group layer
                Contents.LayerStart(DrawingTest);

                // sticky note annotation
                PdfAnnotation StickyNote = Page.AddStickyNote(2.0, 9.0, "My sticky note", StickyNoteIcon.Note);
                StickyNote.LayerControl = DrawingTest;

                // draw a single layer
                Contents.LayerStart(Rectangle);
                Contents.DrawText(ArialFont, 14, 1.0, 8.0, TextJustify.Left, "Draw rectangle");
                Contents.LayerEnd();

                // draw a single layer
                Contents.LayerStart(HorLines);
                Contents.DrawText(ArialFont, 14, 1.0, 7.5, TextJustify.Left, "Draw horizontal lines");
                Contents.LayerEnd();

                // draw a single layer
                Contents.LayerStart(VertLines);
                Contents.DrawText(ArialFont, 14, 1.0, 7.0, TextJustify.Left, "Draw vertical lines");
                Contents.LayerEnd();

                double Left   = 4.0;
                double Right  = 7.0;
                double Top    = 9.0;
                double Bottom = 6.0;

                // draw a single layer
                Contents.LayerStart(Rectangle);
                Contents.SaveGraphicsState();
                Contents.SetLineWidth(0.1);
                Contents.SetColorStroking(Color.Black);
                Contents.SetColorNonStroking(Color.LightBlue);
                Contents.DrawRectangle(Left, Bottom, 3.0, 3.0, PaintOp.CloseFillStroke);
                Contents.RestoreGraphicsState();
                Contents.LayerEnd();

                // save graphics state
                Contents.SaveGraphicsState();

                // draw a single layer
                Contents.SetLineWidth(0.02);
                Contents.LayerStart(HorLines);
                for (int Row = 1; Row < 6; Row++)
                {
                    Contents.DrawLine(Left, Bottom + 0.5 * Row, Right, Bottom + 0.5 * Row);
                }
                Contents.LayerEnd();

                // draw a single layer
                Contents.LayerStart(VertLines);
                for (int Col = 1; Col < 6; Col++)
                {
                    Contents.DrawLine(Left + 0.5 * Col, Bottom, Left + 0.5 * Col, Top);
                }
                Contents.LayerEnd();

                // restore graphics state
                Contents.RestoreGraphicsState();

                // terminate a group of layers
                Contents.LayerEnd();

                // define QRCode barcode
                QREncoder QREncoder = new QREncoder();
                QREncoder.ErrorCorrection = ErrorCorrection.M;
                QREncoder.Encode(QRCodeArticle);
                PdfImage QRImage = new PdfImage(Document);
                QRImage.LoadImage(QREncoder);

                // define PDF417 barcode
                Pdf417Encoder Pdf417Encoder = new Pdf417Encoder();
                Pdf417Encoder.ErrorCorrection = ErrorCorrectionLevel.AutoMedium;
                Pdf417Encoder.Encode(Pdf417Article);
                PdfImage Pdf417Image = new PdfImage(Document);
                Pdf417Image.LoadImage(Pdf417Encoder);

                // draw a single layer
                Contents.LayerStart(QRCodeLayer);
                Contents.DrawText(ArialFont, 14, 1.0, 2.5, TextJustify.Left, "QRCode Barcode");
                Contents.DrawImage(QRImage, 3.7, 2.5 - 1.75, 3.5);
                Contents.LayerEnd();

                // draw a single layer
                Contents.LayerStart(Pdf417Layer);
                Contents.DrawText(ArialFont, 14, 1.0, 2.5, TextJustify.Left, "PDF417 Barcode");
                Contents.DrawImage(Pdf417Image, 3.7, 2.5 - 1.75 * Pdf417Encoder.ImageHeight / Pdf417Encoder.ImageWidth, 3.5);
                Contents.LayerEnd();

                // draw a single layer
                Contents.LayerStart(NoBarcodeLayer);
                Contents.DrawText(ArialFont, 14, 1.0, 3.0, TextJustify.Left, "Display no barcode");
                Contents.LayerEnd();

                // create pdf file
                Document.CreateFile();

                // start default PDF reader and display the file
                Process Proc = new Process();
                Proc.StartInfo = new ProcessStartInfo(InputFileName);
                Proc.Start();
            }
            return;
        }
        /////////////////////////////////////////////////////////////////////
        // create pdf417 barcode image file
        /////////////////////////////////////////////////////////////////////

        private void OnEncode(object sender, EventArgs e)
        {
            // get bar width
            if (!int.TryParse(BarWidthTextBox.Text.Trim(), out int BarWidth) || BarWidth < 1 || BarWidth > 20)
            {
                MessageBox.Show("Narrow bar width must be 1 to 20 pixels");
                return;
            }

            // get row height
            if (!int.TryParse(RowHeightTextBox.Text.Trim(), out int RowHeight) || RowHeight < 3 * BarWidth || RowHeight > 60)
            {
                MessageBox.Show("Row height must be 3 times bar width or more in pixels");
                return;
            }

            // get quiet zone
            if (!int.TryParse(QuietZoneTextBox.Text.Trim(), out int QuietZone) || QuietZone < 2 * BarWidth || RowHeight > 40)
            {
                MessageBox.Show("Quiet zone must be 2 times bar width or more in pixels");
                return;
            }

            // get default data columns
            if (!int.TryParse(DefDataColTextBox.Text.Trim(), out int DefDataColumns) || DefDataColumns < 1 || DefDataColumns > 30)
            {
                MessageBox.Show("Number of data columns must be 1 to 30");
                return;
            }

            // disable buttons
            EnableButtons(false);

            // clear current image
            Pdf417Bitmap = null;

            // trap encoding errors
            try
            {
                // encoding request
                Encoder.EncodingControl = (EncodingControl)EncodingComboBox.SelectedIndex;

                // error correction request
                Encoder.ErrorCorrection = (ErrorCorrectionLevel)ErrorCorrectionComboBox.SelectedIndex;

                // language or character set
                int Part = CharacterSetComboBox.SelectedIndex;
                if (Part == 10)
                {
                    Part = 15;
                }
                else if (Part == 9)
                {
                    Part = 13;
                }
                else
                {
                    Part++;
                }
                Encoder.GlobalLabelIDCharacterSet = string.Format("ISO-8859-{0}", Part);

                // narrow bar width
                Encoder.NarrowBarWidth = BarWidth;

                // row height
                Encoder.RowHeight = RowHeight;

                // quiet zone
                Encoder.QuietZone = QuietZone;

                // default data columns
                Encoder.DefaultDataColumns = DefDataColumns;

                // load barcode data
                Encoder.Encode(DataTextBox.Text);

                // create bitmap image
                Pdf417Bitmap = Encoder.CreateBarcodeBitmap();

                // update screen labels
                WidthHeightLabel.Text = ((double)Encoder.ImageWidth / Encoder.ImageHeight).ToString("0.0");
                DataRowsLabel.Text    = Encoder.DataRows.ToString();
                DataColumnsLabel.Text = Encoder.DataColumns.ToString();


#if DEBUG
                string ArgLine = string.Format("-Col:{0} -Error:{1} -Width:{2} -Height:{3} -Quiet:{4} -t \"{5}\" \"{6}\"",
                                               10, 7, 6, 20, 15, "TestFile.txt", "Test Image.png");
                Pdf417CommandLine.Encode(ArgLine);
#endif
            }

            catch (Exception Ex)
            {
                MessageBox.Show("Encoding exception.\r\n" + Ex.Message);
            }

            // enable buttons
            EnableButtons(true);

            // repaint panel
            Invalidate();
            return;
        }
Пример #5
0
        public static void Encode
        (
            string[] Args
        )
        {
            // help
            if (Args == null || Args.Length < 2)
            {
                throw new ArgumentException("help");
            }

            int    DataColumns    = 0;
            int    DataRows       = 0;
            double Ratio          = 0;
            bool   TextFile       = false;
            string CharacterSet   = null;
            string InputFileName  = null;
            string OutputFileName = null;
            string Code;
            string Value;

            Pdf417Encoder Encoder = new Pdf417Encoder();

            for (int ArgPtr = 0; ArgPtr < Args.Length; ArgPtr++)
            {
                string Arg = Args[ArgPtr];

                // file name
                if (Arg[0] != '/' && Arg[0] != '-')
                {
                    if (InputFileName == null)
                    {
                        InputFileName = Arg;
                        continue;
                    }
                    if (OutputFileName == null)
                    {
                        OutputFileName = Arg;
                        continue;
                    }
                    throw new ArgumentException(string.Format("Invalid option. Argument={0}", ArgPtr + 1));
                }

                // search for colon
                int Ptr = Arg.IndexOf(':');
                if (Ptr < 0)
                {
                    Ptr = Arg.IndexOf('=');
                }
                if (Ptr > 0)
                {
                    Code  = Arg.Substring(1, Ptr - 1);
                    Value = Arg.Substring(Ptr + 1);
                }
                else
                {
                    Code  = Arg.Substring(1);
                    Value = string.Empty;
                }

                Code  = Code.ToLower();
                Value = Value.ToLower();

                switch (Code)
                {
                case "encode":
                case "n":
                    EncodingControl EC;
                    switch (Value)
                    {
                    case "auto":
                    case "a":
                        EC = EncodingControl.Auto;
                        break;

                    case "byte":
                    case "b":
                        EC = EncodingControl.ByteOnly;
                        break;

                    case "text":
                    case "t":
                        EC = EncodingControl.TextAndByte;
                        break;

                    default:
                        throw new ArgumentException("Encoding control option in error");
                    }
                    Encoder.EncodingControl = EC;
                    break;

                case "error":
                case "e":
                    ErrorCorrectionLevel ECL;
                    switch (Value)
                    {
                    case "0":
                        ECL = ErrorCorrectionLevel.Level_0;
                        break;

                    case "1":
                        ECL = ErrorCorrectionLevel.Level_1;
                        break;

                    case "2":
                        ECL = ErrorCorrectionLevel.Level_2;
                        break;

                    case "3":
                        ECL = ErrorCorrectionLevel.Level_3;
                        break;

                    case "4":
                        ECL = ErrorCorrectionLevel.Level_4;
                        break;

                    case "5":
                        ECL = ErrorCorrectionLevel.Level_5;
                        break;

                    case "6":
                        ECL = ErrorCorrectionLevel.Level_6;
                        break;

                    case "7":
                        ECL = ErrorCorrectionLevel.Level_7;
                        break;

                    case "8":
                        ECL = ErrorCorrectionLevel.Level_8;
                        break;

                    case "low":
                    case "l":
                        ECL = ErrorCorrectionLevel.AutoLow;
                        break;

                    case "normal":
                    case "n":
                        ECL = ErrorCorrectionLevel.AutoNormal;
                        break;

                    case "medium":
                    case "m":
                        ECL = ErrorCorrectionLevel.AutoMedium;
                        break;

                    case "high":
                    case "h":
                        ECL = ErrorCorrectionLevel.AutoHigh;
                        break;

                    default:
                        throw new ArgumentException("Error correction level option in error");
                    }
                    Encoder.ErrorCorrection = ECL;
                    break;

                case "width":
                case "w":
                    if (!int.TryParse(Value, out int BarWidth))
                    {
                        BarWidth = -1;
                    }
                    Encoder.NarrowBarWidth = BarWidth;
                    break;

                case "height":
                case "h":
                    if (!int.TryParse(Value, out int RowHeight))
                    {
                        RowHeight = -1;
                    }
                    Encoder.RowHeight = RowHeight;
                    break;

                case "quiet":
                case "q":
                    if (!int.TryParse(Value, out int QuietZone))
                    {
                        QuietZone = -1;
                    }
                    Encoder.QuietZone = QuietZone;
                    break;

                case "col":
                case "c":
                    if (DataRows != 0 || Ratio != 0)
                    {
                        throw new ArgumentException("Only one value is allowed for Data Rows, Data Columns and Ratio");
                    }
                    if (!int.TryParse(Value, out DataColumns) || DataColumns < 1 || DataColumns > 30)
                    {
                        throw new ApplicationException("Data columns in error");
                    }
                    break;

                case "row":
                case "r":
                    if (DataColumns != 0 || Ratio != 0)
                    {
                        throw new ArgumentException("Only one value is allowed for Data Rows, Data Columns and Ratio");
                    }
                    if (!int.TryParse(Value, out DataRows) || DataRows < 3 || DataRows > 90)
                    {
                        throw new ApplicationException("Data Rows in error");
                    }
                    break;

                case "ratio":
                case "o":
                    if (DataRows != 0 || DataColumns != 0)
                    {
                        throw new ArgumentException("Only one value is allowed for Data Rows, Data Columns and Ratio");
                    }
                    if (!double.TryParse(Value, out Ratio) || Ratio < 0.1 || Ratio > 100)
                    {
                        throw new ApplicationException("Image aspect ratio (width/height) in error");
                    }
                    break;

                case "text":
                case "t":
                    TextFile = true;
                    if (Value != string.Empty)
                    {
                        CharacterSet = Value;
                    }
                    break;

                default:
                    throw new ApplicationException(string.Format("Invalid argument no {0}, code {1}", ArgPtr + 1, Code));
                }
            }

            if (TextFile)
            {
                string InputText = File.ReadAllText(InputFileName);
                Encoder.Encode(InputText);
            }
            else
            {
                byte[] InputBytes = File.ReadAllBytes(InputFileName);
                Encoder.Encode(InputBytes);
            }

            if (DataColumns != 0)
            {
                if (!Encoder.SetDataColumns(DataColumns))
                {
                    throw new ApplicationException("Set data columns failed");
                }
            }
            else if (DataRows != 0)
            {
                if (!Encoder.SetDataRows(DataRows))
                {
                    throw new ApplicationException("Set data rows failed");
                }
            }
            else if (Ratio != 0)
            {
                if (!Encoder.WidthToHeightRatio(Ratio))
                {
                    throw new ApplicationException("Set width to height aspect ratio failed");
                }
            }

            Encoder.SaveBarcodeToPngFile(OutputFileName);
            return;
        }