Esempio n. 1
0
        ////////////////////////////////////////////////////////////////////
        // Draw image and clip it
        ////////////////////////////////////////////////////////////////////

        private void DrawImage()
        {
            // define local image resources
            // resolution 96 pixels per inch, image quality 50%
            PdfImage Image1 = new PdfImage(Document);

            Image1.Resolution   = 96.0;
            Image1.ImageQuality = 50;
            Image1.LoadImage("TestImage.jpg");

            // save graphics state
            Contents.SaveGraphicsState();

            // translate coordinate origin to the center of the picture
            Contents.Translate(3.75, 5.0);

            // adjust image size and preserve aspect ratio
            PdfRectangle NewSize = Image1.ImageSizePosition(1.75, 1.5, ContentAlignment.MiddleCenter);

            // clipping path
            Contents.DrawOval(NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height, PaintOp.ClipPathEor);

            // draw image
            Contents.DrawImage(Image1, NewSize.Left, NewSize.Bottom, NewSize.Width, NewSize.Height);

            // restore graphics state
            Contents.RestoreGraphicsState();
            return;
        }
Esempio n. 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;
        }
Esempio n. 3
0
        /// <summary>
        /// Add an image to the current page with a specified width
        /// </summary>
        /// <param name="path">Image file path</param>
        /// <param name="width">Target width for image in mm</param>
        /// <param name="options">ImageOptions</param>
        public void AddImage(string path, double width, ImageOptions options = null)
        {
            this.checkBuilderState();

            if (options == null)
            {
                options = new ImageOptions()
                {
                    PositionX = this.PagePositionX,
                    PositionY = this.PagePositionY
                };
            }

            // load the image from size and determine it new pixel dimensions based on relative resolutions
            var    srcBitmap            = new Bitmap(path);
            double srcBitmapAspectRatio = srcBitmap.Size.Height / (srcBitmap.Size.Width * 1.0);
            double widthInInches        = width / 25.4;
            int    widthInPixels        = Convert.ToInt32(Math.Round(widthInInches * srcBitmap.HorizontalResolution));
            int    heightInPixels       = Convert.ToInt32(Math.Round(widthInPixels * srcBitmapAspectRatio));

            // resize the source image and load into pdf
            var targetBitmap = Helpers.Image.Resize(srcBitmap, widthInPixels, heightInPixels, Color.White);

            PdfImage pdfImage = new PdfImage(this.document);

            pdfImage.Resolution   = options.Resolution;
            pdfImage.ImageQuality = options.Quality;
            pdfImage.LoadImage(targetBitmap);

            this.contents.SaveGraphicsState();

            // Height is calculated from width as per aspect ratio
            double height = width * pdfImage.HeightPix / pdfImage.WidthPix;

            SizeD newSize   = pdfImage.ImageSize(width, height);
            var   positionX = options.PositionX;
            var   positionY = options.PositionY - newSize.Height;

            this.contents.DrawImage(pdfImage, positionX, positionY, newSize.Width, newSize.Height);
            this.contents.RestoreGraphicsState();
            this.pagePosition.Y = positionY;
        }
Esempio n. 4
0
        ////////////////////////////////////////////////////////////////////
        // Draw Barcode
        ////////////////////////////////////////////////////////////////////

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

            // draw EAN13 barcode
            BarcodeEAN13 Barcode1 = new BarcodeEAN13("1234567890128");

            Contents.DrawBarcode(1.3, 7.05, 0.012, 0.75, Barcode1, ArialNormal, 8.0);

            // create QRCode barcode
            QREncoder QREncoder = new QREncoder();

            // set error correction code
            QREncoder.ErrorCorrection = ErrorCorrection.M;

            // set module size in pixels
            QREncoder.ModuleSize = 1;

            // set quiet zone in pixels
            QREncoder.QuietZone = 4;

            // encode your text or byte array
            QREncoder.Encode(ArticleLink);

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

            BarcodeImage.LoadImage(QREncoder);

            // draw image (height is the same as width for QRCode)
            Contents.DrawImage(BarcodeImage, 6.0, 6.8, 1.2);

            // define a web link area coinsiding with the qr code
            Page.AddWebLink(6.0, 6.8, 7.2, 8.0, ArticleLink);

            // restore graphics sate
            Contents.RestoreGraphicsState();
            return;
        }
Esempio n. 5
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;
        }
Esempio n. 6
0
        ////////////////////////////////////////////////////////////////////
        // Create table example
        ////////////////////////////////////////////////////////////////////

        public void CreateBookList()
        {
            // Add new page
            Page = new PdfPage(Document);

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

            PdfFont TitleFont  = PdfFont.CreatePdfFont(Document, "Verdana", FontStyle.Bold);
            PdfFont AuthorFont = PdfFont.CreatePdfFont(Document, "Verdana", FontStyle.Italic);

            // create stock table
            PdfTable BookList = new PdfTable(Page, Contents, NormalFont, 9.0);

            // divide columns width in proportion to following values
            BookList.SetColumnWidth(1.0, 2.5, 1.2, 1.0, 0.5, 0.6, 1.2);

            // event handlers
            BookList.TableStartEvent     += BookListTableStart;
            BookList.TableEndEvent       += BookListTableEnd;
            BookList.CustomDrawCellEvent += BookListDrawCellEvent;

            // set display header at the top of each additional page
            BookList.HeaderOnEachPage = true;

            // make some changes to default header style
            BookList.DefaultHeaderStyle.Alignment              = ContentAlignment.MiddleCenter;
            BookList.DefaultHeaderStyle.FontSize               = 9.0;
            BookList.DefaultHeaderStyle.MultiLineText          = true;
            BookList.DefaultHeaderStyle.TextBoxTextJustify     = TextBoxJustify.Center;
            BookList.DefaultHeaderStyle.BackgroundColor        = Color.Blue;
            BookList.DefaultHeaderStyle.ForegroundColor        = Color.LightCyan;
            BookList.DefaultHeaderStyle.TextBoxLineBreakFactor = 0.2;

            // headers
            BookList.Header[0].Value = "Book Cover";
            BookList.Header[1].Value = "Book Title and Authors";
            BookList.Header[2].Value = "Date\nPublished";
            BookList.Header[3].Value = "Type";
            BookList.Header[4].Value = "In\nStock";
            BookList.Header[5].Value = "Price";
            BookList.Header[6].Value = "Weblink";

            // default cell style
            BookList.DefaultCellStyle.Alignment = ContentAlignment.MiddleCenter;

            // create private style for type column
            BookList.Cell[3].Style = BookList.CellStyle;
            BookList.Cell[3].Style.RaiseCustomDrawCellEvent = true;

            // create private style for in stock column
            BookList.Cell[4].Style           = BookList.CellStyle;
            BookList.Cell[4].Style.Format    = "#,##0";
            BookList.Cell[4].Style.Alignment = ContentAlignment.MiddleRight;

            // create private style for price column
            BookList.Cell[5].Style           = BookList.CellStyle;
            BookList.Cell[5].Style.Format    = "#,##0.00";
            BookList.Cell[5].Style.Alignment = ContentAlignment.MiddleRight;

            // book list text file
            StreamReader Reader = new StreamReader("BookList.txt");

            // loop for records
            for (;;)
            {
                // read one line
                string TextLine = Reader.ReadLine();
                if (TextLine == null)
                {
                    break;
                }

                // split to fields (must be 8 fields)
                string[] Fld = TextLine.Split(new char[] { '\t' });
                if (Fld.Length != 8)
                {
                    continue;
                }

                // book cover
                PdfImage Cell0Image = new PdfImage(Document);
                Cell0Image.LoadImage(Fld[6]);
                BookList.Cell[0].Value = Cell0Image;

                // note create text box set Value field
                TextBox Box = BookList.Cell[1].CreateTextBox();
                Box.AddText(TitleFont, 10.0, Color.DarkBlue, Fld[0]);
                Box.AddText(NormalFont, 8.0, Color.Black, ", Author(s): ");
                Box.AddText(AuthorFont, 9.0, Color.DarkRed, Fld[2]);

                // date, type in-stock and price
                BookList.Cell[2].Value = Fld[1];
                BookList.Cell[3].Value = Fld[3];
                BookList.Cell[4].Value = int.Parse(Fld[5]);
                BookList.Cell[5].Value = double.Parse(Fld[4], NFI.PeriodDecSep);

                // QRCode and web link
                QREncoder Encoder = new QREncoder();
                Encoder.ErrorCorrection = ErrorCorrection.M;
                Encoder.Encode(Fld[7]);
                PdfImage QRImage = new PdfImage(Document);
                QRImage.LoadImage(Encoder);

                BookList.Cell[6].Value   = QRImage;
                BookList.Cell[6].WebLink = Fld[7];

                // draw it
                BookList.DrawRow();
            }

            // close book list
            BookList.Close();

            // exit
            return;
        }