Example #1
0
        async private void OpenFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            await Task.Run(() =>
            {
                // Create a pdf document with a section and page added.
                PdfDocument doc    = new PdfDocument();
                PdfSection section = doc.Sections.Add();
                PdfPageBase page   = doc.Pages.Add();
                //Load a tiff image from system
                PdfImage image =
                    PdfImage.FromFile(
                        System.Text.RegularExpressions.Regex.Escape(
                            openFileDialog1.FileName).Replace(@"\.", "."));
                //Set image display location and size in PDF
                float widthFitRate  = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
                float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
                float fitRate       = Math.Max(widthFitRate, heightFitRate);
                float fitWidth      = image.PhysicalDimension.Width / fitRate;
                float fitHeight     = image.PhysicalDimension.Height / fitRate;
                page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight);
                doc.SaveToFile("imageToPdf.pdf");
                doc.Close();
            });

            pdfViewer2.LoadFromFile("imageToPdf.pdf");
        }
        public static byte[] Text2Images(Stream inputFile, string filename)
        {
            // TODO Don't write file next time
            using (var fileStream = File.Create(filename))
            {
                inputFile.Seek(0, SeekOrigin.Begin);
                inputFile.CopyTo(fileStream);
            }

            string          text    = File.ReadAllText(filename);
            PdfDocument     doc     = new PdfDocument();
            PdfSection      section = doc.Sections.Add();
            PdfPageBase     page    = section.Pages.Add();
            PdfFont         font    = new PdfFont(PdfFontFamily.Helvetica, 11);
            PdfStringFormat format  = new PdfStringFormat();

            format.LineSpacing = 20f;
            PdfBrush      brush      = PdfBrushes.Black;
            PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);
            float         y          = 0;
            PdfTextLayout textLayout = new PdfTextLayout();

            textLayout.Break  = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);

            textWidget.StringFormat = format;
            textWidget.Draw(page, bounds, textLayout);

            MemoryStream stream = new MemoryStream();

            doc.SaveToStream(stream, Spire.Pdf.FileFormat.PDF);

            return(Pdf2Images(stream));
        }
Example #3
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (textBox2.Text != "")
     {
         PdfDocument     doc     = new PdfDocument();
         PdfSection      section = doc.Sections.Add();
         PdfPageBase     page    = section.Pages.Add();
         PdfFont         font    = new PdfFont(PdfFontFamily.Helvetica, 11);
         PdfStringFormat format  = new PdfStringFormat();
         format.LineSpacing = 20f;
         PdfBrush      brush      = PdfBrushes.Black;
         PdfTextWidget textWidget = new PdfTextWidget(richTextBox1.Text, font, brush);
         float         y          = 0;
         PdfTextLayout textLayout = new PdfTextLayout();
         textLayout.Break  = PdfLayoutBreakType.FitPage;
         textLayout.Layout = PdfLayoutType.Paginate;
         RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
         textWidget.StringFormat = format;
         textWidget.Draw(page, bounds, textLayout);
         doc.SaveToFile("E:\\" + textBox2.Text + ".pdf", FileFormat.PDF);
         MessageBox.Show("Amal bajarildi", "Bajarildi", MessageBoxButtons.OK, MessageBoxIcon.Information);
         textBox2.Text     = null;
         richTextBox1.Text = null;
         pictureBox1.Image = null;
         textBox1.Text     = null;
     }
 }
Example #4
0
        //void pdftest()
        //{
        //     try
        //     {

        //         string Article = Request.QueryString["Article"];
        //         if (Article.Length > 6)
        //         {

        //             string pdfPath = "W:\\test\\Access\\Planos\\" + Article.Substring(0, 6) + "\\" + Article + ".PC.pdf";
        //             //string path = Server.MapPath(pdfPath);
        //             WebClient client = new WebClient();
        //             Byte[] buffer = client.DownloadData(pdfPath);
        //             if (buffer != null)
        //             {
        //                 Response.ContentType = "application/pdf";
        //                 Response.AddHeader("content-length", buffer.Length.ToString());
        //                 Response.BinaryWrite(buffer);
        //             }
        //         }
        //     }
        //     catch (Exception ex)
        //     {
        //         Response.Write(ex.ToString());
        //     }
        // }

        protected void Button3_Click1(object sender, EventArgs e)
        {
            string      UID     = Request.QueryString["UID"];
            PdfDocument doc     = new PdfDocument();
            PdfSection  section = doc.Sections.Add();
            PdfPageBase page    = doc.Pages.Add();

            Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromFile(Server.MapPath("~/Images/UIDimage/New.jpg"));


            float widthFitRate  = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
            float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
            float fitRate       = Math.Max(widthFitRate, heightFitRate);
            float fitWidth      = image.PhysicalDimension.Width / fitRate;
            float fitHeight     = image.PhysicalDimension.Height / fitRate;

            //page.Canvas.DrawImage(image, 0, 0, fitWidth, fitHeight);
            page.Canvas.DrawImage(image, -6, 0, fitWidth + 20, fitHeight + 70);

            doc.SaveToFile(Server.MapPath("~/Images/New.pdf"));
            doc.Close();
            string       strURL   = "~/Images/New.pdf";
            WebClient    req      = new WebClient();
            HttpResponse response = HttpContext.Current.Response;

            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.Buffer = true;
            //response.AddHeader(" Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
            Response.AddHeader("Content-Disposition", "attachment; filename=" + UID + ".pdf");
            byte[] data = req.DownloadData(Server.MapPath(strURL));
            response.BinaryWrite(data);
            response.End();
        }
Example #5
0
        private void SetSectionTemplate(PdfSection section, SizeF pageSize, PdfMargins margin, String label)
        {
            PdfPageTemplateElement leftSpace
                = new PdfPageTemplateElement(margin.Left, pageSize.Height);

            leftSpace.Foreground     = true;
            section.Template.OddLeft = leftSpace;

            PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            float           y      = (pageSize.Height - margin.Top - margin.Bottom) * (1 - 0.618f);
            RectangleF      bounds
                = new RectangleF(10, y, margin.Left - 20, font.Height + 6);

            leftSpace.Graphics.DrawRectangle(PdfBrushes.OrangeRed, bounds);
            leftSpace.Graphics.DrawString(label, font, PdfBrushes.White, bounds, format);

            PdfPageTemplateElement rightSpace
                = new PdfPageTemplateElement(margin.Right, pageSize.Height);

            rightSpace.Foreground      = true;
            section.Template.EvenRight = rightSpace;
            bounds
                = new RectangleF(10, y, margin.Right - 20, font.Height + 6);
            rightSpace.Graphics.DrawRectangle(PdfBrushes.SaddleBrown, bounds);
            rightSpace.Graphics.DrawString(label, font, PdfBrushes.White, bounds, format);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // Create a pdf document with a section and page added.
            PdfDocument pdf     = new PdfDocument();
            PdfSection  section = pdf.Sections.Add();
            PdfPageBase page    = pdf.Pages.Add();

            //Load a tiff image from system
            PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Water.jpg");
            //Set image display location and size in PDF
            //Calculate rate
            float widthFitRate  = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
            float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
            float fitRate       = Math.Max(widthFitRate, heightFitRate);
            //Calculate the size of image
            float fitWidth  = image.PhysicalDimension.Width / fitRate;
            float fitHeight = image.PhysicalDimension.Height / fitRate;

            //Draw image
            page.Canvas.DrawImage(image, 0, 30, fitWidth, fitHeight);

            string output = "ConvertImageToPDF-result.pdf";

            pdf.SaveToFile(output);
            pdf.Close();

            //Launch the Pdf file
            PDFDocumentViewer(output);
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            string      UID     = Request.QueryString["UID"];
            string      Drawing = Request.QueryString["Drawing"];
            PdfDocument doc     = new PdfDocument();
            PdfSection  section = doc.Sections.Add();
            PdfPageBase page    = doc.Pages.Add();

            Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromFile(Server.MapPath("~/Images/UIDimage/New.jpg"));


            float widthFitRate  = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
            float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
            float fitRate       = Math.Max(widthFitRate, heightFitRate);
            float fitWidth      = image.PhysicalDimension.Width / fitRate;
            float fitHeight     = image.PhysicalDimension.Height / fitRate;

            //page.Canvas.DrawImage(image, 0, 0, fitWidth, fitHeight);
            page.Canvas.DrawImage(image, -6, 0, fitWidth + 20, fitHeight + 70);

            doc.SaveToFile(Server.MapPath("~/Images/New.pdf"));
            doc.Close();
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + TextBox1.Text + ".pdf");
            Response.TransmitFile(Server.MapPath("~/Images/New.pdf"));
            Response.End();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            doc.DocumentInformation.Author = "Spire.Pdf";

            //Set the margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            //Create section
            PdfSection section = doc.Sections.Add();

            section.PageSettings.Size    = PdfPageSize.A4;
            section.PageSettings.Margins = margin;

            //Create one page
            PdfPageBase page = section.Pages.Add();

            //Draw automatic fields
            DrawAutomaticField(page);

            //Save the document
            doc.SaveToFile("AutomaticField.pdf");
            doc.Close();

            //Launch the Pdf file
            PDFDocumentViewer("AutomaticField.pdf");
        }
Example #9
0
    private PdfSection method_14(SizeF A_0)
    {
        PdfSection section = this.pdfNewDocument_0.get_Sections().Add();

        section.get_PageSettings().get_Margins().set_All(0f);
        section.get_PageSettings().set_Size(A_0);
        return(section);
    }
Example #10
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Create PdfUnitConvertor to convert the unit
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            //Setting for page margin
            PdfMargins margin = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(2.0f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            //Create PdfSection
            PdfSection section = doc.Sections.Add();

            //Set "A4" for Pdf page
            section.PageSettings.Size = PdfPageSize.A4;

            //Set page margin
            section.PageSettings.Margins = margin;

            //Set rotating angle
            section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;

            //Add the page
            PdfPageBase page = section.Pages.Add();

            //Define a PdfBrush
            PdfBrush brush = PdfBrushes.Black;

            //Define a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f, FontStyle.Bold), true);

            //Set the string format
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            //Set the position for drawing
            float x = 0;
            float y = 50;

            //Text string
            string specification = "The sample demonstrates how to rotate page when creating a PDF document.";

            //Draw text string on page canvas
            page.Canvas.DrawString(specification, font, brush, x, y, format);

            String result = "RotateNewPDF_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Example #11
0
        public bool IntroPdf(PdfSection sec, string name, string surname, string email, string note, string pathOfImage, int numberOfNewLine, bool argument)
        {
            float intro = 10;

            try
            {
                page = sec.Pages.Add();

                float width  = 0;
                float height = 0;
                if (argument)
                {
                    PdfImage image = PdfImage.FromFile(pathOfImage);
                    if (image.Width < 150 || image.Height < 150)
                    {
                        return(false);
                    }
                    else
                    {
                        width = 150; height = 150;
                        float x = (page.Canvas.ClientSize.Width - width) / 2;
                        page.Canvas.DrawImage(image, x, intro, width, height);
                    }
                }
                intro += height;
                page.Canvas.DrawString("Browser Report", font1, brush1, page.Canvas.ClientSize.Width / 2, intro, format1);
                intro += fontInfo.MeasureString("Country List", format1).Height;
                intro += 20;
                page.Canvas.DrawString("Name    : " + name, fontInfo, brush1, 0, intro, leftAlignment);
                intro += 20;
                page.Canvas.DrawString("Surname : " + surname, fontInfo, brush1, 0, intro, leftAlignment);
                intro += 20;
                page.Canvas.DrawString("E-Mail  : " + email, fontInfo, brush1, 0, intro, leftAlignment);
                if (numberOfNewLine <= 1)
                {
                    intro += (20 * numberOfNewLine);
                }
                else
                {
                    intro += (20 * numberOfNewLine) - fontInfo.MeasureString("Country List", format1).Height;
                }

                if (note.Length != 0)
                {
                    page.Canvas.DrawString("Notes   : " + note, fontInfo, brush1, 0, intro, leftAlignment);
                }
                intro += 20;
                page.Canvas.DrawString("Date  : " + DateTime.Now, fontInfo, brush1, 0, intro, leftAlignment);
                return(true);
            }
            catch
            {
                Exception.ThrowExc.Instance.ErrorMessage(Const.Constants.ERROR_WHILE_CREATING);
                return(false);
            }
        }
Example #12
0
    private PdfSection method_13(PageSetup A_0)
    {
        int        num     = 7;
        PdfSection section = this.pdfNewDocument_0.get_Sections().Add();

        section.get_PageSettings().get_Margins().set_All(0f);
        section.get_PageSettings().set_Orientation((((A_0.Orientation == PageOrientation.Portrait) && (A_0.PageSize.Width <= A_0.PageSize.Height)) || (A_0.Orientation.ToString() == BookmarkStart.b("á°¬", num))) ? ((PdfPageOrientation)0) : ((PdfPageOrientation)1));
        section.get_PageSettings().set_Size(A_0.PageSize);
        return(section);
    }
Example #13
0
        public BuildPDF()
        {
            _spirePDF        = new PdfDocument();
            _section         = _spirePDF.Sections.Add();
            _currentPage     = _section.Pages.Add();
            _pageMaxHeight   = _currentPage.Canvas.ClientSize.Height;
            _pageMaxWidth    = _currentPage.Canvas.ClientSize.Width;
            _pagePaddingLeft = 10f;

            _elementCounter = 1;
            _document       = new PDFdocument();
        }
Example #14
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Create one section
            PdfSection section = doc.Sections.Add();

            //Load an image
            PdfImage image       = PdfImage.FromFile(@"..\..\..\..\..\..\Data\ChartImage.png");
            float    imageWidth  = image.PhysicalDimension.Width / 2;
            float    imageHeight = image.PhysicalDimension.Height / 2;

            foreach (PdfBlendMode mode in Enum.GetValues(typeof(PdfBlendMode)))
            {
                PdfPageBase page      = section.Pages.Add();
                float       pageWidth = page.Canvas.ClientSize.Width;
                float       y         = 0;

                //Title
                y = y + 15;
                PdfBrush        brush  = new PdfSolidBrush(Color.OrangeRed);
                PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
                String          text   = String.Format("Transparency Blend Mode: {0}", mode);
                page.Canvas.DrawString(text, font, brush, pageWidth / 2, y, format);
                SizeF size = font.MeasureString(text, format);
                y = y + size.Height + 25;

                page.Canvas.DrawImage(image, 0, y, imageWidth, imageHeight);
                page.Canvas.Save();
                float d = (page.Canvas.ClientSize.Width - imageWidth) / 5;
                float x = d;
                y = y + d / 2 + 40;
                for (int i = 0; i < 5; i++)
                {
                    float alpha = 1.0f / 6 * (5 - i);
                    page.Canvas.SetTransparency(alpha, alpha, mode);
                    page.Canvas.DrawImage(image, x, y, imageWidth, imageHeight);
                    x = x + d;
                    y = y + d / 2 + 40;
                }
                page.Canvas.Restore();
            }

            //Save the document
            doc.SaveToFile("Transparency.pdf");
            doc.Close();

            //Launch the Pdf file
            PDFDocumentViewer("Transparency.pdf");
        }
Example #15
0
        public void CreatePdf(PdfDocument doc, PdfSection sec, DataGridView grid, int columnNumber, string title)
        {
            string[] datasetArray = new string[Calculate(grid, columnNumber) + 1];
            datasetArray           = FillArray(grid, datasetArray, columnNumber);
            sec                    = doc.Sections.Add();
            sec.PageSettings.Width = PdfPageSize.A4.Width;
            bool check = pdfOp.PrintPdf(sec, title, datasetArray);

            if (!check)
            {
                MessageBox.Show("During " + title + " pdf operations got an error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #16
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            doc.ViewerPreferences.PageLayout = PdfPageLayout.TwoColumnLeft;

            //Set the margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            SetDocumentTemplate(doc, PdfPageSize.A4, margin);

            //Create one section
            PdfSection section = doc.Sections.Add();

            section.PageSettings.Size    = PdfPageSize.A4;
            section.PageSettings.Margins = new PdfMargins(0);
            SetSectionTemplate(section, PdfPageSize.A4, margin, "Section 1");

            //Create one page
            PdfNewPage page = section.Pages.Add();

            //Draw page
            DrawPage(page);

            page = section.Pages.Add();
            DrawPage(page);

            page = section.Pages.Add();
            DrawPage(page);

            page = section.Pages.Add();
            DrawPage(page);

            page = section.Pages.Add();
            DrawPage(page);

            //Save the document
            doc.SaveToFile("Template.pdf");
            doc.Close();

            //Launch the Pdf file
            PDFDocumentViewer("Template.pdf");
        }
        /// <summary>
        /// Used to convert the text file into the equivalent
        /// </summary>
        /// <param name="input">The text file in a stream format</param>
        /// <param name="output">The converted PDF file in a stream format</param>
        public void ConvertToPDF(Stream input, Stream output)
        {
            //Read file from stream
            StreamReader reader = new StreamReader(input);
            string       text   = reader.ReadToEnd();

            //Create a default PDF and format
            PdfDocument doc     = new PdfDocument();
            PdfSection  section = doc.Sections.Add();
            PdfPageBase page    = section.Pages.Add();
            PdfFont     font    = new PdfFont(PdfFontFamily.Helvetica, 11);

            //Saves the newly made PDF file to the stream, passing it onto the webjob
            doc.SaveToStream(output, FileFormat.PDF);
            doc.Close();
        }
        public ActionResult TiffToPdf(string Browser)
        {
            //Create a PDF document
            doc = new PdfDocument();

            //Add a section to the PDF document
            PdfSection section = doc.Sections.Add();

            //Declare the PDF page
            PdfPage page;

            //Declare PDF page graphics
            PdfGraphics graphics;

            //Load Multiframe Tiff image
            PdfBitmap tiffImage = new PdfBitmap(ResolveApplicationImagePath("image.tiff"));

            //Get the frame count
            int frameCount = tiffImage.FrameCount;

            //Access each frame draw into the page
            for (int i = 0; i < frameCount; i++)
            {
                page = section.Pages.Add();

                section.PageSettings.Margins.All = 0;

                graphics = page.Graphics;

                tiffImage.ActiveFrame = i;

                graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
            }

            //Stream the output to the browser.
            if (Browser == "Browser")
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }
            else
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
            }
        }
Example #19
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Get text from .txt file
            string text = File.ReadAllText(@"..\..\..\..\..\..\Data\TextToPdf.txt");

            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            PdfSection  section = doc.Sections.Add();
            PdfPageBase page    = section.Pages.Add();

            //Create a PdfFont
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);

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

            format.LineSpacing = 20f;

            PdfBrush brush = PdfBrushes.Black;

            //Set text layout
            PdfTextLayout textLayout = new PdfTextLayout();

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

            RectangleF bounds = new RectangleF(new PointF(10, 20), page.Canvas.ClientSize);

            PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);

            textWidget.StringFormat = format;
            textWidget.Draw(page, bounds, textLayout);

            string output = "TextToPdf.pdf";

            //Save to file
            doc.SaveToFile(output, FileFormat.PDF);

            //Launch the result file
            PDFDocumentViewer(output);
        }
Example #20
0
        public bool PrintPdf(PdfSection sec, string name, string[] dataset)
        {
            float y     = 10;
            bool  check = true;

            if (dataset.Length != 1)
            {
                page = sec.Pages.Add();
                try
                {
                    page.Canvas.DrawString(name, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
                    y += font1.MeasureString("Country List", format1).Height;
                    y += 5;
                    String[][] dataSource = new String[dataset.Length][];
                    for (int i = 0; i < dataset.Length; i++)
                    {
                        dataSource[i] = dataset[i].Split(';');
                    }
                    PdfTable table = new PdfTable();
                    table.Style.CellPadding = 2;
                    table.Style.BorderPen   = new PdfPen(brush1, 0.75f);
                    table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
                    table.Style.HeaderSource             = PdfHeaderSource.Rows;
                    table.Style.HeaderRowCount           = 1;
                    table.Style.ShowHeader = true;
                    table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
                    table.DataSource = dataSource;
                    foreach (PdfColumn column in table.Columns)
                    {
                        column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                    }
                    table.Draw(page, new PointF(0, y));
                }
                catch
                {
                    check = false;
                }
            }
            return(check);
        }
Example #21
0
 private void DrawPageNumber(PdfSection section, PdfMargins margin, int startNumber, int pageCount)
 {
     foreach (PdfPageBase page in section.Pages)
     {
         page.Canvas.SetTransparency(0.5f);
         PdfBrush        brush  = PdfBrushes.Black;
         PdfPen          pen    = new PdfPen(brush, 0.75f);
         PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
         PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
         format.MeasureTrailingSpaces = true;
         float space = font.Height * 0.75f;
         float x     = margin.Left;
         float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
         float y     = page.Canvas.ClientSize.Height - margin.Bottom + space;
         page.Canvas.DrawLine(pen, x, y, x + width, y);
         y = y + 1;
         String numberLabel
             = String.Format("{0} of {1}", startNumber++, pageCount);
         page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
         page.Canvas.SetTransparency(1);
     }
 }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Add a section
            PdfSection section = doc.Sections.Add();

            //Load a image
            PdfImage image = PdfImage.FromFile(@"../../../../../../Data/scenery.jpg");

            //Check whether the width of the image file is greater than default page width or not
            if (image.PhysicalDimension.Width > section.PageSettings.Size.Width)
            {
                //Set the orientation as landscape
                section.PageSettings.Orientation = PdfPageOrientation.Landscape;
            }

            else
            {
                section.PageSettings.Orientation = PdfPageOrientation.Portrait;
            }

            //Add a new page with orientation Landscape
            PdfPageBase page = section.Pages.Add();

            //Draw the image on the page
            page.Canvas.DrawImage(image, new PointF(0, 0));

            string output = "SetPageOrientation-result.pdf";

            //Save to file
            doc.SaveToFile(output);


            //Launch the reuslt file
            PDFDocumentViewer(output);
        }
Example #23
0
        private void button1_Click(object sender, EventArgs e)
        {
            // Create a pdf document with a section and page added.
            PdfDocument pdf     = new PdfDocument();
            PdfSection  section = pdf.Sections.Add();
            PdfPageBase page    = section.Pages.Add();

            // Create a FileStream object to read the imag file
            FileStream fs = File.OpenRead(@"..\..\..\..\..\..\Data\Water.jpg");

            // Read the image into Byte array
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            // Create a MemoryStream object from image Byte array
            MemoryStream ms = new MemoryStream(data);
            // Specify the image source as MemoryStream
            PdfImage image = PdfImage.FromStream(ms);

            //Set image display location and size in PDF
            //Calculate rate
            float widthFitRate  = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
            float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
            float fitRate       = Math.Max(widthFitRate, heightFitRate);
            //Calculate the size of image
            float fitWidth  = image.PhysicalDimension.Width / fitRate;
            float fitHeight = image.PhysicalDimension.Height / fitRate;

            //Draw image
            page.Canvas.DrawImage(image, 0, 30, fitWidth, fitHeight);

            //save and launch the file
            string output = "ConvertImageStreamToPDF.pdf";

            pdf.SaveToFile(output);
            System.Diagnostics.Process.Start(output);
        }
        /// <summary>
        /// Merges PDF's together - Used when documents issued to workshop
        /// </summary>
        /// <param name="outputFileName"></param>
        /// <param name="files"></param>
        void MergePDFs(string outputFileName, List <Document> files)
        {
            using (PdfDocument doc = new PdfDocument())
            {
                foreach (var file in files)
                {
                    //Add a section to PDF document.
                    PdfSection section = doc.Sections.Add();
                    section.PageLabel.Prefix = Path.GetFileNameWithoutExtension(file.FileName);
                    using (PdfLoadedDocument ldoc = new PdfLoadedDocument(file.FileName))
                    {
                        foreach (PdfPage lpage in ldoc.Pages)
                        {
                            //Add pages to the section
                            section.Pages.Add(lpage);
                            MarkPdf(section.Pages[section.Pages.Count - 1], Transmittal.Project.NumberStr, Transmittal.WorkShopJtsk, Transmittal.IssueBy.Initials, file.TotalQty, "");
                        }
                    }
                }

                doc.Save(outputFileName);
                doc.Close(true);
            }
        }
Example #25
0
        private void SetSectionTemplate(PdfSection section, SizeF pageSize, PdfMargins margin, String label)
        {
            PdfPageTemplateElement leftSpace
                = new PdfPageTemplateElement(margin.Left, pageSize.Height);
            leftSpace.Foreground = true;
            section.Template.OddLeft = leftSpace;

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            float y = (pageSize.Height - margin.Top - margin.Bottom) * (1 - 0.618f);
            RectangleF bounds
                = new RectangleF(10, y, margin.Left - 20, font.Height + 6);
            leftSpace.Graphics.DrawRectangle(PdfBrushes.OrangeRed, bounds);
            leftSpace.Graphics.DrawString(label, font, PdfBrushes.White, bounds, format);

            PdfPageTemplateElement rightSpace
                = new PdfPageTemplateElement(margin.Right, pageSize.Height);
            rightSpace.Foreground = true;
            section.Template.EvenRight = rightSpace;
            bounds
                = new RectangleF(10, y, margin.Right - 20, font.Height + 6);
            rightSpace.Graphics.DrawRectangle(PdfBrushes.SaddleBrown, bounds);
            rightSpace.Graphics.DrawString(label, font, PdfBrushes.White, bounds, format);
        }
        public static byte[] Image2Images(Stream inputFile)
        {
            // Create a pdf document with a section and page added.
            PdfDocument doc     = new PdfDocument();
            PdfSection  section = doc.Sections.Add();
            PdfPageBase page    = doc.Pages.Add();
            //Load a tiff image from system
            PdfImage image = PdfImage.FromStream(inputFile);
            //Set image display location and size in PDF
            float widthFitRate  = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
            float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
            float fitRate       = Math.Max(widthFitRate, heightFitRate);
            float fitWidth      = image.PhysicalDimension.Width / fitRate;
            float fitHeight     = image.PhysicalDimension.Height / fitRate;

            page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight);
            //save and launch the file
            MemoryStream stream = new MemoryStream();

            doc.SaveToStream(stream, Spire.Pdf.FileFormat.PDF);
            doc.Close();

            return(Pdf2Images(stream));
        }
Example #27
0
        public static bool PrintPic()
        {
            try
            {
                PdfDocument doc     = new PdfDocument();
                PdfDocument print   = new PdfDocument();
                PdfSection  section = doc.Sections.Add();
                PdfPageBase page    = doc.Pages.Add();

                PdfImage image        = PdfImage.FromFile(path + "\\" + _importProjectName + "\\Pics\\printpic.png");
                float    widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;

                float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;

                float fitRate = Math.Max(widthFitRate, heightFitRate);

                float fitWidth = image.PhysicalDimension.Width / fitRate;

                float fitHeight = image.PhysicalDimension.Height / fitRate;

                page.Canvas.DrawImage(image, 3, 3, fitWidth, fitHeight);
                doc.SaveToFile(path + "\\" + _importProjectName + "\\Pics\\pic.pdf");
                //print.LoadFromFile(path + "\\" + _importProjectName + "\\Pics\\pic.pdf");
                //print.PrintDocument.Print();
                //print.Close();
                doc.LoadFromFile(path + "\\" + _importProjectName + "\\Pics\\pic.pdf");
                doc.PrintDocument.Print();
                doc.Close();

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Example #28
0
        private void DrawContent(PdfSection section, PdfMargins margin)
        {
            section.PageNumber = new PdfPageNumber();
            section.PageNumber.NumberStyle = PdfNumberStyle.Numeric;
            section.PageNumber.Prefix = "page ";
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins.All = 0;
            PdfPageBase page = section.Pages.Add();
            DrawPageHeaderAndFooter(page, margin, false);

            float x = margin.Left;
            float y = margin.Top + 8;
            float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f), true);
            PdfBrush brush1 = PdfBrushes.Black;
            PdfPen pen1 = new PdfPen(brush1, 0.75f);
            page.Canvas.DrawString(Pagination.Properties.Resources.Title, font1, brush1, x, y);
            y = y + font1.MeasureString(Pagination.Properties.Resources.Title).Height + 6;
            page.Canvas.DrawLine(pen1, x, y, page.Canvas.ClientSize.Width - margin.Right, y);
            y = y + 1.75f;

            String content = Pagination.Properties.Resources.Content;
            String[] lines
                = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Italic), true);
            PdfStringFormat format1 = new PdfStringFormat();
            format1.MeasureTrailingSpaces = true;
            format1.LineSpacing = font2.Height * 1.5f;
            format1.ParagraphIndent = font2.MeasureString("\t", format1).Width;
            y = y + font2.Height * 0.5f;
            SizeF size = font2.MeasureString(lines[0], width, format1);
            page.Canvas.DrawString(lines[0], font2, brush1,
                new RectangleF(new PointF(x, y), size), format1);
            y = y + size.Height;

            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f), true);
            PdfStringFormat format2 = new PdfStringFormat();
            format2.LineSpacing = font3.Height * 1.4f;
            format2.MeasureTrailingSpaces = true;
            size = font3.MeasureString(lines[1], width, format2);
            page.Canvas.DrawString(lines[1], font3, brush1,
                new RectangleF(new PointF(x, y), size), format2);
            y = y + size.Height;

            y = y + font3.Height * 0.75f;
            float indent = font3.MeasureString("\t\t", format2).Width;
            float x1 = x + indent;
            size = font3.MeasureString(lines[2], width - indent, format2);
            page.Canvas.DrawString(lines[2], font3, brush1,
                new RectangleF(new PointF(x1, y), size), format2);
            y = y + size.Height + font3.Height * 0.75f;

            StringBuilder buff = new StringBuilder();
            for (int i = 3; i < lines.Length; i++)
            {
                buff.AppendLine(lines[i]);
            }
            content = buff.ToString();

            PdfStringLayouter textLayouter = new PdfStringLayouter();
            PdfStringLayoutResult result
                = textLayouter.Layout(content, font3, format2, new SizeF(width, float.MaxValue));
            foreach (LineInfo line in result.Lines)
            {
                if ((line.LineType & LineType.FirstParagraphLine) == LineType.FirstParagraphLine)
                {
                    y = y + font3.Height * 0.75f;
                }
                if (y > (page.Canvas.ClientSize.Height - margin.Bottom - result.LineHeight))
                {
                    page = section.Pages.Add();
                    DrawPageHeaderAndFooter(page, margin, false);
                    y = margin.Top;
                }
                page.Canvas.DrawString(line.Text, font3, brush1, x, y, format2);
                y = y + result.LineHeight;
            }
        }
Example #29
0
        private void DrawCover(PdfSection section, PdfMargins margin)
        {
            section.PageNumber = new PdfPageNumber();
            section.PageNumber.NumberStyle = PdfNumberStyle.LowerRoman;
            section.PageNumber.Prefix = "cover ";
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins.All = 0;
            PdfPageBase page = section.Pages.Add();
            DrawPageHeaderAndFooter(page, margin, true);

            //refenrence content
            PdfBrush brush1 = PdfBrushes.LightGray;
            PdfBrush brush2 = PdfBrushes.Blue;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f));
            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            String text1 = "(All text and picture from ";
            String text2 = "Wikipedia";
            String text3 = ", the free encyclopedia)";
            float x = 0, y = 10;
            x = x + margin.Left;
            y = y + margin.Top;
            page.Canvas.DrawString(text1, font1, brush1, x, y, format);
            x = x + font1.MeasureString(text1, format).Width;
            page.Canvas.DrawString(text2, font1, brush2, x, y, format);
            x = x + font1.MeasureString(text2, format).Width;
            page.Canvas.DrawString(text3, font1, brush1, x, y, format);

            //cover
            PdfBrush brush3 = PdfBrushes.Black;
            PdfBrush brush4 = new PdfSolidBrush(new PdfRGBColor(0xf9, 0xf9, 0xf9));
            PdfImage image
                = PdfImage.FromFile(@"..\..\..\..\..\..\Data\SciencePersonificationBoston.jpg");
            String text = Pagination.Properties.Resources.ImageDescription;
            float r = image.PhysicalDimension.Height / image.Height;
            PdfPen pen = new PdfPen(brush1, r);
            SizeF size = font1.MeasureString(text, image.PhysicalDimension.Width - 2);
            PdfTemplate template
                = new PdfTemplate(image.PhysicalDimension.Width + 4 * r + 4,
                    image.PhysicalDimension.Height + 4 * r + 7 + size.Height);
            template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height);
            x = y = r + 2;
            template.Graphics.DrawRectangle(brush1, x, y,
                image.PhysicalDimension.Width + 2 * r,
                image.PhysicalDimension.Height + 2 * r);
            x = y = x + r;
            template.Graphics.DrawImage(image, x, y);

            x = x - 1;
            y = y + image.PhysicalDimension.Height + r + 2;
            template.Graphics.DrawString(text, font1, brush3,
                new RectangleF(new PointF(x, y), size));

            float x1 = (page.Canvas.ClientSize.Width - template.Width) / 2;
            float y1 = (page.Canvas.ClientSize.Height - margin.Top - margin.Bottom) * (1 - 0.618f) - template.Height / 2 + margin.Top;
            template.Draw(page.Canvas, x1, y1);

            //title
            format.Alignment = PdfTextAlignment.Center;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 24f, FontStyle.Bold));
            x = page.Canvas.ClientSize.Width / 2;
            y = y1 + template.Height + 10;
            page.Canvas.DrawString(Pagination.Properties.Resources.Title, font2, brush3, x, y, format);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            PdfSection section = doc.Sections.Add();

            section.PageSettings.Margins = margin;
            section.PageSettings.Size    = PdfPageSize.A4;

            // Create one page
            PdfPageBase page = section.Pages.Add();
            float       y    = 10;

            PdfBrush               brush1 = PdfBrushes.Black;
            PdfTrueTypeFont        font1  = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true);
            RectangleF             rctg   = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize);
            PdfLinearGradientBrush brush2
                = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical);

            //draw Codabar
            PdfTextWidget text = new PdfTextWidget();

            text.Font = font1;
            text.Text = "Codabar:";
            PdfLayoutResult result = text.Draw(page, 0, y);

            page = result.Page;
            y    = result.Bounds.Bottom + 2;

            PdfCodabarBarcode barcode1 = new PdfCodabarBarcode("00:12-3456/7890");

            barcode1.BarcodeToTextGapHeight = 1f;
            barcode1.EnableCheckDigit       = true;
            barcode1.ShowCheckDigit         = true;
            barcode1.TextDisplayLocation    = TextLocation.Bottom;
            barcode1.TextColor = Color.Blue;
            barcode1.Draw(page, new PointF(0, y));
            y = barcode1.Bounds.Bottom + 5;


            //draw Code11Barcode
            text.Text = "Code11:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode11Barcode barcode2 = new PdfCode11Barcode("123-4567890");

            barcode2.BarcodeToTextGapHeight = 1f;
            barcode2.TextDisplayLocation    = TextLocation.Bottom;
            barcode2.TextColor = Color.Blue;
            barcode2.Draw(page, new PointF(0, y));
            y = barcode2.Bounds.Bottom + 5;


            //draw Code128-A
            text.Text = "Code128-A:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode128ABarcode barcode3 = new PdfCode128ABarcode("HELLO 00-123");

            barcode3.BarcodeToTextGapHeight = 1f;
            barcode3.TextDisplayLocation    = TextLocation.Bottom;
            barcode3.TextColor = Color.Blue;
            barcode3.Draw(page, new PointF(0, y));
            y = barcode3.Bounds.Bottom + 5;


            //draw Code128-B
            text.Text = "Code128-B:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode128BBarcode barcode4 = new PdfCode128BBarcode("Hello 00-123");

            barcode4.BarcodeToTextGapHeight = 1f;
            barcode4.TextDisplayLocation    = TextLocation.Bottom;
            barcode4.TextColor = Color.Blue;
            barcode4.Draw(page, new PointF(0, y));
            y = barcode4.Bounds.Bottom + 5;


            //draw Code32
            text.Text = "Code32:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode32Barcode barcode5 = new PdfCode32Barcode("16273849");

            barcode5.BarcodeToTextGapHeight = 1f;
            barcode5.TextDisplayLocation    = TextLocation.Bottom;
            barcode5.TextColor = Color.Blue;
            barcode5.Draw(page, new PointF(0, y));
            y = barcode5.Bounds.Bottom + 5;

            page = section.Pages.Add();
            y    = 10;


            //draw Code39
            text.Text = "Code39:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode39Barcode barcode6 = new PdfCode39Barcode("16-273849");

            barcode6.BarcodeToTextGapHeight = 1f;
            barcode6.TextDisplayLocation    = TextLocation.Bottom;
            barcode6.TextColor = Color.Blue;
            barcode6.Draw(page, new PointF(0, y));
            y = barcode6.Bounds.Bottom + 5;


            //draw Code39-E
            text.Text = "Code39-E:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode39ExtendedBarcode barcode7 = new PdfCode39ExtendedBarcode("16-273849");

            barcode7.BarcodeToTextGapHeight = 1f;
            barcode7.TextDisplayLocation    = TextLocation.Bottom;
            barcode7.TextColor = Color.Blue;
            barcode7.Draw(page, new PointF(0, y));
            y = barcode7.Bounds.Bottom + 5;


            //draw Code93
            text.Text = "Code93:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode93Barcode barcode8 = new PdfCode93Barcode("16-273849");

            barcode8.BarcodeToTextGapHeight = 1f;
            barcode8.TextDisplayLocation    = TextLocation.Bottom;
            barcode8.TextColor        = Color.Blue;
            barcode8.QuietZone.Bottom = 5;
            barcode8.Draw(page, new PointF(0, y));
            y = barcode8.Bounds.Bottom + 5;


            //draw Code93-E
            text.Text = "Code93-E:";
            result    = text.Draw(page, 0, y);
            page      = result.Page;
            y         = result.Bounds.Bottom + 2;

            PdfCode93ExtendedBarcode barcode9 = new PdfCode93ExtendedBarcode("16-273849");

            barcode9.BarcodeToTextGapHeight = 1f;
            barcode9.TextDisplayLocation    = TextLocation.Bottom;
            barcode9.TextColor = Color.Blue;
            barcode9.Draw(page, new PointF(0, y));
            y = barcode9.Bounds.Bottom + 5;


            //Save pdf file.
            doc.SaveToFile("Barcode.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("Barcode.pdf");
        }
Example #31
0
        private void printPDF(string text)
        {
            PdfDocument     doc     = new PdfDocument();
            PdfSection      section = doc.Sections.Add();
            PdfPageBase     page    = section.Pages.Add();
            PdfFont         font    = new PdfFont(PdfFontFamily.Helvetica, 11);
            PdfFont         font2   = new PdfFont(PdfFontFamily.Helvetica, 20);
            PdfStringFormat format  = new PdfStringFormat();

            format.LineSpacing = 20f;
            PdfBrush      brush       = PdfBrushes.Black;
            PdfTextWidget textWidget  = new PdfTextWidget(text, font, brush);
            PdfTextWidget textWidget2 = new PdfTextWidget("Historial Medico Familiar.", font2, brush);

            float         y          = 0;
            float         y2         = 35;
            PdfTextLayout textLayout = new PdfTextLayout();

            textLayout.Break  = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            //limites de header
            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
            //limites para la lista
            RectangleF bounds2 = new RectangleF(new PointF(0, y2), page.Canvas.ClientSize);

            textWidget.StringFormat = format;
            textWidget2.Draw(page, bounds, textLayout);
            textWidget.Draw(page, bounds2, textLayout);


            FolderBrowserDialog folderDlg = new FolderBrowserDialog();

            folderDlg.ShowNewFolderButton = true;
            // Show the FolderBrowserDialog.

            /*DialogResult result = folderDlg.ShowDialog();
             * if (result == DialogResult.OK)
             * {
             *  try
             *  {
             *      doc.SaveToFile(folderDlg.SelectedPath + "/ReporteFamiliar.pdf", FileFormat.PDF);
             *      MessageBox.Show("El documento ha sido almacenado en: " + folderDlg.SelectedPath + "/Reporte.pdf");
             *  }
             *  catch
             *  {
             *      MessageBox.Show("No fue posible guardar el PDF, es posible que se encuentre abierto por otra aplicacion");
             *  }
             * }*/

            SaveFileDialog sf = new SaveFileDialog();

            sf.Filter   = "PDF Document (.pdf)|*.pdf";
            sf.Title    = "Guardar reporte en...";
            sf.FileName = "Reporte familiar";
            sf.ShowDialog();
            try
            {
                doc.SaveToFile(sf.FileName, FileFormat.PDF);
                System.Diagnostics.Process.Start(sf.FileName);
            }
            catch
            {
                MessageBox.Show("No fue posible guardar el PDF, es posible que se encuentre abierto por otra aplicacion o que cancelara la creacion del mismo");
            }
        }
Example #32
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            // Create one page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            page.BackgroundColor = Color.Chocolate;

            //Draw page
            DrawPage(page);

            page = doc.Pages.Add(PdfPageSize.A4, margin);
            page.BackgroundColor = Color.Coral;

            //Draw page
            DrawPage(page);

            page = doc.Pages.Add(PdfPageSize.A3, margin, PdfPageRotateAngle.RotateAngle180, PdfPageOrientation.Landscape);
            page.BackgroundColor = Color.LightPink;

            //Draw page
            DrawPage(page);

            //create section
            PdfSection section = doc.Sections.Add();

            section.PageSettings.Size    = PdfPageSize.A4;
            section.PageSettings.Margins = margin;

            page = section.Pages.Add();

            //Draw page
            DrawPage(page);

            //set background color
            page = section.Pages.Add();
            page.BackgroundColor = Color.LightSkyBlue;
            DrawPage(page);

            //Landscape
            section = doc.Sections.Add();
            section.PageSettings.Size        = PdfPageSize.A4;
            section.PageSettings.Margins     = margin;
            section.PageSettings.Orientation = PdfPageOrientation.Landscape;
            page = section.Pages.Add();
            DrawPage(page);

            //Rotate 90
            section = doc.Sections.Add();
            section.PageSettings.Size    = PdfPageSize.A4;
            section.PageSettings.Margins = margin;
            section.PageSettings.Rotate  = PdfPageRotateAngle.RotateAngle90;
            page = section.Pages.Add();
            DrawPage(page);

            //Rotate 180
            section = doc.Sections.Add();
            section.PageSettings.Size    = PdfPageSize.A4;
            section.PageSettings.Margins = margin;
            section.PageSettings.Rotate  = PdfPageRotateAngle.RotateAngle180;
            page = section.Pages.Add();
            DrawPage(page);

            //Save pdf file.
            doc.SaveToFile("PageSetup.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("PageSetup.pdf");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            //create section
            PdfSection section = doc.Sections.Add();

            section.PageSettings.Size    = PdfPageSize.A4;
            section.PageSettings.Margins = margin;

            // Create one page
            PdfPageBase page = section.Pages.Add();

            float y = 10;

            //title
            PdfBrush        brush1  = PdfBrushes.Black;
            PdfTrueTypeFont font1   = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.DrawString("Sales Report", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
            y = y + font1.MeasureString("Sales Report", format1).Height;
            y = y + 5;

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));

            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb";
                conn.Open();

                OleDbCommand partQueryCommand      = PreparePartQueryCommand(conn);
                OleDbCommand orderItemQueryCommand = PrepareOrderItemQueryCommand(conn);

                DataTable vendors = GetVendors(conn);
                for (int i = 0; i < vendors.Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        //next page
                        page = section.Pages.Add();
                        y    = 0;
                    }
                    //draw vendor
                    String          vendorTitle            = String.Format("{0}. {1}", i + 1, vendors.Rows[i].ItemArray[1]);
                    PdfLayoutResult drawVendorLayoutResult = DrawVendor(page, vendors, i, vendorTitle, y);

                    //add vendor bookmark
                    PdfDestination vendorBookmarkDest = new PdfDestination(page, new PointF(0, y));
                    PdfBookmark    vendorBookmark     = doc.Bookmarks.Add(vendorTitle);
                    vendorBookmark.Color        = Color.SaddleBrown;
                    vendorBookmark.DisplayStyle = PdfTextStyle.Bold;
                    vendorBookmark.Action       = new PdfGoToAction(vendorBookmarkDest);

                    y    = drawVendorLayoutResult.Bounds.Bottom + 5;
                    page = drawVendorLayoutResult.Page;

                    //get parts of vendor
                    DataTable parts = GetParts(partQueryCommand, (double)vendors.Rows[i].ItemArray[0]);
                    for (int j = 0; j < parts.Rows.Count; j++)
                    {
                        if (j > 0)
                        {
                            //next page
                            page = section.Pages.Add();
                            y    = 0;
                        }
                        //draw part
                        String          partTitle            = String.Format("{0}.{1}. {2}", i + 1, j + 1, parts.Rows[j].ItemArray[1]);
                        PdfLayoutResult drawPartLayoutResult = DrawPart(page, parts, j, partTitle, y);

                        //add part bookmark
                        PdfDestination partBookmarkDest = new PdfDestination(page, new PointF(0, y));
                        PdfBookmark    partBookmark     = vendorBookmark.Add(partTitle);
                        partBookmark.Color        = Color.Coral;
                        partBookmark.DisplayStyle = PdfTextStyle.Italic;
                        partBookmark.Action       = new PdfGoToAction(partBookmarkDest);

                        y    = drawPartLayoutResult.Bounds.Bottom + 5;
                        page = drawPartLayoutResult.Page;

                        //get order items
                        String    orderItemsTitle = String.Format("{0} - Order Items", parts.Rows[j].ItemArray[1]);
                        DataTable orderItems      = GetOrderItems(orderItemQueryCommand, (double)parts.Rows[j].ItemArray[0]);
                        DrawOrderItems(page, orderItems, orderItemsTitle, y);
                    }
                }
            }

            //Save pdf file.
            doc.SaveToFile("Bookmark.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("Bookmark.pdf");
        }
Example #34
0
 private void DrawPageNumber(PdfSection section, PdfMargins margin, int startNumber, int pageCount)
 {
     foreach (PdfPageBase page in section.Pages)
     {
         page.Canvas.SetTransparency(0.5f);
         PdfBrush brush = PdfBrushes.Black;
         PdfPen pen = new PdfPen(brush, 0.75f);
         PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
         PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
         format.MeasureTrailingSpaces = true;
         float space = font.Height * 0.75f;
         float x = margin.Left;
         float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
         float y = page.Canvas.ClientSize.Height - margin.Bottom + space;
         page.Canvas.DrawLine(pen, x, y, x + width, y);
         y = y + 1;
         String numberLabel
             = String.Format("{0} of {1}", startNumber++, pageCount);
         page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
         page.Canvas.SetTransparency(1);
     }
 }