Пример #1
0
 private static bool SaveImageToPdf(Image imageToSave, Stream stream)
 {
     try {
         using (PdfDocument doc = new PdfDocument()) {
             PdfPage page = new PdfPage();
             doc.Pages.Add(page);
             XGraphics xgr = XGraphics.FromPdfPage(page);
             XImage    img = XImage.FromGdiPlusImage(imageToSave);
             if (img.PointWidth / img.PointHeight >= (double)page.Width / (double)page.Width)
             {
                 xgr.DrawImage(img, 0, 0, page.Width, img.PixelHeight * page.Width / img.PixelWidth);
             }
             else
             {
                 xgr.DrawImage(img, 0, 0, img.PixelWidth * page.Height / img.PixelHeight, page.Height);
             }
             doc.Save(stream, true);
             doc.Close();
         }
         return(true);
     }
     catch (Exception ex) {
         log.Error("Failed to save image to pdf, " + ex.Message);
     }
     return(false);
 }
Пример #2
0
        private void GenerateAttachmentPage(XGraphics gfx, Image image, int index)
        {
            index = index + 1;
            //======================================================================================================================================//
            //Add attachment header
            var font       = new XFont("Calibri", 15.0, XFontStyle.Bold);
            var stringSize = gfx.MeasureString("Attachment " + index.ToString(), font);
            var rect       = new XRect(Margin, Margin, PageWidthLandscape - Margin * 2, font.GetHeight());

            CreateTextFormatter(gfx, XParagraphAlignment.Left).DrawString("Attachment " + index.ToString(), font, TextBrush, rect, XStringFormats.TopLeft);

            //======================================================================================================================================//
            //Add attachment image
            XImage ximg = XImage.FromGdiPlusImage(image);

            //double width, height;
            if (gfx.PageSize.Width > gfx.PageSize.Height)
            {
                //Landscape
                gfx.DrawImage(ximg, PageWidthLandscape / 2 - ximg.PointWidth / 2, PageWidth / 2 - ximg.PointHeight / 2);
            }
            else
            {
                //Portrait
                gfx.DrawImage(ximg, PageWidth / 2 - ximg.PointWidth / 2, PageWidthLandscape / 2 - ximg.PointHeight / 2);
            }
        }
        public void GeneratePdf(string destination, IEnumerable <string> sourceFiles)
        {
            var document = new PdfDocument();

            foreach (string fileName in sourceFiles)
            {
                if (!ValidateName(fileName))
                {
                    continue;
                }

                var page     = document.AddPage();
                var graphics = XGraphics.FromPdfPage(page);

                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    using (Image image = Image.FromStream(fs))
                    {
                        var xImage = XImage.FromGdiPlusImage(image);
                        graphics.DrawImage(xImage, 0, 0, page.Width, page.Height);
                    }
                }
            }

            document.Save(destination);
        }
        private void CreatePDF(Queue <QImage> images)
        {
            if (images.Count == 0)
            {
                return;
            }

            PdfDocument document = new PdfDocument();

            foreach (var img in images)
            {
                if (!img.IsSeparator)
                {
                    PdfPage page = document.AddPage();

                    XGraphics gfx   = XGraphics.FromPdfPage(page);
                    XImage    image = XImage.FromGdiPlusImage(img.Image);

                    double resize = img.Image.Width / 500d;
                    gfx.DrawImage(image, 50, 50, 500, img.Image.Height / resize);
                }
            }

            document.Save(Path.Combine(config.DropFolder, DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss-fff") + ".pdf"));

            // We can dispose images in previous loop but it is safier to do it once document is saved.
            foreach (var img in images)
            {
                this.DoneImage(img);
            }
        }
Пример #5
0
        /// <summary>
        /// Generates a page for a chart containing a picture and a header
        /// </summary>
        /// <param name="gfx"></param>
        /// <param name="chart">Image of chart</param>
        private void GenerateChartPage(XGraphics gfx, Image chart)
        {
            //======================================================================================================================================//
            //Add chart image
            XImage ximg = XImage.FromGdiPlusImage(chart);

            gfx.DrawImage(ximg, PageWidthLandscape / 2 - ximg.PointWidth / 2, PageWidth / 2 - ximg.PointHeight / 2);
        }
Пример #6
0
        static void Main()
        {
            // Create a new PDF document
            PdfDocument document = new PdfDocument();

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Create a font
            XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);

            // Draw the text
            gfx.DrawString("Hello, World!", font, XBrushes.Black,
                           new XRect(0, 0, page.Width, page.Height),
                           XStringFormats.Center);

#if GDI
            // Using GDI-specific routines.
            // Make sure to use "#if GDI" for any usings you add for platform-specific code.
            {
                // Just for demo purposes, we create an image and draw it.
                Image image = new Bitmap(@"..\..\Sample.png");
                // XImage.FromGdiPlusImage is available with the GDI build only.
                XImage gdiImage = XImage.FromGdiPlusImage(image);
                gdiImage.Interpolate = false;
                gfx.DrawImage(gdiImage, 0, 0, 16, 16);
            }
#endif

#if WPF
            // Using WPF-specific routines.
            // Make sure to use "#if GDI" for any usings you add for platform-specific code.
            {
                // Just for demo purposes, we create an image and draw it.
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource   = new Uri(@"..\..\Sample.png", UriKind.Relative);
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.EndInit();
                // XImage.FromBitmapSource is available with the WPF build only.
                XImage gdiImage = XImage.FromBitmapSource(image);
                gdiImage.Interpolate = false;
                gfx.DrawImage(gdiImage, 0, 0, 16, 16);
            }
#endif

            // Save the document...
            const string filename = "HelloWorld.pdf";
            document.Save(filename);

            // ...and start a viewer.
            Process.Start(filename);
        }