Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        public static void RenderGrid(C1PdfDocument pdf, C1FlexGrid flex, PdfExportOptions options)
        {
            // get rendering options
            if (options == null)
            {
                options = new PdfExportOptions();
            }

            // get root element to lay out the PDF pages
            Panel root = null;
            for (var parent = flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement)
            {
                if (parent is Panel)
                {
                    root = parent as Panel;
                }
            }

            // get page size
            var rc = pdf.PageRectangle;

            // create panel to hold elements while they render
            var pageTemplate = new PageTemplate();
            pageTemplate.Width = rc.Width;
            pageTemplate.Height = rc.Height;
            pageTemplate.SetPageMargin(options.Margin);
            root.Children.Add(pageTemplate);

            // render grid into PDF document
            var m = options.Margin;
            var sz = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom);
            var pages = flex.GetPageImages(options.ScaleMode, sz, 100);
            for (int i = 0; i < pages.Count; i++)
            {
                // skip a page when necessary
                if (i > 0)
                {
                    pdf.NewPage();
                }

                // set content
                pageTemplate.PageContent.Child = pages[i];
                pageTemplate.PageContent.Stretch = options.ScaleMode == ScaleMode.ActualSize
                    ? System.Windows.Media.Stretch.None
                    : System.Windows.Media.Stretch.Uniform;

                // set header/footer text
                pageTemplate.HeaderLeft.Text = options.DocumentTitle;
                if (options.KnownPageCount)
                {
                    pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}",
                        pdf.CurrentPage + 1, pages.Count);
                }
                else
                {
                    pageTemplate.FooterRight.Text = string.Format("Page {0}",
                        pdf.CurrentPage + 1);
                }

                // measure page element
                pageTemplate.UpdateLayout();
                pageTemplate.Arrange(new Rect(0, 0, rc.Width, rc.Height));

                // add to PDF
                pdf.DrawElement(pageTemplate, rc);
            }

            // done with template
            root.Children.Remove(pageTemplate);
        }
Exemplo n.º 4
0
        // export the grid to a PDF file
        void SavePdf(Stream s)
        {
            // get root element to lay out the PDF pages
            Panel root = null;

            for (var parent = _flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement)
            {
                if (parent is Panel)
                {
                    root = parent as Panel;
                }
            }

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

            // get page size
            var rc        = pdf.PageRectangle;
            var m         = new Thickness(96, 96, 96 / 2, 96 / 2);
            var scaleMode = ScaleMode.ActualSize;

            // create panel to hold elements while they render
            var pageTemplate = new PageTemplate();

            pageTemplate.Width  = rc.Width;
            pageTemplate.Height = rc.Height;
            pageTemplate.SetPageMargin(m);
            root.Children.Add(pageTemplate);

            // render grid into PDF document
            var sz    = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom);
            var pages = _flex.GetPageImages(scaleMode, sz, 100);

            for (int i = 0; i < pages.Count; i++)
            {
                // skip a page when necessary
                if (i > 0)
                {
                    pdf.NewPage();
                }

                // set content
                pageTemplate.PageContent.Child   = pages[i];
                pageTemplate.PageContent.Stretch = System.Windows.Media.Stretch.Uniform;

                // set footer text
                pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}",
                                                              i + 1, pages.Count);

                // measure page element
                pageTemplate.Measure(new Size(rc.Width, rc.Height));
                pageTemplate.UpdateLayout();

                // add page element to PDF
                pdf.DrawElement(pageTemplate, rc);
            }

            // done with template
            root.Children.Remove(pageTemplate);

            // save the PDF document
            pdf.Save(s);
            s.Close();
        }
Exemplo n.º 5
0
        // export the grid to a PDF file
        void SavePdf(Stream s, string documentName)
        {
            #if false
            // get root element to lay out the PDF pages
            Panel root = null;
            for (var parent = _flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement)
            {
                if (parent is Panel)
                {
                    root = parent as Panel;
                }
            }

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

            // get page size
            var rc = pdf.PageRectangle;
            var m = new Thickness(96, 96, 96 / 2, 96 / 2);
            var scaleMode = ScaleMode.ActualSize;

            // create panel to hold elements while they render
            var pageTemplate = new PageTemplate();
            pageTemplate.Width = rc.Width;
            pageTemplate.Height = rc.Height;
            pageTemplate.SetPageMargin(m);
            root.Children.Add(pageTemplate);

            // render grid into PDF document
            var sz = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom);
            var pages = _flex.GetPageImages(scaleMode, sz, 100);
            for (int i = 0; i < pages.Count; i++)
            {
                // skip a page when necessary
                if (i > 0)
                {
                    pdf.NewPage();
                }

                // set content
                pageTemplate.PageContent.Child = pages[i];
                pageTemplate.PageContent.Stretch = System.Windows.Media.Stretch.Uniform;

                // set header/footer text
                pageTemplate.HeaderLeft.Text = documentName;
                pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}",
                    i + 1, pages.Count);

                // measure page element
                pageTemplate.Measure(new Size(rc.Width, rc.Height));
                pageTemplate.UpdateLayout();

                // add page element to PDF
                pdf.DrawElement(pageTemplate, rc);
            }

            // done with template
            root.Children.Remove(pageTemplate);

            // save the PDF document
            pdf.Save(s);
            s.Close();
            #endif
        }
        public static void RenderGrid(C1PdfDocument pdf, C1FlexGrid flex, PdfExportOptions options)
        {
            // get rendering options
            if (options == null)
            {
                options = new PdfExportOptions();
            }

            // get root element to lay out the PDF pages
            Panel root = null;

            for (var parent = flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement)
            {
                if (parent is Panel)
                {
                    root = parent as Panel;
                }
            }

            // get page size
            var rc = pdf.PageRectangle;

            // create panel to hold elements while they render
            var pageTemplate = new PageTemplate();

            pageTemplate.Width  = rc.Width;
            pageTemplate.Height = rc.Height;
            pageTemplate.SetPageMargin(options.Margin);
            root.Children.Add(pageTemplate);

            // render grid into PDF document
            var m     = options.Margin;
            var sz    = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom);
            var pages = flex.GetPageImages(options.ScaleMode, sz, 100);

            for (int i = 0; i < pages.Count; i++)
            {
                // skip a page when necessary
                if (i > 0)
                {
                    pdf.NewPage();
                }

                // set content
                pageTemplate.PageContent.Child   = pages[i];
                pageTemplate.PageContent.Stretch = options.ScaleMode == ScaleMode.ActualSize
                    ? System.Windows.Media.Stretch.None
                    : System.Windows.Media.Stretch.Uniform;

                // set header/footer text
                pageTemplate.HeaderLeft.Text = options.DocumentTitle;
                if (options.KnownPageCount)
                {
                    pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}",
                                                                  pdf.CurrentPage + 1, pages.Count);
                }
                else
                {
                    pageTemplate.FooterRight.Text = string.Format("Page {0}",
                                                                  pdf.CurrentPage + 1);
                }

                // measure page element
                pageTemplate.UpdateLayout();
                pageTemplate.Arrange(new Rect(0, 0, rc.Width, rc.Height));

                // add to PDF
                pdf.DrawElement(pageTemplate, rc);
            }

            // done with template
            root.Children.Remove(pageTemplate);
        }