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}"); } }
//////////////////////////////////////////////////////////////////// // 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; }
// constructor public AdjustLayout ( Pdf417Encoder Encoder ) { this.Encoder = Encoder; InitializeComponent(); return; }
/// <summary> /// SaveImage constructor /// </summary> /// <param name="Pdf417Encoder">Pdf417Encoder class</param> /// <param name="Pdf417BitmapImage">Barcode Bitmap image</param> public SaveImage ( Pdf417Encoder Pdf417Encoder, Bitmap Pdf417BitmapImage ) { // save arguments this.Pdf417Encoder = Pdf417Encoder; this.Pdf417BitmapImage = Pdf417BitmapImage; // initialize components InitializeComponent(); return; }
public PdfImage ( PdfDocument Document, Pdf417Encoder Pdf417Encoder ) : this(Document) { // barcode width and height WidthPix = Pdf417Encoder.ImageWidth; HeightPix = Pdf417Encoder.ImageHeight; // black and white barcode image BWImage = Pdf417Encoder.ConvertBarcodeMatrixToPixels(); // image control for PDF417 code ReverseBW = true; SaveAs = SaveImageAs.BWImage; // write image stream to pdf file WriteObjectToPdfFile(); return; }
/// <summary> /// Load image fro Pdf417Encoder /// </summary> /// <param name="Pdf417Encoder">Pdf417 encoder</param> public void LoadImage ( Pdf417Encoder Pdf417Encoder ) { // barcode width and height WidthPix = Pdf417Encoder.ImageWidth; HeightPix = Pdf417Encoder.ImageHeight; // black and white barcode image BWImage = Pdf417Encoder.ConvertBarcodeMatrixToPixels(); // set save as to BWImage SaveAs = SaveImageAs.BWImage; ReverseBW = true; // write to output file WriteObjectToPdfFile(); // exit return; }
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; }
///////////////////////////////////////////////////////////////////// // initialization ///////////////////////////////////////////////////////////////////// private void OnLoad(object sender, EventArgs e) { #if DEBUG // current directory string CurDir = Environment.CurrentDirectory; string WorkDir = CurDir.Replace("bin\\Debug", "Work"); if (WorkDir != CurDir && Directory.Exists(WorkDir)) { Environment.CurrentDirectory = WorkDir; } #endif // program title Text = "Pdf417EncoderDemo - " + Pdf417Encoder.VersionNumber + " \u00a9 2019 Uzi Granot. All rights reserved."; // create samples array Samples = new string[] { Text, English, Greek, Hebrew, French, Numeric, Punctuation, }; // load encoding control combo box for (int Index = 0; Index < EncodingControlText.Length; Index++) { EncodingComboBox.Items.Add(EncodingControlText[Index]); } EncodingComboBox.SelectedIndex = 0; // load error correction combo box for (int Index = 0; Index < ErrorCorrLevelText.Length; Index++) { ErrorCorrectionComboBox.Items.Add(ErrorCorrLevelText[Index]); } ErrorCorrectionComboBox.SelectedIndex = 10; // load language ISO combo box for (int Index = 0; Index < CharacterSetText.Length; Index++) { CharacterSetComboBox.Items.Add(CharacterSetText[Index]); } CharacterSetComboBox.SelectedIndex = 0; // set narrow bar width BarWidthTextBox.Text = "4"; // set row height RowHeightTextBox.Text = "12"; // set quiet zone QuietZoneTextBox.Text = "8"; // set data columns DefDataColTextBox.Text = "4"; // load samples for (int Index = 0; Index < SamplesText.Length; Index++) { SamplesComboBox.Items.Add(SamplesText[Index]); } SamplesComboBox.SelectedIndex = 0; // create encoder object Encoder = new Pdf417Encoder(); // enable buttons EnableButtons(true); // force resize OnResize(sender, e); return; }
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; }