Exemplo n.º 1
0
        private void EndPageLayout2(object sender, EndPageLayoutEventArgs e)
        {
            EndTextPageLayoutEventArgs args = (EndTextPageLayoutEventArgs)e;
            PdfTextLayoutResult        tlr  = args.Result;
            RectangleF bounds = tlr.Bounds;

            args.NextPage = tlr.Page;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates PDF document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Create a new PDF document.
            PdfDocument doc = new PdfDocument();

            //Set compression level
            doc.Compression = PdfCompressionLevel.None;

            //Add a page to the document.
            PdfPage page = doc.Pages.Add();

            //Read the text from the text file
            string path = ResolveApplicationDataPath("Manual.txt");
            StreamReader reader = new StreamReader(path, Encoding.ASCII);
            string text = reader.ReadToEnd();
            reader.Close();

            //Set the font
            Font f = new Font("Arial", 12);
            PdfTrueTypeFont font = new PdfTrueTypeFont(f, false);

            //Set the formats for the text
            PdfStringFormat format = new PdfStringFormat();
            format.Alignment = PdfTextAlignment.Justify;
            format.LineAlignment = PdfVerticalAlignment.Top;
            format.ParagraphIndent = 15f;

            //Create a text element
            PdfTextElement element = new PdfTextElement(text, font);
            element.Brush = new PdfSolidBrush(Color.Black);
            element.StringFormat = format;
            element.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

            //Set the properties to paginate the text.
            PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
            layoutFormat.Break = PdfLayoutBreakType.FitPage;
            layoutFormat.Layout = PdfLayoutType.Paginate;

            RectangleF bounds = new RectangleF(PointF.Empty, page.Graphics.ClientSize);

            //Raise the events to draw the graphic elements for each page.
            element.BeginPageLayout += new BeginPageLayoutEventHandler(BeginPageLayout);
            element.EndPageLayout += new EndPageLayoutEventHandler(EndPageLayout);

            //Draw the text element with the properties and formats set.
            PdfTextLayoutResult result = element.Draw(page, bounds, layoutFormat);

            //Stream the output to the browser.
            if (this.CheckBox1.Checked)
            {
                doc.Save("Sample.pdf", Response, HttpReadType.Open);
            }
            else
            {
                doc.Save("Sample.pdf", Response, HttpReadType.Save);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a simple PDF document
        /// </summary>
        /// <returns>Return the created PDF document as stream</returns>
        public MemoryStream CreatePdfDocument()
        {
            //Create a new PDF document.
            PdfDocument doc = new PdfDocument();

            //Set compression level
            doc.Compression = PdfCompressionLevel.None;

            //Add a page to the document.
            PdfPage page = doc.Pages.Add();

            string basePath = _hostingEnvironment.WebRootPath;
            //Read the file
            FileStream file = new FileStream(ResolveApplicationPath("manual.txt"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            //Read the text from the text file
            StreamReader reader = new StreamReader(file, System.Text.Encoding.ASCII);
            string       text   = reader.ReadToEnd();

            reader.Dispose();

            //Set the font
            PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);

            //Set the formats for the text
            PdfStringFormat format = new PdfStringFormat();

            format.Alignment       = PdfTextAlignment.Justify;
            format.LineAlignment   = PdfVerticalAlignment.Top;
            format.ParagraphIndent = 15f;

            //Create a text element
            PdfTextElement element = new PdfTextElement(text, font);

            element.Brush        = new PdfSolidBrush(Color.Black);
            element.StringFormat = format;
            element.Font         = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

            //Set the properties to paginate the text.
            PdfLayoutFormat layoutFormat = new PdfLayoutFormat();

            layoutFormat.Break  = PdfLayoutBreakType.FitPage;
            layoutFormat.Layout = PdfLayoutType.Paginate;

            RectangleF bounds = new RectangleF(PointF.Empty, page.Graphics.ClientSize);

            //Raise the events to draw the graphic elements for each page.
            element.BeginPageLayout += new BeginPageLayoutEventHandler(BeginPageLayout);
            element.EndPageLayout   += new EndPageLayoutEventHandler(EndPageLayout);

            //Draw the text element with the properties and formats set.
            PdfTextLayoutResult result = element.Draw(page, bounds, layoutFormat);

            //Save the PDF to the MemoryStream
            MemoryStream ms = new MemoryStream();

            doc.Save(ms);

            //If the position is not set to '0' then the PDF will be empty.
            ms.Position = 0;

            //Close the PDF document.
            doc.Close(true);
            return(ms);
        }
Exemplo n.º 4
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            //Create a PDF document
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPage    page = doc.Pages.Add();
            RectangleF rect = new RectangleF(0, 0, page.GetClientSize().Width, 50);

            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            PdfPen        pen   = new PdfPen(Color.Black, 1f);

            //Create font
            PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 11.5f);

            Font            ttf     = new Font("Calibri", 14f, FontStyle.Bold);
            PdfTrueTypeFont heading = new PdfTrueTypeFont(ttf, true);

            //Adding Header
            this.AddHeader(doc, "Syncfusion Essential PDF", "Header and Footer Demo");

            //Adding Footer
            this.AddFooter(doc, "@Copyright 2015");

            //Set formats
            PdfStringFormat format = new PdfStringFormat();

            format.Alignment = PdfTextAlignment.Justify;
            string       path   = @"..\..\..\..\..\..\..\Common\Data\PDF\Essential studio.txt";
            StreamReader reader = new StreamReader(path, Encoding.ASCII);
            string       text   = reader.ReadToEnd();

            reader.Close();
            RectangleF column = new RectangleF(0, 20, page.Graphics.ClientSize.Width / 2f - 10f, page.Graphics.ClientSize.Height);

            bounds         = column;
            m_columnBounds = column;

            //Create text element to control the text flow
            PdfTextElement element = new PdfTextElement(text, font);

            element.Brush        = new PdfSolidBrush(Color.Black);
            element.StringFormat = format;
            PdfLayoutFormat layoutFormat = new PdfLayoutFormat();

            layoutFormat.Break  = PdfLayoutBreakType.FitPage;
            layoutFormat.Layout = PdfLayoutType.Paginate;

            //Get the remaining text that flows beyond the boundary.
            PdfTextLayoutResult result = element.Draw(page, bounds, layoutFormat);

            path   = @"..\..\..\..\..\..\..\Common\Data\Essential PDF.txt";
            reader = new StreamReader(path, Encoding.ASCII);
            text   = reader.ReadToEnd();
            reader.Close();

            element              = new PdfTextElement(text, font);
            element.Brush        = new PdfSolidBrush(Color.Black);
            element.StringFormat = format;

            // Next paragraph flow from last line of the previous paragraph.
            bounds.Y = result.LastLineBounds.Y + 35f;

            //Raise the event when the text flows to next page.
            element.BeginPageLayout += new BeginPageLayoutEventHandler(BeginPageLayout2);

            //Raise the event when the text reaches the end of the page.
            element.EndPageLayout += new EndPageLayoutEventHandler(EndPageLayout2);
            result.Page.Graphics.DrawString("Essential PDF", heading, PdfBrushes.DarkBlue, new PointF(bounds.X, bounds.Y));

            bounds.Y = result.LastLineBounds.Y + 60f;
            result   = element.Draw(result.Page, new RectangleF(bounds.X, bounds.Y, bounds.Width, -1), layoutFormat);
            PdfImage image = new PdfBitmap(@"..\..\..\..\..\..\..\Common\Images\PDF\Essen PDF.gif");

            bounds.Y = result.LastLineBounds.Y + 20f;
            bounds.X = bounds.Width + 20f;

            result.Page.Graphics.DrawImage(image, new PointF(bounds.X, bounds.Y));
            result.Page.Graphics.DrawString("Essential DocIO", heading, PdfBrushes.DarkBlue, new PointF(bounds.X, bounds.Y + image.Height - 20));
            path   = @"..\..\..\..\..\..\..\Common\Data\Essential DocIO.txt";
            reader = new StreamReader(path, Encoding.ASCII);
            text   = reader.ReadToEnd();
            reader.Close();

            element              = new PdfTextElement(text, font);
            element.Brush        = new PdfSolidBrush(Color.Black);
            element.StringFormat = format;

            bounds.Y = result.LastLineBounds.Y + image.Height + 20;
            bounds.X = bounds.Width + 20f;


            result = element.Draw(result.Page, new RectangleF(bounds.X, bounds.Y, bounds.Width, -1), layoutFormat);

            image    = new PdfBitmap(@"..\..\..\..\..\..\..\Common\Images\PDF\Essen DocIO.gif");
            bounds.Y = result.LastLineBounds.Y + 20f;
            bounds.X = bounds.Width + 20f;

            result.Page.Graphics.DrawImage(image, new PointF(bounds.X, bounds.Y));

            path   = @"..\..\..\..\..\..\..\Common\Data\Essential XlsIO.txt";
            reader = new StreamReader(path, Encoding.ASCII);
            text   = reader.ReadToEnd();
            reader.Close();

            element              = new PdfTextElement(text, font);
            element.Brush        = new PdfSolidBrush(Color.Black);
            element.StringFormat = format;

            result.Page.Graphics.DrawString("Essential XlsIO", heading, PdfBrushes.DarkBlue, new PointF(bounds.X, bounds.Y + image.Height - 20));

            bounds.Y = result.LastLineBounds.Y + image.Height + 20;
            bounds.X = bounds.Width + 20f;
            result   = element.Draw(result.Page, new RectangleF(bounds.X, bounds.Y, bounds.Width, -1), layoutFormat);

            image    = new PdfBitmap(@"..\..\..\..\..\..\..\Common\Images\PDF\Essen XlsIO.gif");
            bounds.Y = result.LastLineBounds.Y + 20f;
            bounds.X = bounds.Width + 20f;

            result.Page.Graphics.DrawImage(image, new PointF(bounds.X, bounds.Y));
            doc.Save("Sample.pdf");

            //Message box confirmation to view the created PDF document.
            if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                == DialogResult.Yes)
            {
                //Launching the PDF file using the default Application.[Acrobat Reader]
                System.Diagnostics.Process.Start("Sample.pdf");
                this.Close();
            }
            else
            {
                // Exit
                this.Close();
            }
        }
        private async void GeneratePDF_Click(object sender, RoutedEventArgs e)
        {
            //Create a PDF document
            PdfDocument doc        = new PdfDocument();
            PdfColor    BlackColor = new PdfColor(System.Drawing.Color.FromArgb(255, 0, 0, 0));

            //Add a page
            PdfPage    page = doc.Pages.Add();
            RectangleF rect = new RectangleF(0, 0, page.GetClientSize().Width, 50);

            PdfSolidBrush brush = new PdfSolidBrush(BlackColor);
            PdfPen        pen   = new PdfPen(BlackColor, 1f);

            //Create font
            PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 11.5f);


            PdfStandardFont heading = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);

            //Adding Header
            this.AddHeader(doc, "Syncfusion Essential PDF", "Header and Footer Demo");

            //Adding Footer
            this.AddFooter(doc, "@Copyright 2015");

            //Set formats
            PdfStringFormat format = new PdfStringFormat();

            format.Alignment = PdfTextAlignment.Justify;
            Stream       txtStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essential studio.txt");
            StreamReader reader    = new StreamReader(txtStream, System.Text.Encoding.ASCII);
            string       text      = reader.ReadToEnd();

            reader.Dispose();
            RectangleF column = new RectangleF(0, 20, page.Graphics.ClientSize.Width / 2f - 10f, page.Graphics.ClientSize.Height);

            bounds         = column;
            m_columnBounds = column;

            //Create text element to control the text flow
            PdfTextElement element = new PdfTextElement(text, font);

            element.Brush        = new PdfSolidBrush(BlackColor);
            element.StringFormat = format;
            PdfLayoutFormat layoutFormat = new PdfLayoutFormat();

            layoutFormat.Break  = PdfLayoutBreakType.FitPage;
            layoutFormat.Layout = PdfLayoutType.Paginate;

            //Get the remaining text that flows beyond the boundary.
            PdfTextLayoutResult result = element.Draw(page, bounds, layoutFormat);

            txtStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essential PDF.txt");
            reader    = new StreamReader(txtStream, System.Text.Encoding.ASCII);
            text      = reader.ReadToEnd();
            reader.Dispose();

            element              = new PdfTextElement(text, font);
            element.Brush        = new PdfSolidBrush(BlackColor);
            element.StringFormat = format;

            // Next paragraph flow from last line of the previous paragraph.
            bounds.Y = result.LastLineBounds.Y + 35f;

            //Raise the event when the text flows to next page.
            element.BeginPageLayout += new BeginPageLayoutEventHandler(BeginPageLayout2);

            //Raise the event when the text reaches the end of the page.
            element.EndPageLayout += new EndPageLayoutEventHandler(EndPageLayout2);
            result.Page.Graphics.DrawString("Essential PDF", heading, PdfBrushes.DarkBlue, new PointF(bounds.X, bounds.Y));

            bounds.Y = result.LastLineBounds.Y + 60f;
            result   = element.Draw(result.Page, new RectangleF(bounds.X, bounds.Y, bounds.Width, -1), layoutFormat);
            Stream   imgStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essen PDF.gif");
            PdfImage image     = new PdfBitmap(imgStream);

            bounds.Y = result.LastLineBounds.Y + 20f;
            bounds.X = bounds.Width + 20f;

            result.Page.Graphics.DrawImage(image, new PointF(bounds.X, bounds.Y), new SizeF(image.Width, image.Height - 20));
            PointF a = new PointF(bounds.X, bounds.Y + image.Height - 20);

            result.Page.Graphics.DrawString("Essential DocIO", heading, PdfBrushes.DarkBlue, new PointF(bounds.X, bounds.Y + image.Height - 20));
            txtStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essential DocIO.txt");
            reader    = new StreamReader(txtStream, Encoding.ASCII);
            text      = reader.ReadToEnd();
            reader.Dispose();

            element              = new PdfTextElement(text, font);
            element.Brush        = new PdfSolidBrush(BlackColor);
            element.StringFormat = format;

            bounds.Y = result.LastLineBounds.Y + image.Height + 20;
            bounds.X = bounds.Width + 20f;


            result    = element.Draw(result.Page, new RectangleF(bounds.X, bounds.Y, bounds.Width, -1), layoutFormat);
            imgStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essen DocIO.gif");
            image     = new PdfBitmap(imgStream);
            bounds.Y  = result.LastLineBounds.Y + 20f;
            bounds.X  = bounds.Width + 20f;

            result.Page.Graphics.DrawImage(image, new PointF(bounds.X, bounds.Y), new SizeF(image.Width, image.Height - 20));
            txtStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essential XlsIO.txt");

            reader = new StreamReader(txtStream, Encoding.ASCII);
            text   = reader.ReadToEnd();
            reader.Dispose();

            element              = new PdfTextElement(text, font);
            element.Brush        = new PdfSolidBrush(BlackColor);
            element.StringFormat = format;

            result.Page.Graphics.DrawString("Essential XlsIO", heading, PdfBrushes.DarkBlue, new PointF(bounds.X, bounds.Y + image.Height - 20));

            bounds.Y  = result.LastLineBounds.Y + image.Height + 20;
            bounds.X  = bounds.Width + 20f;
            result    = element.Draw(result.Page, new RectangleF(bounds.X, bounds.Y, bounds.Width, -1), layoutFormat);
            imgStream = typeof(HeadersAndFooters).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.Essen XlsIO.gif");
            image     = new PdfBitmap(imgStream);
            bounds.Y  = result.LastLineBounds.Y + 20f;
            bounds.X  = bounds.Width + 20f;

            result.Page.Graphics.DrawImage(image, new PointF(bounds.X, bounds.Y), new SizeF(image.Width, image.Height - 20));
            MemoryStream stream = new MemoryStream();
            await doc.SaveAsync(stream);

            doc.Close(true);
            Save(stream, "HeadersAndFooters.pdf");
        }