fixed document print

vitaminchik 486 Reputation points
2023-06-19T19:02:13.81+00:00
private void PrintDocument(object sender, RoutedEventArgs e)
        {
            PrintDialog pDialog = new PrintDialog();
            var printers = new LocalPrintServer().GetPrintQueues();
            var selectedPrinter = printers.FirstOrDefault(p => p.Name == "Microsoft Print to PDF");
            if (selectedPrinter == null)
            {
                MessageBox.Show("Printer not found!");
                return;
            }
            pDialog.PrintQueue = selectedPrinter;
            FixedDocument fixedDoc = new FixedDocument();
            PageContent pageContent = new PageContent();
            FixedPage fixedPage = new FixedPage();
            fixedPage.Width = 1056;
            fixedPage.Height = 816;
            var gridToRemove = scrollViewerOfTable.Content as Grid;
            if (gridToRemove != null && gridToRemove.Name == "gridOfCreatingTableOfCloth")
            {
                scrollViewerOfTable.Content = null;
                fixedPage.Children.Add(gridToRemove);
                ((IAddChild)pageContent).AddChild(fixedPage);
                fixedDoc.Pages.Add(pageContent);
                //Create any other required pages here
                DocumentViewer documentViewer1 = new DocumentViewer();
                //View the document
                scrollViewerOfTable.Content= documentViewer1;
                documentViewer1.Document = fixedDoc;
                pDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
                pDialog.PrintTicket.PageMediaSize = new PageMediaSize(fixedPage.Width, fixedPage.Height);
                IDocumentPaginatorSource paginatorSource = documentViewer1.Document as IDocumentPaginatorSource;
                DocumentPaginator paginator = paginatorSource.DocumentPaginator;
                paginator.PageSize = new Size(fixedPage.Width, fixedPage.Height);
                pDialog.PrintDocument(paginator, " ");
            }
        }
Help edit the code. My code renders everything correctly, but when the grid is too big, it gets cut off and doesn't show up on other pages.
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2023-06-21T09:35:08.38+00:00

    Hi,@vitaminchik. You could use the following code.

         private void SaveDocumentOfTable(object sender, RoutedEventArgs e)
            {
                PrintDialog pDialog = new PrintDialog();
                var printers = new LocalPrintServer().GetPrintQueues();
                var selectedPrinter = printers.FirstOrDefault(p => p.Name == "Microsoft Print to PDF");
                if (selectedPrinter == null)
                {
                    MessageBox.Show("Printer not found!");
                    return;
                }
                var gridToRemove = scrollViewerOfTable.Content as Grid;
                pDialog.PrintQueue = selectedPrinter;
    
                pDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
               
             
                if (gridToRemove != null && gridToRemove.Name == "gridOfCreatingTableOfCloth")
                {
                    scrollViewerOfTable.Content = null;
    
                    FixedDocument fixedDoc = PrintHelper.GetFixedDocument(gridToRemove, pDialog);
                 
    
                    DocumentPaginator paginator = fixedDoc.DocumentPaginator;
               
                    pDialog.PrintDocument(paginator, " ");
                }
            }
    
    
    

    PrintHelper:

     public static class PrintHelper
        {
    
            public static FixedDocument GetFixedDocument(FrameworkElement toPrint, PrintDialog printDialog)
            {
                PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
                Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
                Size visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
                FixedDocument fixedDoc = new FixedDocument();
                //If the toPrint visual is not displayed on screen we neeed to measure and arrange it  
                toPrint.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                toPrint.Arrange(new Rect(new Point(0, 0), toPrint.DesiredSize));
                //  
                Size size = toPrint.DesiredSize;
                //Will assume for simplicity the control fits horizontally on the page  
                double yOffset = 0;
                while (yOffset < size.Height)
                {
                    VisualBrush vb = new VisualBrush(toPrint);
                    vb.Stretch = Stretch.None;
                    vb.AlignmentX = AlignmentX.Left;
                    vb.AlignmentY = AlignmentY.Top;
                    vb.ViewboxUnits = BrushMappingMode.Absolute;
                    vb.TileMode = TileMode.None;
                    vb.Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height);
                    PageContent pageContent = new PageContent();
                    FixedPage page = new FixedPage();
                    ((IAddChild)pageContent).AddChild(page);
                    fixedDoc.Pages.Add(pageContent);
                    page.Width = pageSize.Width;
                    page.Height = pageSize.Height;
                    Canvas canvas = new Canvas();
                    FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                    FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
                    canvas.Width = visibleSize.Width;
                    canvas.Height = visibleSize.Height;
                    canvas.Background = vb;
                    page.Children.Add(canvas);
                    yOffset += visibleSize.Height;
                }
                return fixedDoc;
            }
    
            public static void ShowPrintPreview(FixedDocument fixedDoc)
            {
                Window wnd = new Window();
                DocumentViewer viewer = new DocumentViewer();
                viewer.Document = fixedDoc;
                wnd.Content = viewer;
                wnd.ShowDialog();
            }
    
        }
    

    The result:

    User's image


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.