Exemplo n.º 1
0
        /// <summary>Add an image</summary>
        /// <param name="image">Image object</param>
        /// <param name="bookmark">Optional - Bookmark</param>
        /// <param name="newpage">Optional - Add the image in a new page</param>
        public void addImage(Image image, string bookmark = null, bool newpage = false)
        {
            var img = (Image)image;

            try {
                using (var bitmap = img.Bitmap) {
                    PdfSharp.Drawing.XSize imgSize = GetImageSize(bitmap);
                    double scale = 1;

                    if (imgSize.Width > _area.Width)
                    {
                        scale           = _area.Width / imgSize.Width;
                        imgSize.Width  *= scale;
                        imgSize.Height *= scale;
                    }

                    if (newpage)
                    {
                        this.addPage();
                    }
                    if (!String.IsNullOrEmpty(bookmark))
                    {
                        this.addBookmark(bookmark, false);
                    }
                    if (this.AvailableHeigth < 20)
                    {
                        this.addPage();
                    }
                    if (imgSize.Height < this.AvailableHeigth)
                    {
                        this.AddImageToPdf(bitmap, imgSize.Width, imgSize.Height);
                    }
                    else
                    {
                        double scaledPointsToPixelsY = bitmap.VerticalResolution / scale / 72; // Points scaled -> Points original -> Pixels
                        double yPosPt       = 0;
                        double leftHeightPt = imgSize.Height;
                        while (true)
                        {
                            if (leftHeightPt < 1)
                            {
                                break;
                            }
                            if (this.AvailableHeigth < 20)
                            {
                                this.addPage();
                            }
                            double insertHeightPt;
                            if (leftHeightPt > this.AvailableHeigth)
                            {
                                insertHeightPt = this.AvailableHeigth;
                            }
                            else
                            {
                                insertHeightPt = leftHeightPt;
                            }

                            System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle {
                                X      = 0,
                                Y      = (int)(yPosPt * scaledPointsToPixelsY),
                                Height = (int)(insertHeightPt * scaledPointsToPixelsY),
                                Width  = bitmap.Width
                            };
                            using (System.Drawing.Bitmap bmpCrop = bitmap.Clone(cropArea, bitmap.PixelFormat))
                                this.AddImageToPdf(bmpCrop, imgSize.Width, insertHeightPt);
                            leftHeightPt = imgSize.Height - yPosPt - insertHeightPt;
                            if (leftHeightPt < 1)
                            {
                                break;
                            }
                            yPosPt += insertHeightPt;
                            if (leftHeightPt > 10)
                            {
                                leftHeightPt += 8;
                                yPosPt       -= 8;
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                throw new ApplicationException("addImage methode failed! " + ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>Add text</summary>
        /// <param name="text">Text to add</param>
        /// <param name="size">Optional - font size</param>
        /// <param name="color">Optional - font color</param>
        /// <param name="bold">Optional - bold</param>
        /// <param name="italic">Optional - italic</param>
        /// <param name="underline">Optional - underline</param>
        /// <param name="center">Optional - center the texte</param>
        /// <param name="font">Optional - Font name. Default is Helvetica</param>
        public void addText(string text,
                            int size       = 10,
                            string color   = "Black",
                            bool bold      = false,
                            bool italic    = false,
                            bool underline = false,
                            bool center    = false,
                            string font    = "Helvetica")
        {
            PdfSharp.Drawing.XFontStyle xfontStyle =
                (bold ? PdfSharp.Drawing.XFontStyle.Bold : 0)
                | (underline ? PdfSharp.Drawing.XFontStyle.Underline : 0)
                | (italic ? PdfSharp.Drawing.XFontStyle.Italic : 0);
            PdfSharp.Drawing.XFont xfont;
            if (bold || underline || italic)
            {
                xfont = new PdfSharp.Drawing.XFont(font, size, xfontStyle);
            }
            else
            {
                xfont = new PdfSharp.Drawing.XFont(font, size);
            }
            PdfSharp.Drawing.XBrush xBrush;
            try {
                xBrush = (PdfSharp.Drawing.XSolidBrush) typeof(PdfSharp.Drawing.XBrushes).GetProperty(color).GetValue(null, null);
            } catch (Exception) {
                throw new ArgumentException("Color <" + color + "> is not available!");
            }

            if (center)
            {
                PdfSharp.Drawing.XSize strSize = _graphics.MeasureString(text, xfont);
                double minWidth             = Math.Min(strSize.Width, _area.Width);
                PdfSharp.Drawing.XRect rect = new PdfSharp.Drawing.XRect {
                    X      = (_area.Width / 2) - (minWidth / 2),
                    Y      = +2,
                    Width  = minWidth,
                    Height = strSize.Height
                };
                this.AddTextToPdf(text, xfont, xBrush, rect);
            }
            else
            {
                string leftText = text;
                while (true)
                {
                    int    lenToAdd;
                    string textToAdd;
                    PdfSharp.Drawing.XSize textSize = _graphics.MeasureString(leftText, xfont, PdfSharp.Drawing.XStringFormats.Default);
                    double textHeight;
                    if (textSize.Width < _area.Width)
                    {
                        textHeight = textSize.Height;
                    }
                    else
                    {
                        textHeight = textSize.Height * 1.2 * (textSize.Width / _area.Width);
                    }

                    if (textHeight < this.AvailableHeigth)
                    {
                        PdfSharp.Drawing.XRect rect = new PdfSharp.Drawing.XRect {
                            X      = 0,
                            Y      = 2,
                            Width  = _area.Width,
                            Height = textHeight
                        };
                        this.AddTextToPdf(leftText, xfont, xBrush, rect);
                        break;
                    }
                    else
                    {
                        if (textSize.Width < _area.Width)
                        {
                            lenToAdd  = leftText.Length;
                            textToAdd = leftText.Substring(0, lenToAdd);
                        }
                        else
                        {
                            lenToAdd = (int)(((float)leftText.Length) * this.AvailableHeigth / textHeight * 1.1);
                            while (true)
                            {
                                lenToAdd = leftText.LastIndexOf(' ', lenToAdd - 1);
                                string str = leftText.Substring(0, lenToAdd);
                                textSize   = _graphics.MeasureString(str, xfont);
                                textHeight = textSize.Height * 1.2 * textSize.Width / _area.Width;
                                if (textHeight < this.AvailableHeigth)
                                {
                                    break;
                                }
                            }
                            textToAdd = leftText.Substring(0, lenToAdd);
                        }
                        if (lenToAdd == 0)
                        {
                            break;
                        }

                        PdfSharp.Drawing.XRect rect = new PdfSharp.Drawing.XRect {
                            X      = 0,
                            Y      = 2,
                            Width  = _area.Width,
                            Height = textHeight
                        };
                        this.AddTextToPdf(textToAdd, xfont, xBrush, rect);
                        this.addPage();
                        if (leftText.Length == 0)
                        {
                            break;
                        }
                        leftText = leftText.Substring(lenToAdd);
                    }
                }
            }
        }