public static FixedDocument FlowToFixed(FlowDocument flowDoc)
        {
            DocumentPaginator paginator = (flowDoc as IDocumentPaginatorSource).DocumentPaginator;

            paginator.PageSize = new Size(Page.ExtentWidth, Page.ExtentHeight);     // A3
            if (!paginator.IsPageCountValid)
            {
                paginator.ComputePageCount();
            }

            FixedDocument fixedDoc = new FixedDocument();

            for (int i = 0; i < paginator.PageCount; i++)
            {
                Canvas pageCanvas = new Canvas();
                pageCanvas.Margin = new Thickness(Page.Bleed);
                DocumentPageView dpv = new DocumentPageView {
                    DocumentPaginator = paginator, PageNumber = i
                };
                pageCanvas.Children.Add(dpv);

                FixedPage fp = new FixedPage {
                    Width = Page.MediaWidth, Height = Page.MediaHeight
                };
                fp.Children.Add(pageCanvas);
                PageContent pc = new PageContent();
                (pc as IAddChild).AddChild(fp);
                fixedDoc.Pages.Add(pc);
            }
            return(fixedDoc);
        }
示例#2
0
 public PrintingPaginator(DocumentPaginator paginator, Size pageSize, Size margin)
 {
     _originalPaginator          = paginator;
     _pageSize                   = pageSize;
     _pageMargin                 = margin;
     _originalPaginator.PageSize = new Size(_pageSize.Width - _pageMargin.Width * 2, _pageSize.Height - _pageMargin.Height * 2);
     _originalPaginator.ComputePageCount();
 }
示例#3
0
 public PrintingPaginator(DocumentPaginator paramPaginator, Size paramSize, Size paramMargin)
 {
     originalPaginator          = paramPaginator;
     pageSize                   = paramSize;
     pageMargin                 = paramMargin;
     originalPaginator.PageSize = new Size(pageSize.Width - pageMargin.Width * 2, pageSize.Height - pageMargin.Height * 2);
     originalPaginator.ComputePageCount();
 }
        public void ComputeValues()
        {
            _paginator.ComputePageCount();

            _paginator.PageSize = new Size(ContentPageSize.Width - ContentMargin.Width * 2,
                                           ContentPageSize.Height - ContentMargin.Height * 4);
            var text = new FormattedText("X",
                                         CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                                         FooterTypeface, FooterFontSize, ForegroundBrush);

            _footerHeight = text.Height;
        }
 public PageLayoutPaginator(DocumentPaginator paginator, Size pageSize, Thickness margin)
 {
     _pageSize           = pageSize;
     _margin             = margin;
     _paginator          = paginator;
     _pageBox            = new Rect(0, 0, pageSize.Width, pageSize.Height);
     _contentBox         = new Rect(margin.Left, margin.Top, pageSize.Width - margin.Left - margin.Right, PageSize.Height - margin.Top - margin.Bottom);
     _headerBox          = new Rect(margin.Left, 0.0, pageSize.Width - margin.Left - margin.Right, margin.Top);
     _footerBox          = new Rect(margin.Left, pageSize.Height - margin.Bottom, pageSize.Width - margin.Left - margin.Right, margin.Bottom);
     _paginator.PageSize = new Size(_pageSize.Width - margin.Left - margin.Right,
                                    _pageSize.Height - margin.Top - margin.Bottom);
     _paginator.ComputePageCount();
 }
        private void OnPrint(object sender, ExecutedRoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();

            printDialog.PageRangeSelection   = PageRangeSelection.AllPages;
            printDialog.UserPageRangeEnabled = true;
            bool?dialogResult = printDialog.ShowDialog();

            if (dialogResult != null && dialogResult.Value == false)
            {
                return;
            }

            FlowDocument printSource = this.CreateFlowDocumentForEditor();

            // Save all the existing settings.
            double    pageHeight  = printSource.PageHeight;
            double    pageWidth   = printSource.PageWidth;
            Thickness pagePadding = printSource.PagePadding;
            double    columnGap   = printSource.ColumnGap;
            double    columnWidth = printSource.ColumnWidth;

            // Make the FlowDocument page match the printed page.
            printSource.PageHeight  = printDialog.PrintableAreaHeight;
            printSource.PageWidth   = printDialog.PrintableAreaWidth;
            printSource.PagePadding = new Thickness(20);
            printSource.ColumnGap   = Double.NaN;
            printSource.ColumnWidth = printDialog.PrintableAreaWidth;

            Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);

            DocumentPaginator paginator = ((IDocumentPaginatorSource)printSource).DocumentPaginator;

            paginator.PageSize = pageSize;
            paginator.ComputePageCount();

            printDialog.PrintDocument(paginator, "Svg Contents");

            // Reapply the old settings.
            printSource.PageHeight  = pageHeight;
            printSource.PageWidth   = pageWidth;
            printSource.PagePadding = pagePadding;
            printSource.ColumnGap   = columnGap;
            printSource.ColumnWidth = columnWidth;
        }
示例#7
0
        /// <summary>
        /// Creates the fixed document.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <returns>The document.</returns>
        private FixedDocument CreateFixedDocument(Size size)
        {
            IDocumentPaginatorSource dps = this.doc;
            DocumentPaginator        sourceFlowDocPaginator = dps.DocumentPaginator;

            sourceFlowDocPaginator.PageSize = new Size(
                size.Width - this.Style.Margins.Left - this.Style.Margins.Right,
                size.Height - this.Style.Margins.Top - this.Style.Margins.Bottom);
            if (!sourceFlowDocPaginator.IsPageCountValid)
            {
                sourceFlowDocPaginator.ComputePageCount();
            }

            var margins = new Thickness(
                this.Style.Margins.Left, this.Style.Margins.Top, this.Style.Margins.Width, this.Style.Margins.Height);

            return(this.BuildFixedDocument(sourceFlowDocPaginator, size, margins));
        }
示例#8
0
        public static int SaveAsXps(FlowDocument doc, string fileName, Size printableArea)
        {
            doc.ColumnWidth = printableArea.Width;
            using (Package container = Package.Open(fileName, FileMode.Create))
            {
                using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
                {
                    XpsSerializationManager rsm       = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
                    DocumentPaginator       paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator;
                    paginator.ComputePageCount();

                    DocumentPaginator newPaginator = new DocumentPaginatorWrapper(
                        paginator,
                        printableArea, new Size(8, 8));

                    rsm.SaveAsXaml(paginator);
                }
            }

            return(0);
        }
示例#9
0
        public static FixedDocument ConvertFrom(FlowDocument doc, Size pageSize, Thickness pageMargin)
        {
            FixedDocument fixedDoc = new FixedDocument();

            fixedDoc.DocumentPaginator.PageSize = InchesToDpi(pageSize);

            pageSize   = InchesToDpi(GetCoreSize(pageSize, pageMargin));
            pageMargin = InchesToDpi(pageMargin);

            DocumentPaginator paginator = (doc as IDocumentPaginatorSource).DocumentPaginator;

            paginator.PageSize = pageSize;

            if (!paginator.IsPageCountValid)
            {
                paginator.ComputePageCount();
            }

            int pages = paginator.PageCount;

            for (int i = 0; i < pages; i++)
            {
                DocumentPage page = paginator.GetPage(i);

                FixedPage fp = new FixedPage();
                fp.Children.Add(new Border()
                {
                    Width      = pageSize.Width,
                    Height     = pageSize.Height,
                    Margin     = pageMargin,
                    Background = new VisualBrush(page.Visual)
                });
                fixedDoc.Pages.Add(new PageContent()
                {
                    Child = fp
                });
            }

            return(fixedDoc);
        }
示例#10
0
        private void PrintButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //forPrint.PageHeight = InchToPoint(paperSizeA4.Height); // paperSizeA4.Height: integer value
                //forPrint.PageWidth = forPrint.ColumnWidth = InchToPoint(paperSizeA4.Width); // paperSizeA4.Width: integer value
                forPrint.PagePadding = new Thickness(40);
                DocumentPaginator docPaginator = ((IDocumentPaginatorSource)forPrint).DocumentPaginator;
                docPaginator.ComputePageCount();


                this.IsEnabled = false;
                PrintDialog My_printDialog = new PrintDialog();
                if (My_printDialog.ShowDialog() == true)
                {
                    My_printDialog.PrintDocument(docPaginator, "My PDF print");
                }
            }
            finally
            {
                this.IsEnabled = true;
            }
        }
        /// <summary>
        /// This is the most importan method , modifies the original
        /// </summary>
        public override DocumentPage GetPage(int pageNumber)
        {
            DocumentPage page = paginator.GetPage(pageNumber);

            if (pageNumber == 0)
            {
                paginator.ComputePageCount();
                pageCount = paginator.PageCount;
            }

            var newpage = new ContainerVisual();

            if (pageDef.HeaderTemplate != null)
            {
                ContainerVisual v = getPartVisual(pageDef.HeaderTemplate, pageNumber);
                v.Offset = new Vector(pageDef.Margin.Width, pageDef.Margin.Height);
                newpage.Children.Add(v);
            }

            var smallerPage = new ContainerVisual();

            smallerPage.Children.Add(page.Visual);
            smallerPage.Offset = new Vector(pageDef.Margin.Width, pageDef.HeaderHeight + pageDef.Margin.Height + minimalOffset);
            newpage.Children.Add(smallerPage);

            if (pageDef.FooterTemplate != null)
            {
                ContainerVisual footer = getPartVisual(pageDef.FooterTemplate, pageNumber);
                footer.Offset = new Vector(pageDef.Margin.Width, pageSize.Height - pageDef.FooterHeight - pageDef.Margin.Height - minimalOffset);
                newpage.Children.Add(footer);
            }

            var dp = new DocumentPage(newpage, new Size(pageSize.Width, pageSize.Height), page.BleedBox, page.ContentBox);

            return(dp);
        }
示例#12
0
 /// <summary>
 /// Compute page count.
 /// </summary>
 public override void ComputePageCount()
 {
     flowDocPaginator.ComputePageCount();
 }
        private void OnPrintPreview(object sender, ExecutedRoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();

            printDialog.PageRangeSelection   = PageRangeSelection.AllPages;
            printDialog.UserPageRangeEnabled = true;
            bool?dialogResult = printDialog.ShowDialog();

            if (dialogResult != null && dialogResult.Value == false)
            {
                return;
            }

            FlowDocument printSource = this.CreateFlowDocumentForEditor();

            // Save all the existing settings.
            double    pageHeight  = printSource.PageHeight;
            double    pageWidth   = printSource.PageWidth;
            Thickness pagePadding = printSource.PagePadding;
            double    columnGap   = printSource.ColumnGap;
            double    columnWidth = printSource.ColumnWidth;

            // Make the FlowDocument page match the printed page.
            printSource.PageHeight  = printDialog.PrintableAreaHeight;
            printSource.PageWidth   = printDialog.PrintableAreaWidth;
            printSource.PagePadding = new Thickness(20);
            printSource.ColumnGap   = Double.NaN;
            printSource.ColumnWidth = printDialog.PrintableAreaWidth;

            Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);

            MemoryStream xpsStream        = new MemoryStream();
            Package      package          = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite);
            string       packageUriString = "memorystream://data.xps";

            PackageStore.AddPackage(new Uri(packageUriString), package);

            XpsDocument       xpsDocument = new XpsDocument(package, CompressionOption.Normal, packageUriString);
            XpsDocumentWriter writer      = XpsDocument.CreateXpsDocumentWriter(xpsDocument);

            DocumentPaginator paginator = ((IDocumentPaginatorSource)printSource).DocumentPaginator;

            paginator.PageSize = pageSize;
            paginator.ComputePageCount();

            writer.Write(paginator);

            // Reapply the old settings.
            printSource.PageHeight  = pageHeight;
            printSource.PageWidth   = pageWidth;
            printSource.PagePadding = pagePadding;
            printSource.ColumnGap   = columnGap;
            printSource.ColumnWidth = columnWidth;

            PrintPreviewWindow printPreview = new PrintPreviewWindow();

            printPreview.Width  = this.ActualWidth;
            printPreview.Height = this.ActualHeight;
            printPreview.Owner  = Application.Current.MainWindow;

            printPreview.LoadDocument(xpsDocument, package, packageUriString);

            printPreview.Show();
        }
示例#14
0
        /// <summary>
        /// This is most important method, modifies the original
        /// </summary>
        /// <param name="pageNumber">page number</param>
        /// <returns></returns>
        public override DocumentPage GetPage(int pageNumber)
        {
            for (int i = 0; i < 2; i++) // do it twice because filling context values could change the page count
            {
                // compute page count
                if (pageNumber == 0)
                {
                    _paginator.ComputePageCount();
                    _pageCount = _paginator.PageCount;
                }

                // fill context values
                FillContextValues(_reportContextValues, pageNumber + 1);
            }

            DocumentPage page = _paginator.GetPage(pageNumber);

            if (page == DocumentPage.Missing)
            {
                return(DocumentPage.Missing);                              // page missing
            }
            _pageSize = page.Size;

            // add header block
            ContainerVisual newPage = new ContainerVisual();

            if (_blockPageHeader != null)
            {
                ContainerVisual v = CloneVisualBlock(_blockPageHeader, pageNumber + 1);
                v.Offset = new Vector(0, 0);
                newPage.Children.Add(v);
            }

            // TODO: process ReportContextValues

            // add content page
            ContainerVisual smallerPage = new ContainerVisual();

            smallerPage.Offset = new Vector(0, _report.PageHeaderHeight / 100d * _report.PageHeight);
            smallerPage.Children.Add(page.Visual);
            newPage.Children.Add(smallerPage);

            // add footer block
            if (_blockPageFooter != null)
            {
                ContainerVisual v = CloneVisualBlock(_blockPageFooter, pageNumber + 1);
                v.Offset = new Vector(0, _report.PageHeight - _report.PageFooterHeight / 100d * _report.PageHeight);
                newPage.Children.Add(v);
            }

            // create modified BleedBox
            Rect bleedBox = new Rect(page.BleedBox.Left, page.BleedBox.Top, page.BleedBox.Width,
                                     _report.PageHeight - (page.Size.Height - page.BleedBox.Size.Height));

            // create modified ContentBox
            Rect contentBox = new Rect(page.ContentBox.Left, page.ContentBox.Top, page.ContentBox.Width,
                                       _report.PageHeight - (page.Size.Height - page.ContentBox.Size.Height));

            DocumentPage dp = new DocumentPage(newPage, new Size(_report.PageWidth, _report.PageHeight), bleedBox, contentBox);

            _report.FireEventGetPageCompleted(new GetPageCompletedEventArgs(page, pageNumber, null, false, null));

            if (_pageGeneratedCallBack != null)
            {
                _pageGeneratedCallBack(pageNumber, _pageCount);
            }
            return(dp);
        }
示例#15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentPaginatorEx" /> class.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="printableArea">The printable area size.</param>
        /// <param name="range">The print page range.</param>
        public DocumentPaginatorEx(FlowDocument document, Size printableArea, PageRange range)
        {
            // Clone the source document's content into a new FlowDocument.
            // This is because the pagination for the printer needs to be
            // done differently than the pagination for the displayed page.
            // We print the copy, rather that the original FlowDocument.
            var stream = new MemoryStream();
            var source = new TextRange(document.ContentStart, document.ContentEnd);

            source.Save(stream, DataFormats.Xaml);
            var documentCopy = new FlowDocument();
            var dest         = new TextRange(documentCopy.ContentStart, documentCopy.ContentEnd);

            dest.Load(stream, DataFormats.Xaml);

            // Ready to go on the copy of the document
            var paginatorSource = documentCopy as IDocumentPaginatorSource;

            _flowDocumentPaginator = paginatorSource.DocumentPaginator;
            CurrentPage            = 0;

            TotalPrintableArea = printableArea;
            var height = printableArea.Height;
            var width  = printableArea.Width;

            var docEx = document as FlowDocumentEx;

            if (docEx != null)
            {
                width  -= (docEx.PrintMargin.Left + docEx.PrintMargin.Right);
                height -= (docEx.PrintMargin.Top + docEx.PrintMargin.Bottom);
                DocumentPrintMargin = docEx.PrintMargin;
                OriginalPrintMargin = docEx.PrintMargin;

                Watermark = docEx.PrintWatermark;
                if (Watermark != null)
                {
                    Watermark.Resources   = document.Resources;
                    Watermark.DataContext = document.DataContext;
                    Watermark.Measure(printableArea);
                    Watermark.Arrange(new Rect(0, 0, Watermark.DesiredSize.Width, Watermark.DesiredSize.Height));
                }

                Header = docEx.PageHeader;
                if (Header != null)
                {
                    Header.Resources   = document.Resources;
                    Header.DataContext = document.DataContext;
                    Header.Width       = width;
                    Header.Measure(new Size(width, double.PositiveInfinity));
                    Header.Height = Header.DesiredSize.Height; // These two lines attempt to fix the size as desired and make sure it is properly measured at that
                    Header.Measure(new Size(width, Header.DesiredSize.Height));
                    Header.Arrange(new Rect(0, 0, Header.DesiredSize.Width, Header.DesiredSize.Height));
                    height -= Header.DesiredSize.Height;
                    DocumentPrintMargin = new Thickness(DocumentPrintMargin.Left, DocumentPrintMargin.Top + Header.DesiredSize.Height, DocumentPrintMargin.Right, DocumentPrintMargin.Bottom);
                }

                Footer = docEx.PageFooter;
                if (Footer != null)
                {
                    Footer.Resources   = document.Resources;
                    Footer.DataContext = document.DataContext;
                    Footer.Width       = width;
                    Footer.Measure(new Size(width, double.PositiveInfinity));
                    Footer.Height = Footer.DesiredSize.Height; // These two lines attempt to fix the size as desired and make sure it is properly measured at that
                    Footer.Measure(new Size(width, Footer.DesiredSize.Height));
                    Footer.Arrange(new Rect(0, 0, Footer.DesiredSize.Width, Footer.DesiredSize.Height));
                    height -= Footer.DesiredSize.Height;
                    DocumentPrintMargin = new Thickness(DocumentPrintMargin.Left, DocumentPrintMargin.Top, DocumentPrintMargin.Right, DocumentPrintMargin.Bottom + Footer.DesiredSize.Height);
                }
            }
            else
            {
                DocumentPrintMargin = new Thickness();
            }

            _flowDocumentPaginator.PageSize = new Size(width, height);
            _flowDocumentPaginator.ComputePageCount();
            TotalPages = _flowDocumentPaginator.PageCount;

            Range = range;
        }