Example #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // get stream to save to
            var dlg = new SaveFileDialog();

            dlg.DefaultExt = ".pdf";
            var dr = dlg.ShowDialog();

            if (!dr.HasValue || !dr.Value)
            {
                return;
            }

            // get sender button
            var btn = sender as Button;

            // create document
            var pdf = new C1PdfDocument(PaperKind.Letter);

            pdf.Clear();

            // set document info
            var di = pdf.DocumentInfo;

            di.Author  = "ComponentOne";
            di.Subject = "C1.WPF.Pdf demo.";
            di.Title   = "Experimental VisualTree Exporter for PDF";

            // walk visual tree
            CreateDocumentVisualTree(pdf, content);

            // render footers
            // this reopens each page and adds content to them (now we know the page count).
            var font = new Font("Arial", 8, PdfFontStyle.Bold);
            var fmt  = new StringFormat();

            fmt.Alignment     = HorizontalAlignment.Right;
            fmt.LineAlignment = VerticalAlignment.Bottom;
            for (int page = 0; page < pdf.Pages.Count; page++)
            {
                pdf.CurrentPage = page;
                var text = string.Format("C1.WPF.Pdf: {0}, page {1} of {2}",
                                         di.Title,
                                         page + 1,
                                         pdf.Pages.Count);
                pdf.DrawString(
                    text,
                    font,
                    Colors.DarkGray,
                    PdfUtils.Inflate(pdf.PageRectangle, -72, -36),
                    fmt);
            }

            // save document
            using (var stream = dlg.OpenFile())
            {
                pdf.Save(stream);
            }
            MessageBox.Show("Pdf Document saved to " + dlg.SafeFileName);
        }
Example #2
0
        static Rect RenderTableRow(C1PdfDocument pdf, Font font, Font hdrFont, Rect rcPage, Rect rc, string[] fields, DataRow dr)
        {
            // calculate cell width (same for all columns)
            Rect rcCell = rc;

            rcCell.Width  = rc.Width / fields.Length;
            rcCell.Height = 0;

            // calculate cell height (max of all columns)
            rcCell = PdfUtils.Inflate(rcCell, -4, 0);
            foreach (string field in fields)
            {
                string text   = dr[field].ToString();
                var    height = pdf.MeasureString(text, font, rcCell.Width).Height;
                rcCell.Height = Math.Max(rcCell.Height, height);
            }
            rcCell         = PdfUtils.Inflate(rcCell, 4, 0); // add 4 point margin
            rcCell.Height += 2;

            // break page if we have to
            if (rcCell.Bottom > rcPage.Bottom)
            {
                pdf.NewPage();
                rc       = RenderTableHeader(pdf, hdrFont, rcPage, fields);
                rcCell.Y = rc.Y;
            }

            // center vertically just to show how
            StringFormat fmt = new StringFormat();

            fmt.LineAlignment = VerticalAlignment.Center;

            // render data cells
            foreach (string field in fields)
            {
                // get content
                string text = dr[field].ToString();

                // set horizontal alignment
                double d;
                fmt.Alignment = (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out d))
                    ? HorizontalAlignment.Right
                    : HorizontalAlignment.Left;

                // render cell
                pdf.DrawRectangle(Colors.LightGray, rcCell);
                rcCell = PdfUtils.Inflate(rcCell, -4, 0);
                pdf.DrawString(text, font, Colors.Black, rcCell, fmt);
                rcCell = PdfUtils.Inflate(rcCell, 4, 0);
                rcCell = PdfUtils.Offset(rcCell, rcCell.Width, 0);
            }

            // update rectangle and return it
            return(PdfUtils.Offset(rc, 0, rcCell.Height));
        }
Example #3
0
        //---------------------------------------------------------------------------------
        #region ** paper sizes

        static void CreateDocumentPaperSizes(C1PdfDocument pdf)
        {
            // add title
            Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold);
            Rect rc        = PdfUtils.PageRectangle(pdf);

            PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rc, rc, false);

            // create constant font and StringFormat objects
            Font         font = new Font("Tahoma", 18);
            StringFormat sf   = new StringFormat();

            sf.Alignment     = HorizontalAlignment.Center;
            sf.LineAlignment = VerticalAlignment.Center;

            // create one page with each paper size
            bool firstPage = true;

            foreach (var fi in typeof(PaperKind).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                // Silverlight/Phone doesn't have Enum.GetValues
                PaperKind pk = (PaperKind)fi.GetValue(null);

                // skip custom size
                if (pk == PaperKind.Custom)
                {
                    continue;
                }

                // add new page for every page after the first one
                if (!firstPage)
                {
                    pdf.NewPage();
                }
                firstPage = false;

                // set paper kind and orientation
                pdf.PaperKind = pk;
                pdf.Landscape = !pdf.Landscape;

                // draw some content on the page
                rc = PdfUtils.PageRectangle(pdf);
                rc = PdfUtils.Inflate(rc, -6, -6);
                string text = string.Format("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]",
                                            pdf.PaperKind, pdf.Landscape);
                pdf.DrawString(text, font, Colors.Black, rc, sf);
                pdf.DrawRectangle(Colors.Black, rc);
            }
        }
Example #4
0
        //---------------------------------------------------------------------------------
        #region ** text

        static void CreateDocumentText(C1PdfDocument pdf)
        {
            // use landscape for more impact
            pdf.Landscape = true;

            // measure and show some text
            var text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
            var font = new Font("Times New Roman", 9, PdfFontStyle.Italic);

            // create StringFormat used to set text alignment and line spacing
            var fmt = new StringFormat();

            fmt.LineSpacing = -1.5; // 1.5 char height
            fmt.Alignment   = HorizontalAlignment.Center;

            // measure it
            var sz = pdf.MeasureString(text, font, 72 * 3, fmt);
            var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height);

            rc = PdfUtils.Offset(rc, 72, 0);

            // draw a rounded frame
            rc = PdfUtils.Inflate(rc, 10, 10);
            pdf.FillRectangle(Colors.Black, rc, new Size(30, 30));
            pdf.DrawRectangle(new Pen(Colors.Red, 4), rc, new Size(30, 30));
            rc = PdfUtils.Inflate(rc, -10, -10);

            // draw the text
            pdf.DrawString(text, font, Colors.White, rc, fmt);

            // point in center for rotate the text
            rc = pdf.PageRectangle;
            var pt = new Point(rc.Location.X + rc.Width / 2, rc.Location.Y + rc.Height / 2);

            // rotate the string in small increments
            var step = 6;

            text = "PDF works in WPF!";
            for (int i = 0; i <= 360; i += step)
            {
                pdf.RotateAngle = i;
                font            = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold);
                byte b = (byte)(255 * (1 - i / 360.0));
                pdf.DrawString(text, font, Color.FromArgb(0xff, b, b, b), pt);
            }
        }
Example #5
0
        void CreateDocumentVisualTree(C1PdfDocument pdf, FrameworkElement targetElement)
        {
            // set up to render
            var font = new Font("Courier", 14);
            var img  = new WriteableBitmap(CreateBitmap(targetElement));

            // go render
            bool firstPage = true;

            foreach (Stretch stretch in new Stretch[] { Stretch.Fill, Stretch.None, Stretch.Uniform, Stretch.UniformToFill })
            {
                // add page break
                if (!firstPage)
                {
                    pdf.NewPage();
                }
                firstPage = false;

                // set up to render
                var alignment = ContentAlignment.TopLeft;
                var rc        = PdfUtils.Inflate(pdf.PageRectangle, -72, -72);
                rc.Height /= 2;

                // render element as image
                pdf.DrawString("Element as Image, Stretch: " + stretch.ToString(), font, Colors.Black, rc);
                rc = PdfUtils.Inflate(rc, -20, -20);
                pdf.DrawImage(img, rc, alignment, stretch);
                pdf.DrawRectangle(Colors.Green, rc);
                rc = PdfUtils.Inflate(rc, +20, +20);
                pdf.DrawRectangle(Colors.Green, rc);

                // move to bottom of the page
                rc = PdfUtils.Offset(rc, 0, rc.Height + 20);

                // render element
                pdf.DrawString("Element as VisualTree, Stretch: " + stretch.ToString(), font, Colors.Black, rc);
                rc = PdfUtils.Inflate(rc, -20, -20);
                pdf.DrawElement(targetElement, rc, alignment, stretch);
                pdf.DrawRectangle(Colors.Green, rc);
                rc = PdfUtils.Inflate(rc, +20, +20);
                pdf.DrawRectangle(Colors.Green, rc);
            }
        }
Example #6
0
        //---------------------------------------------------------------------------------
        #region ** text flow

        static void CreateDocumentTextFlow(C1PdfDocument pdf)
        {
            // load long string from resource file
            string text = "Resource not found...";

            using (var sr = new StreamReader(DataAccess.GetStream("flow.txt")))
            {
                text = sr.ReadToEnd();
            }
            text = text.Replace("\t", "   ");
            text = string.Format("{0}\r\n\r\n---oOoOoOo---\r\n\r\n{0}", text);

            // create pdf document
            pdf.DocumentInfo.Title = "Text Flow";

            // add title
            Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold);
            Font bodyFont  = new Font("Tahoma", 9);
            Rect rcPage    = PdfUtils.PageRectangle(pdf);
            Rect rc        = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false);

            rc.Y     += titleFont.Size + 6;
            rc.Height = rcPage.Height - rc.Y;

            // create two columns for the text
            Rect rcLeft = rc;

            rcLeft.Width  = rcPage.Width / 2 - 12;
            rcLeft.Height = 300;
            rcLeft.Y      = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2;
            Rect rcRight = rcLeft;

            rcRight.X = rcPage.Right - rcRight.Width;

            // start with left column
            rc = rcLeft;

            // render string spanning columns and pages
            for (; ;)
            {
                // render as much as will fit into the rectangle
                rc = PdfUtils.Inflate(rc, -3, -3);
                int nextChar = pdf.DrawString(text, bodyFont, Colors.Black, rc);
                rc = PdfUtils.Inflate(rc, +3, +3);
                pdf.DrawRectangle(Colors.LightGray, rc);

                // break when done
                if (nextChar >= text.Length)
                {
                    break;
                }

                // get rid of the part that was rendered
                text = text.Substring(nextChar);

                // switch to right-side rectangle
                if (rc.Left == rcLeft.Left)
                {
                    rc = rcRight;
                }
                else // switch to left-side rectangle on the next page
                {
                    pdf.NewPage();
                    rc = rcLeft;
                }
            }
        }
Example #7
0
        public static void HandleButtonClick(object sender, RoutedEventArgs e)
        {
            // get sender button
            var btn = sender as Button;

            // create document
            var pdf = new C1PdfDocument(PaperKind.Letter);

            pdf.Clear();

            // set document info
            var di = pdf.DocumentInfo;

            di.Author  = "ComponentOne";
            di.Subject = "C1.WPF.Pdf demo.";
            di.Title   = (string)btn.Content;

            //// add some security
            //if (false)
            //{
            //    var si = pdf.Security;
            //    si.AllowPrint = false;
            //    si.AllowEditAnnotations = false;
            //    si.AllowEditContent = false;
            //    si.AllowCopyContent = false;
            //    //si.UserPassword = "******";
            //    //si.OwnerPassword = "******";
            //}

            //// set viewer preferences
            //if (false)
            //{
            //    var vp = pdf.ViewerPreferences;
            //    vp.CenterWindow = true;
            //    vp.FitWindow = true;
            //    vp.PageLayout = PageLayout.TwoColumnLeft;
            //    vp.PageMode = PageMode.FullScreen;
            //}

            // create document
            switch (di.Title)
            {
            case "Quotes":
                CreateDocumentQuotes(pdf);
                break;

            case "Tables":
                CreateDocumentTables(pdf);
                break;

            case "Images":
                CreateDocumentImages(pdf);
                break;

            case "Paper Sizes":
                CreateDocumentPaperSizes(pdf);
                break;

            case "Table of Contents":
                CreateDocumentTOC(pdf);
                break;

            case "Text Flow":
                CreateDocumentTextFlow(pdf);
                break;

            case "Text":
                CreateDocumentText(pdf);
                break;

            case "Graphics":
                CreateDocumentGraphics(pdf);
                break;
            }

            // render footers
            // this reopens each page and adds content to them (now we know the page count).
            var font = new Font("Arial", 8, PdfFontStyle.Bold);
            var fmt  = new StringFormat();

            fmt.Alignment     = HorizontalAlignment.Right;
            fmt.LineAlignment = VerticalAlignment.Bottom;
            for (int page = 0; page < pdf.Pages.Count; page++)
            {
                pdf.CurrentPage = page;
                var text = string.Format("C1.WPF.Pdf: {0}, page {1} of {2}",
                                         di.Title,
                                         page + 1,
                                         pdf.Pages.Count);
                pdf.DrawString(
                    text,
                    font,
                    Colors.DarkGray,
                    PdfUtils.Inflate(pdf.PageRectangle, -72, -36),
                    fmt);
            }

            var w = new Preview();

            w.PdfDocument = pdf;
            w.Show();
        }