public ActionResult ImageInsertion(string InsideBrowser)
        {
            string jpgImage        = ResolveApplicationImagePath("Autumn Leaves.jpg");
            string tifImage        = ResolveApplicationImagePath("256.tif");
            string bmpImage        = ResolveApplicationImagePath("mask2.bmp");
            string pngImage        = ResolveApplicationImagePath("design.png");
            string emfImage        = ResolveApplicationImagePath("metachart.emf");
            string multiframeImage = ResolveApplicationImagePath("Image.tiff");
            string gifImage        = ResolveApplicationImagePath("Ani.gif");

            PdfDocument doc = new PdfDocument();

            doc.ViewerPreferences.PageMode = PdfPageMode.FullScreen;
            PdfTrueTypeFont font  = new PdfTrueTypeFont(new Font("Tahoma", 22f, FontStyle.Bold), false);
            PdfSolidBrush   brush = new PdfSolidBrush(Color.DarkBlue);

            PdfSection  section = doc.Sections.Add();
            PdfPage     page    = section.Pages.Add();
            PdfGraphics g       = page.Graphics;

            //png image
            PdfImage image = new PdfBitmap(pngImage);

            g.DrawImage(image, 0, 0, image.Width, image.Height);
            page.Graphics.DrawString("Png Image", font, brush, new PointF(10, 0));

            page = section.Pages.Add();
            g    = page.Graphics;

            //Bitmap with Tiff image mask
            image = new PdfBitmap(tifImage);
            (image as PdfBitmap).Mask = new PdfImageMask(new PdfBitmap(bmpImage));
            page.Graphics.DrawString("Image mask", font, brush, new PointF(10, 0));
            g.DrawImage(image, 80, 40);

            //Metafile
            PdfMetafile metafile;

            page     = section.Pages.Add();
            g        = page.Graphics;
            metafile = new PdfMetafile(emfImage);
            page.Graphics.DrawString("Metafile", font, brush, new PointF(10, 0));
            g.DrawImage(metafile, new PointF(0, 50));

            //Image pagination -jpg
            image = new PdfBitmap(jpgImage);

            PdfLayoutFormat format = new PdfLayoutFormat();

            format.Layout = PdfLayoutType.Paginate;

            PointF     location = new PointF(0, 400);
            SizeF      size     = new SizeF(400, -1);
            RectangleF rect     = new RectangleF(location, size);

            page.Graphics.DrawString("Image Pagination", font, brush, new PointF(10, 360));
            image.Draw(page, rect, format);

            //Multiframe Tiff image
            PdfBitmap tiffImage = new PdfBitmap(multiframeImage);

            int frameCount = tiffImage.FrameCount;

            for (int i = 0; i < frameCount; i++)
            {
                page = section.Pages.Add();
                section.PageSettings.Margins.All = 0;
                g = page.Graphics;
                tiffImage.ActiveFrame = i;
                g.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
            }

            page  = section.Pages.Add();
            g     = page.Graphics;
            image = new PdfBitmap(gifImage);
            g.DrawImage(image, 0, 0, page.Graphics.ClientSize.Width, image.Height);
            page.Graphics.DrawString("Gif Image", font, brush, new PointF(10, 0));

            section.Pages[section.Pages.Count - 3].Graphics.DrawString("Multiframe Tiff Image", font, brush, new PointF(10, 10));
            section.PageSettings.Transition.PageDuration = 1;
            section.PageSettings.Transition.Duration     = 1;
            section.PageSettings.Transition.Style        = PdfTransitionStyle.Fly;

            //Stream the output to the browser.
            if (InsideBrowser == "Browser")
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }
            else
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
            }
        }
Beispiel #2
0
        private void CreateDocument(object s)
        {
            if (chkPdfA == "on")
            {
                //Create a PDF document in PDF_A1B standard
                Pdfdoc = new PdfDocument(PdfConformanceLevel.Pdf_A1B);
            }
            else
            {
                //Create a PDF document
                Pdfdoc = new PdfDocument();
            }

            //Set page margins
            Pdfdoc.PageSettings.SetMargins(float.Parse(pagemargin));

            //Set page orientation
            if (rdbtnOrientation == "Portrait")
            {
                Pdfdoc.PageSettings.Orientation = PdfPageOrientation.Portrait;
            }
            else
            {
                Pdfdoc.PageSettings.Orientation = PdfPageOrientation.Landscape;
            }

            //Set rotation
            Pdfdoc.PageSettings.Rotate = (PdfPageRotateAngle)Enum.Parse(typeof(PdfPageRotateAngle), rotate);

            PdfPage          page      = null;
            SizeF            pageSize  = SizeF.Empty;
            PdfUnitConvertor convertor = new PdfUnitConvertor();
            float            width     = -1;
            float            height    = -1;


            page = Pdfdoc.Pages.Add();

            pageSize = page.GetClientSize();

            width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);


            //Adding Header
            if (showHeader == "on")
            {
                this.AddHtmlHeader(Pdfdoc, "Syncfusion Essential PDF", " ");
            }

            //Adding Footer
            if (showFooter == "on")
            {
                this.AddHtmlFooter(Pdfdoc, "@Copyright 2008");
            }

            HtmlConverter html = new HtmlConverter();

            // setting Javascript
            html.EnableJavaScript = chkEnableJavaScript == "on" ? true : false;
            //// Setting Pagebreak
            html.AutoDetectPageBreak = activatePageBreak == "on" ? true : false;
            //// set hyperlink
            html.EnableHyperlinks = chkEnableHyperlink == "on" ? true : false;

            if (convertType == "Metafile")
            {
                HtmlToPdfResult result = html.Convert(sourceUrl, Syncfusion.HtmlConverter.ImageType.Metafile, (int)width, (int)height, AspectRatio.KeepWidth);

                if (result != null)
                {
                    PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
                    mf.Quality = 100;

                    PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
                    format.Break  = PdfLayoutBreakType.FitPage;
                    format.Layout = PdfLayoutType.Paginate;
                    Pdfdoc.PageSettings.Height = result.RenderedImage.Size.Height;
                    format.SplitTextLines      = chkSplitText == "on" ? true : false;
                    format.SplitImages         = splitImage == "on" ? true : false;

                    result.Render(page, format);
                }
                else
                {
                    Response.Write("Warning ! Please check the HTML link");
                }
            }
            else if (convertType == "Bitmap")
            {
                using (System.Drawing.Image img = html.ConvertToImage(sourceUrl, Syncfusion.HtmlConverter.ImageType.Bitmap, (int)width, -1, AspectRatio.KeepWidth))
                {
                    if (img != null)
                    {
                        PdfImage        image  = new PdfBitmap(img);
                        PdfLayoutFormat format = new PdfLayoutFormat();
                        format.Break  = PdfLayoutBreakType.FitPage;
                        format.Layout = PdfLayoutType.Paginate;
                        if (img.Size.Width > pageSize.Width)
                        {
                            //Bitmap
                            image.Draw(page, new RectangleF(0, 0, pageSize.Width, pageSize.Height), format);
                        }
                        else
                        {
                            //Bitmap
                            image.Draw(page, new RectangleF(0, 0, img.Width, img.Height), format);
                        }
                    }
                    else
                    {
                        Response.Write("Warning ! Please check the HTML link");
                    }
                }
            }
        }