Example #1
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages.
            PrintPreviewPages.Clear();

            // Clear the print canvas of preview pages.
            PrintCanvas.Children.Clear();

            // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed.
            RichTextBlockOverflow lastRTBOOnPage;

            // Get the PrintTaskOptions.
            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            // Get the page description to deterimine how big the page is.
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            // We know there is at least one page to be printed. passing null as the first parameter to
            // AddOnePrintPreviewPage tells the function to add the first page.
            lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);

            // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
            // page has extra content.
            while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Visibility.Visible)
            {
                lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
            }

            PreviewPagesCreated?.Invoke(PrintPreviewPages, EventArgs.Empty);

            PrintDocument printDoc = sender as PrintDocument;

            // Report the number of preview pages created.
            printDoc.SetPreviewPageCount(PrintPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #2
0
        void printDocument_Paginate(object sender, PaginateEventArgs e)
        {
            pages.Clear();
            PrintContainer.Children.Clear();

            RichTextBlockOverflow lastRTBOOnPage;
            PrintTaskOptions      printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
            PrintPageDescription  pageDescription = printingOptions.GetPageDescription(0);

            lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);

            while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
            }

            if (pagesCreated != null)
            {
                pagesCreated.Invoke(pages, null);
            }

            PrintDocument printDoc = (PrintDocument)sender;

            printDoc.SetPreviewPageCount(pages.Count, PreviewPageCountType.Intermediate);
        }
        /// <summary>
        /// This is the event handler for <see cref="PrintDocument.Paginate"/>.
        /// It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        private async void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            // Get the page description to determine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            if (_directPrint)
            {
                _canvasContainer.RequestedTheme = ElementTheme.Light;
                foreach (FrameworkElement element in this._canvasContainer.Children)
                {
                    _printPreviewPages.Add(element);
                }
            }
            else
            {
                // Attach the canvas
                if (!_canvasContainer.Children.Contains(_printCanvas))
                {
                    _canvasContainer.Children.Add(_printCanvas);
                }

                _canvasContainer.RequestedTheme = ElementTheme.Light;

                // Clear the cache of preview pages
                await ClearPageCache();

                // Clear the print canvas of preview pages
                _printCanvas.Children.Clear();
Example #4
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            printPreviewPages.Clear();

            PrintTaskOptions     printingOptions      = ((PrintTaskOptions)e.PrintTaskOptions);
            PrintPageDescription printPageDescription = printingOptions.GetPageDescription(0);

            foreach (Page page in this.PagesToPrint)
            {
                // Set "paper" width
                page.Width  = printPageDescription.PageSize.Width;
                page.Height = printPageDescription.PageSize.Height;

                // Get the margins size
                // If the ImageableRect is smaller than the app provided margins use the ImageableRect
                double marginWidth  = Math.Max(printPageDescription.PageSize.Width - printPageDescription.ImageableRect.Width, printPageDescription.PageSize.Width * ApplicationContentMarginLeft * 2);
                double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * ApplicationContentMarginTop * 2);

                Grid printableArea = (Grid)page.FindName("printableArea");

                // Set-up "printable area" on the "paper"
                printableArea.Width  = page.Width - marginWidth;
                printableArea.Height = page.Height - marginHeight;

                printPreviewPages.Add(page);
            }

            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #5
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        private void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            ClearPageCache();

            // Clear the print canvas of preview pages
            _printCanvas.Children.Clear();

            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            // Get the page description to determine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            foreach (var element in _elementsToPrint)
            {
                AddOnePrintPreviewPage(element, pageDescription);
            }

            OnPreviewPagesCreated?.Invoke(_printPreviewPages);

            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #6
0
        public static void DebugPrintTaskOptionDetails(this PrintTaskOptions options, Int32 currentPreviewPageNumber)
        {
            options.DebugPrintTaskOptionDetails();
            var pageDescription = options.GetPageDescription((UInt32)currentPreviewPageNumber);

            Debug.WriteLine("Page Description - DPI X={0}, Y={1}", pageDescription.DpiX, pageDescription.DpiY);
            Debug.WriteLine("Page Description - Page Size Width={0}, Height={1} (Device Independent Pixels)", pageDescription.PageSize.Width, pageDescription.PageSize.Height);
            Debug.WriteLine("Page Description - Imageable Rect={0} (Device Independent Pixels)", pageDescription.ImageableRect);
        }
Example #7
0
        /// <summary>
        /// This is the event handler for PrintDocument.GetPrintPreviewPage. It provides a specific print preview page,
        /// in the form of an UIElement, to an instance of PrintDocument. PrintDocument subsequently converts the UIElement
        /// into a page that the Windows print system can deal with.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Arguments containing the preview requested page</param>
        private async void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
        {
            PrintDocument printDoc  = (PrintDocument)sender;
            Size          paperSize = option.GetPageDescription((uint)e.PageNumber).PageSize;
            double        rate      = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

            paperSize = new Size(paperSize.Width / rate, paperSize.Height / rate);
            printDoc.SetPreviewPage(e.PageNumber, await c1PdfViewer1.GetPage(e.PageNumber, paperSize));
        }
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            printPreviewPages.Clear();

            // Clear the printing root of preview pages
            PrintingRoot.Children.Clear();

            // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
            RichTextBlockOverflow lastRTBOOnPage;

            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

            // Get the page description to deterimine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);


            // XAML element that is used to represent to "printing page"
            FrameworkElement page = firstPage;

            // Set "paper" width
            page.Width  = pageDescription.PageSize.Width;
            page.Height = pageDescription.PageSize.Height;

            Grid printableArea = (Grid)page.FindName("printableArea");

            // Get the margins size
            // If the ImageableRect is smaller than the app provided margins use the ImageableRect
            double marginWidth  = Math.Max(pageDescription.PageSize.Width - pageDescription.ImageableRect.Width, pageDescription.PageSize.Width * ApplicationContentMarginLeft * 2);
            double marginHeight = Math.Max(pageDescription.PageSize.Height - pageDescription.ImageableRect.Height, pageDescription.PageSize.Height * ApplicationContentMarginTop * 2);

            // Set-up "printable area" on the "paper"
            printableArea.Width  = firstPage.Width - marginWidth;
            printableArea.Height = firstPage.Height - marginHeight;

            // Add the (newly created) page to the printing root which is part of the visual tree and force it to go
            // through layout so that the linked containers correctly distribute the content inside them.
            PrintingRoot.Children.Add(page);
            PrintingRoot.InvalidateMeasure();
            PrintingRoot.UpdateLayout();

            // Add the page to the page preview collection
            printPreviewPages.Add(page);

            if (pagesCreated != null)
            {
                pagesCreated.Invoke(printPreviewPages, null);
            }

            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #9
0
        /// <summary>
        /// This is the event handler for <see cref="PrintDocument.Paginate"/>.
        /// It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        private async void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            // Get the page description to determine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            if (_directPrint)
            {
                _canvasContainer.RequestedTheme = ElementTheme.Light;
                foreach (FrameworkElement element in this._canvasContainer.Children)
                {
                    _printPreviewPages.Add(element);
                }
            }
            else
            {
                // Attach the canvas
                if (!_canvasContainer.Children.Contains(_printCanvas))
                {
                    _canvasContainer.Children.Add(_printCanvas);
                }

                _canvasContainer.RequestedTheme = ElementTheme.Light;

                // Clear the cache of preview pages
                await ClearPageCache();

                // Clear the print canvas of preview pages
                _printCanvas.Children.Clear();

                var printPageTasks = new List <Task>();
                foreach (var element in _elementsToPrint)
                {
                    printPageTasks.Add(AddOnePrintPreviewPage(element, pageDescription));
                }

                await Task.WhenAll(printPageTasks);
            }

            OnPreviewPagesCreated?.Invoke(_printPreviewPages);

            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            _printCanvas.UpdateLayout();
            printDoc.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate);
            //printDoc.SetPreviewPage(_printPreviewPages.Count, _printPreviewPages[_printPreviewPages.Count - 1]);
            if (_printPreviewPages.Count != 0)
            {
                printDoc.SetPreviewPage(1, _printPreviewPages[0]);
            }
        }
Example #10
0
File: Printer.cs Project: beryah/Yo
        private void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            this.PrintPreviewPages = this.GetPreviewPages();

            PrintDocument printDoc = (PrintDocument)sender;

            printDoc.SetPreviewPageCount(this.PrintPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
        protected void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            printPreviewElements.Clear();
            PrintTaskOptions     printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            AddOnePrintPreviewPage(pageDescription);
            if (pagesCreated != null)
            {
                pagesCreated.Invoke(printPreviewElements, null);
            }
            ((PrintDocument)sender).SetPreviewPageCount(printPreviewElements.Count, PreviewPageCountType.Intermediate);
        }
Example #12
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event  </param>
        public virtual async void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            var paperSize = e.PrintTaskOptions.GetPageDescription(0).PageSize;

            System.Diagnostics.Debug.WriteLine("CreatePrintPreviewPages: {" + paperSize.Width + "," + paperSize.Height + "}");

            //lock (printPreviewPages)
            await _semaphoreSlim.WaitAsync();

            {
                // Clear the cache of preview pages
                printPreviewPages.Clear();

                // Clear the print canvas of preview pages
                PrintCanvas.Children.Clear();

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

                // Get the page description to deterimine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);



                if (await GeneratePagesAsync(pageDescription) is List <UIElement> pages)
                {
                    foreach (var page in pages)
                    {
                        PrintCanvas.Children.Add(page);
                    }
                    PrintCanvas.InvalidateMeasure();
                    PrintCanvas.UpdateLayout();

                    await Task.Delay(1000);

                    printPreviewPages.AddRange(pages);
                    await Task.Delay(1000);
                }

                if (PreviewPagesCreated != null)
                {
                    PreviewPagesCreated.Invoke(printPreviewPages, null);
                }

                PrintDocument printDoc = (PrintDocument)sender;

                // Report the number of preview pages created
                printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
            }
            _semaphoreSlim.Release();
        }
Example #13
0
        private async void PrintDoc_Paginate(object sender, PaginateEventArgs e)
        {
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

            deviceCollection = await DeviceInformation.FindAllAsync("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");

            var rolloPrinter = deviceCollection.Where(x => x.Name.Contains("Rollo")).SingleOrDefault();

            // Get the page description to deterimine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            PrintTaskOptions opt = Task.Options;

            printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
        }
Example #14
0
        private void PrintDocument_Paginate(object sender, PaginateEventArgs e)
        {
            Debug.WriteLine("PrintDocument_Paginate");

            PrintTaskOptions     printingOptions      = ((PrintTaskOptions)e.PrintTaskOptions);
            PrintPageDescription printPageDescription = printingOptions.GetPageDescription(0);

            page = this;
            //NewsGrid.Width = double.NaN;
            //NewsGrid.Height = double.NaN;
            //Grid grid = new Grid();
            //grid.Children.Add(htmlBlock);


            printDocument.SetPreviewPageCount(2, PreviewPageCountType.Final);
        }
Example #15
0
        private void CreatePrintPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            _printPages.Clear();

            // Clear the printing root of preview pages
            PrintingRoot.Children.Clear();

            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            // Get the page description to deterimine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            AddPrintPages(pageDescription);

            // Report the number of preview pages created
            _printDocument.SetPreviewPageCount(_printPages.Count, PreviewPageCountType.Intermediate);
        }
Example #16
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            printPreviewPages.Clear();

            // Clear the printing root of preview pages
            PrintingRoot.Children.Clear();

            // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
            RichTextBlockOverflow lastRTBOOnPage;

            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

            // Get the page description to deterimine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);


            PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(e.PrintTaskOptions);
            //string pageContentSettings = (printDetailedOptions.Options["PageContent"].Value as string).ToLowerInvariant();
            string tagsSettings = (printDetailedOptions.Options["Tags"].Value as string).ToLowerInvariant();

            //imageText = (DisplayContent)((Convert.ToInt32(pageContentSettings.Contains("pictures")) << 1) | Convert.ToInt32(pageContentSettings.Contains("text")));
            ShowTags = tagsSettings.Contains("show");

            // We know there is at least one page to be printed. passing null as the first parameter to
            // AddOnePrintPreviewPage tells the function to add the first page.
            lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);

            // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
            // page has extra content
            while (lastRTBOOnPage.HasOverflowContent)
            {
                lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
            }

            if (pagesCreated != null)
            {
                pagesCreated.Invoke(printPreviewPages, null);
            }

            // Report the number of preview pages created
            printDocument.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #17
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            printPreviewPages.Clear();
            PrintTaskOptions     printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            foreach (var page in Pages)
            {
                page.Height = pageDescription.PageSize.Height;
                page.Width  = pageDescription.PageSize.Width;
                printPreviewPages.Add(page);
            }

            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #18
0
        /// <summary>
        /// This is the event handler for <see cref="PrintDocument.Paginate"/>.
        /// It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        private void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            // Get the page description to determine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            if (_directPrint)
            {
                _canvasContainer.RequestedTheme = ElementTheme.Light;
                foreach (FrameworkElement element in this._canvasContainer.Children)
                {
                    _printPreviewPages.Add(element);
                }
            }
            else
            {
                // Attach the canvas
                if (!_canvasContainer.Children.Contains(_printCanvas))
                {
                    _canvasContainer.Children.Add(_printCanvas);
                }

                // Clear the cache of preview pages
                ClearPageCache();

                // Clear the print canvas of preview pages
                _printCanvas.Children.Clear();

                foreach (var element in _elementsToPrint)
                {
                    AddOnePrintPreviewPage(element, pageDescription);
                }
            }

            OnPreviewPagesCreated?.Invoke(_printPreviewPages);

            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #19
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)  // Required  2. called
        {
            lock (_printPreviewPages)
            {
                // Clear the cache of preview pages
                _printPreviewPages.Clear();

                // Clear the print canvas of preview pages
                //PrintCanvas?.Children.Clear();

                // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
                //RichTextBlockOverflow lastRTBOOnPage;

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

                // Get the page description to determine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

                // We know there is at least one page to be printed. passing null as the first parameter to
                // AddOnePrintPreviewPage tells the function to add the first page.
                //lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);
                AddOnePrintPreviewPage(null, pageDescription);

                // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
                // page has extra content
                //while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
                //{
                //    lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
                //}

                if (PreviewPagesCreated != null)
                {
                    PreviewPagesCreated.Invoke(_printPreviewPages, null);
                }

                PrintDocument printDoc = (PrintDocument)sender;

                // Report the number of preview pages created
                printDoc.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate);
            }
        }
Example #20
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        private static void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            lock (_printPreviewPages)
            {
                // Clear the cache of preview pages
                _printPreviewPages.Clear();

                // Clear the print canvas of preview pages
                PrintCanvas.Children.Clear();

                // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
                RichTextBlockOverflow lastRTBOOnPage;

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = e.PrintTaskOptions;

                // Get the page description to determine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

                var count = 0;
                do
                {
                    // We know there is at least one page to be printed. passing null as the first parameter to
                    // AddOnePrintPreviewPage tells the function to add the first page.
                    lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription, count);

                    // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
                    // page has extra content
                    while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Visibility.Visible)
                    {
                        lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription, count);
                    }

                    count += 1;
                } while (count < _firstPage.Count);

                PrintDocument printDoc = (PrintDocument)sender;

                // Report the number of preview pages created
                printDoc.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate);
            }
        }
Example #21
0
        private void OnPaginate(object sender, PaginateEventArgs e)
        {
            this.pages.Clear();

            List <double> heights = new List <double>();

            foreach (var group in this.viewmodel.SmartCollection.Items)
            {
                heights.Add(GroupItemHeight);
                foreach (var task in group)
                {
                    heights.Add(TaskItemHeight);
                }
            }

            PrintDocument    printDoc        = (PrintDocument)sender;
            PrintTaskOptions printingOptions = e.PrintTaskOptions;

            int startIndex = 0;

            while (heights.Count > 0)
            {
                int itemsInPage = 0;

                PrintPageDescription pageDescription = printingOptions.GetPageDescription((uint)this.pages.Count);
                double availableHeight = pageDescription.PageSize.Height - (2 * GetVerticalMargin(pageDescription));

                while (heights.Count > 0 && heights[0] < availableHeight)
                {
                    availableHeight -= heights[0];
                    heights.RemoveAt(0);
                    itemsInPage++;
                }

                // create page
                this.pages.Add(this.CreatePage(startIndex, startIndex + itemsInPage, pageDescription));

                startIndex += itemsInPage;
            }

            printDoc.SetPreviewPageCount(this.pages.Count, PreviewPageCountType.Intermediate);
        }
        /// <summary>
        /// This is the event handler for PrintDocument.AddPages. It provides all pages to be printed, in the form of
        /// UIElements, to an instance of PrintDocument. PrintDocument subsequently converts the UIElements
        /// into a pages that the Windows print system can deal with.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Add page event arguments containing a print task options reference</param>
        protected override void AddPrintPages(object sender, AddPagesEventArgs e)
        {
            // Clear the cache of print pages
            printPages.Clear();

            // Clear the print canvas of print pages
            PrintCanvas.Children.Clear();

            // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
            RichTextBlockOverflow lastRTBOOnPage;

            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

            // Get the page description to deterimine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            // We know there is at least one page to be printed. passing null as the first parameter to
            // AddOnePrintPage tells the function to add the first page.
            lastRTBOOnPage = AddOnePrintPage(null, pageDescription);

            // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print
            // page has extra content
            while (lastRTBOOnPage.HasOverflowContent && (lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible))
            {
                lastRTBOOnPage = AddOnePrintPage(lastRTBOOnPage, pageDescription);
            }

            // Loop over all of the pages and add each one to add each page to be printed
            for (int i = 0; i < printPages.Count; i++)
            {
                // We should have all pages ready at this point...
                printDocument.AddPage(printPages[i]);
            }

            PrintDocument printDoc = (PrintDocument)sender;

            // Indicate that all of the print pages have been provided
            printDoc.AddPagesComplete();
        }
Example #23
0
        private void PrintDocument_Paginate(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            printPreviewPages.Clear();
            this.pageNumber = 0;

            // Clear the printing root of preview pages
            PrintingRoot.Children.Clear();

            for (int i = 0; i < NeedToPrintPages.Count; i++)
            {
                // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
                RichTextBlockOverflow lastRTBOOnPage;

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

                // Get the page description to deterimine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

                // We know there is at least one page to be printed. passing null as the first parameter to
                // AddOnePrintPreviewPage tells the function to add the first page.
                lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription, i);

                // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
                // page has extra content
                while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
                {
                    lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription, i);
                }
            }



            PrintDocument printDoc = (PrintDocument)sender;

            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
Example #24
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            lock (printPreviewPages)
            {
                // Clear the cache of preview pages
                printPreviewPages.Clear();

                // Clear the print canvas of preview pages
                PrintCanvas.Children.Clear();

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

                // Get the page description to deterimine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

                if (print_Page == 1)
                {
                    // setup print page 1
                    AddOnePrintPreviewPage(1, pageDescription, ScoutName, MeritBadge);
                }

                if (print_Page == 2)
                {
                    // setup print page 2
                    AddOnePrintPreviewPage(2, pageDescription, ScoutName, MeritBadge);
                }

                if (PreviewPagesCreated != null)
                {
                    PreviewPagesCreated.Invoke(printPreviewPages, null);
                }

                PrintDocument printDoc = (PrintDocument)sender;

                // Report the number of preview pages created
                printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
            }
        }
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            // Clear the cache of preview pages
            _printPreviewPages.Clear();

            // Clear the printing root of preview pages
            PrintingRoot.Children.Clear();

            // Get the PrintTaskOptions
            PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

            // Get the page description to deterimine how big the page is
            PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

            AddPrintPreviewPages(pageDescription);

            if (pagesCreated != null)
            {
                pagesCreated.Invoke(_printPreviewPages, null);
            }

            // Report the number of preview pages created
            _printDocument.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
    private static void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
    {
        // Clear the cache of preview pages
        printPreviewPages.Clear();

        // Clear the printing root of preview pages
        PrintingRoot.Children.Clear();

        // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
        RichTextBlockOverflow lastRTBOOnPage;

        // Get the PrintTaskOptions
        PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

        // Get the page description to deterimine how big the page is
        PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

        // We know there is at least one page to be printed. passing null as the first parameter to
        // AddOnePrintPreviewPage tells the function to add the first page.
        lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);

        // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
        // page has extra content
        while (lastRTBOOnPage.HasOverflowContent)
        {
            lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
        }

        if (pagesCreated != null)
        {
            pagesCreated.Invoke(printPreviewPages, null);
        }

        // Report the number of preview pages created
        printDocument.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
    }
Example #27
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event  </param>
        public virtual async void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            var paperSize = e.PrintTaskOptions.GetPageDescription(0).PageSize;

            System.Diagnostics.Debug.WriteLine("CreatePrintPreviewPages: {" + paperSize.Width + "," + paperSize.Height + "}");

            //lock (printPreviewPages)
            await _semaphoreSlim.WaitAsync();

            try
            {
                // Clear the cache of preview pages
                printPreviewPages.Clear();

                // Clear the print canvas of preview pages
                PrintCanvas.Children.Clear();

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);

                // Get the page description to deterimine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

                if (await GeneratePagesAsync(pageDescription) is List <UIElement> pages)
                {
                    foreach (var page in pages)
                    {
                        PrintCanvas.Children.Add(page);
                    }
                    PrintCanvas.InvalidateMeasure();
                    PrintCanvas.UpdateLayout();

                    await Task.Delay(1000);

                    printPreviewPages.AddRange(pages);
                    await Task.Delay(1000);
                }

                if (PreviewPagesCreated != null)
                {
                    PreviewPagesCreated.Invoke(printPreviewPages, null);
                }

                PrintDocument printDoc = (PrintDocument)sender;

                // Report the number of preview pages created
                printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
            }
            catch (Exception pve)
            {
                P42.Utils.BreadCrumbs.AddException(pve, GetType());

                if (pve.Message.Contains("The RPC server is unavailable", StringComparison.OrdinalIgnoreCase))
                {
                    using (var toast = Forms9Patch.Toast.Create("The RPC server is unavailable", "Windows failed trying to setup the print preview because it could not connect to its RPC server.   There are three basic potential causes for this error message. Either the RPC service is not running, there are issues with the network, or some important registry entries that control the RPC service have been corrupted. In Windows 10, the most common cause for the error is that the RPC service is simply not running.  <a id='link' href='https://www.techjunkie.com/rpc-server-is-unavailable/'>Click here to learn more about how to fix this.</a>"))
                    {
                        toast.ActionTagTapped += On_RpcUnavailable_Toast_ActionTagTapped;
                    }
                }
                else
                {
                    using (Forms9Patch.Toast.Create("Cannot print", "Windows failed trying to setup the print preview.  Below is some information from Windows about the failure.  \n\n" + pve.Message)) { }
                }
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Example #28
0
        /// <summary>
        /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app.
        /// </summary>
        /// <param name="sender">PrintDocument</param>
        /// <param name="e">Paginate Event Arguments</param>
        protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            lock (printPreviewPages)
            {
                // Clear the cache of preview pages
                printPreviewPages.Clear();

                // Clear the print canvas of preview pages
                PrintCanvas.Children.Clear();

                // Get the PrintTaskOptions
                PrintTaskOptions printingOptions = e.PrintTaskOptions;

                // Get the page description to determine how big the page is
                PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);

                var pageWidth  = pageDescription.PageSize.Width;
                var pageHeight = pageDescription.PageSize.Height;

                // If the ImageableRect is smaller than the app provided margins use the ImageableRect
                double marginWidth  = Math.Max(pageDescription.PageSize.Width - pageDescription.ImageableRect.Width, _printModel.HorizontalMargin * 2);
                double marginHeight = Math.Max(pageDescription.PageSize.Height - pageDescription.ImageableRect.Height, _printModel.VerticalMargin * 2);

                // Set-up "printable area" on the "paper"
                var printableAreaWidth  = pageWidth - marginWidth;
                var printableAreaHeight = pageHeight - marginHeight;

                int charsPerPage = FontMapPrintPage.CalculateGlyphsPerPage(
                    new Size(Math.Floor(printableAreaWidth), Math.Floor(printableAreaHeight)),
                    _printModel);

                bool hasMore     = true;
                int  currentPage = _printModel.FirstPage - 1;
                while (hasMore && printPreviewPages.Count < _printModel.PagesToPrint)
                {
                    var page = new FontMapPrintPage(_printModel, fontMap.CharGrid.ItemTemplate)
                    {
                        Width  = pageWidth,
                        Height = pageHeight
                    };

                    Grid printableArea = page.PrintableArea;

                    // Set-up "printable area" on the "paper"
                    printableArea.Width  = printableAreaWidth;
                    printableArea.Height = printableAreaHeight;

                    // Layout page
                    hasMore = page.AddCharacters(currentPage, charsPerPage, _printModel.Characters);
                    currentPage++;

                    // Add the (newly created) page to the print canvas which is part of the visual tree and force it to go
                    // through layout so that the linked containers correctly distribute the content inside them.
                    PrintCanvas.Children.Add(page);
                    PrintCanvas.InvalidateMeasure();
                    PrintCanvas.UpdateLayout();

                    printPreviewPages.Add(new PrintPage(printPreviewPages.Count + 1, page));
                }

                if (PreviewPagesCreated != null)
                {
                    PreviewPagesCreated.Invoke(printPreviewPages, null);
                }

                PrintDocument printDoc = (PrintDocument)sender;

                // Report the number of preview pages created
                printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Final);
            }
        }
Example #29
0
 private static Rect GetImageableRect(PrintTaskOptions options, uint pageNumber)
 => options.GetPageDescription(pageNumber).ImageableRect;
        /// <summary>
        /// “导出”的关键方法,用于添加打印选项和设置打印页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintDocument_Paginate(object sender, PaginateEventArgs e)
        {
            PrintTaskOptions     printingOptions      = e.PrintTaskOptions;
            PrintPageDescription printPageDescription = printingOptions.GetPageDescription(0);

            RememberInlineUIElementsLayout(htmlBlock);

            //创建新打印页面
            page = new PageToPrint
            {
                Width             = printPageDescription.PageSize.Width,
                Height            = printPageDescription.PageSize.Height,
                VerticalAlignment = VerticalAlignment.Top
            };
            printPanel.Children.Add(page);
            printPanel.InvalidateMeasure();
            printPanel.UpdateLayout();

            //设置新页面
            pagesToPrint = new List <UIElement>();

            //记录RichTextBlock中每个控件的大小
            RememberInlineUIElementsLayout(htmlBlock);

            //将控件迁移到打印页面
            newsPanel.Width   = newsPanel.ActualWidth;
            newsPanel.Height  = printPageDescription.PageSize.Height;
            oldCanvasSize     = newsCanvas.ActualHeight;
            headerPanel.Width = headerPanel.ActualWidth;
            htmlBlock.Width   = htmlBlock.ActualWidth;
            htmlBlock.Height  = printPageDescription.PageSize.Height - headerPanel.ActualHeight;
            grid = new Grid()
            {
                Width             = newsPanel.Width,
                Height            = newsPanel.Height,
                VerticalAlignment = VerticalAlignment.Top
            };
            contentGrid.Children.Remove(newsPanel);
            grid.Children.Add(newsPanel);
            page.PrintableContentViewBox.Child = grid;
            UpdateInlineUIElementsLayout(htmlBlock, new Size(htmlBlock.Width, htmlBlock.Height));
            //更新布局
            page.InvalidateMeasure();
            page.UpdateLayout();

            //将页面添加到页面列表
            pagesToPrint.Add(page);

            bool hasOverflowContent = false;
            RichTextBlockOverflow richTextBlockOverflow = null;

            if (htmlBlock.HasOverflowContent)
            {
                hasOverflowContent    = true;
                richTextBlockOverflow = new RichTextBlockOverflow
                {
                    Height            = grid.ActualHeight,
                    Width             = grid.Width,
                    VerticalAlignment = VerticalAlignment.Top,
                };
                htmlBlock.OverflowContentTarget = richTextBlockOverflow;
            }


            while (hasOverflowContent)
            {
                ContinueonPageToPrint continueonPageToPrint = new ContinueonPageToPrint()
                {
                    Width             = printPageDescription.PageSize.Width,
                    Height            = printPageDescription.PageSize.Height,
                    VerticalAlignment = VerticalAlignment.Top
                };
                printPanel.Children.Add(continueonPageToPrint);
                printPanel.InvalidateMeasure();
                printPanel.UpdateLayout();

                Grid overFlowGrid = new Grid()
                {
                    Width  = grid.Width,
                    Height = grid.Height
                };
                StackPanel stackPanel = new StackPanel()
                {
                    Height = newsPanel.Height,
                    Width  = newsPanel.Width
                };
                stackPanel.Children.Add(richTextBlockOverflow);
                continueonPageToPrint.PrintableContentViewBox.Child = stackPanel;
                continueonPageToPrint.InvalidateMeasure();
                continueonPageToPrint.UpdateLayout();
                pagesToPrint.Add(continueonPageToPrint);

                if (richTextBlockOverflow.HasOverflowContent)
                {
                    hasOverflowContent = true;
                    var oldRichTextBlockOverflow = richTextBlockOverflow;
                    richTextBlockOverflow = new RichTextBlockOverflow
                    {
                        Height            = grid.ActualHeight,
                        Width             = htmlBlock.Width,
                        VerticalAlignment = VerticalAlignment.Top,
                    };
                    oldRichTextBlockOverflow.OverflowContentTarget = richTextBlockOverflow;
                }
                else
                {
                    hasOverflowContent = false;
                }
            }
            printDocument.SetPreviewPageCount(pagesToPrint.Count, PreviewPageCountType.Final);
        }