locked
[W8.1]Scaling for printing in single page? RRS feed

  • Question

  • I have a canvas.

    <Canvas Canvas.ZIndex="1" x:Name="MainCanvas" x:FieldModifier="public" Width="2301" Height="1671"></Canvas>

    Here is my c# code to print this canvas.

    #region Printing Stuff
            //MainPage rootPage;
            PrintDocument printDocument;
            IPrintDocumentSource printDocumentSource;
            
            Canvas canvas = new Canvas();
    
            protected void RegisterForPrinting()
            {
                // Create the PrintDocument.
                printDocument = new PrintDocument();
    
                // Save the DocumentSource.
                printDocumentSource = printDocument.DocumentSource;
    
                // Add an event handler which creates preview pages.
                printDocument.Paginate += printDocument_Paginate;
    
                // Add an event handler which provides a specified preview page.
                printDocument.GetPreviewPage += printDocument_GetPreviewPage;
    
                // Add an event handler which provides all final print pages.
                printDocument.AddPages += printDocument_AddPages;
    
                // Create a PrintManager and add a handler for printing initialization.
                PrintManager printMan = PrintManager.GetForCurrentView();
    
                printMan.PrintTaskRequested += printMan_PrintTaskRequested;
    
            }
    
            private async void InvokePrintButtonClick(object sender, RoutedEventArgs e)
            {
                // Don't act when in snapped mode
                /*if (ApplicationView.Value != ApplicationViewState.Snapped)
                {
                    await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
                }*/
            }
    
            void printMan_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
            {
                PrintTask printTask = null;
                printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
                {
    
                    // Print Task event handler is invoked when the print job is completed.
                    printTask.Completed += async (s, args) =>
                    {
                        // Notify the user when the print operation fails.
                        if (args.Completion == PrintTaskCompletion.Failed)
                        {
                            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                //Debug.WriteLine("Failed to print.");
                                ///rootPage.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
                            });
                        }
                    };
    
                    sourceRequested.SetSource(printDocumentSource);
                });
                printTask.Options.Orientation = PrintOrientation.Landscape;
            }
    
            async void  printDocument_AddPages(object sender, AddPagesEventArgs e)
            {
                
                Canvas[] previewCanvas = await GetPageCanvas();
                int i = 0;
                while (i < MainListView.Items.Count)
                {
                    printDocument.AddPage(previewCanvas[i]);
                    
                    i++;
                }
                //printDocument.AddPage(canvas);
    
                PrintDocument printDoc = (PrintDocument)sender;
    
                // Indicate that all of the print pages have been provided
                printDoc.AddPagesComplete();
    
            }
    
            async void printDocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
            {
                try
                {
                    
                    PrintDocument printDoc = (PrintDocument)sender;
                    
                    Canvas[] previewCanvas = await GetPageCanvas();
    
    
                    printDoc.SetPreviewPage(e.PageNumber, previewCanvas[e.PageNumber-1]);
                }
                catch { }
                
            }
    
            void printDocument_Paginate(object sender, PaginateEventArgs e)
            {
    
                PrintDocument printDoc = (PrintDocument)sender;
                
                // Report the number of preview pages created
                try
                {
                    printDoc.SetPreviewPageCount(MainListView.Items.Count, PreviewPageCountType.Intermediate);
                }
                catch { }
            }
    
            public async Task<Canvas[]> GetPageCanvas()
            {
                Canvas[] thisCanvas = new Canvas[MainListView.Items.Count];
                int i = 0;
    
                while (i < MainListView.Items.Count)
                {
                    Viewbox viewbox = MainListView.Items[i] as Viewbox;
                    IDRForm page = viewbox.Child as IDRForm;
                    thisCanvas[i] = page.MainCanvas;
                    i++;
                }
                return thisCanvas;
            }
    
            #endregion
    How do I print the whole canvas in 1 page?

    Saturday, June 13, 2015 10:55 PM

Answers

  • Hi RandyPete,

    Welcome to the Developing Universal Windows apps forum! 

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools .

    >>How do I print the whole canvas in 1 page?

    You can use ScaleTransform to shrink the content in Canvas, but you need to calculate the scale based on the content and viewable area.

    <Canvas x:Name="MainCanvas">
                <Canvas.RenderTransform>
                    <ScaleTransform ScaleX="1.0" ScaleY="1.0"/>
                </Canvas.RenderTransform>
                <Rectangle Width="500" Height="500" Fill="Red" />
                <Rectangle Canvas.Top="600" Width="500" Height="500" Fill="Blue" />
            </Canvas>

    (MainCanvas.RenderTransform as ScaleTransform).ScaleX = 0.5;
    (MainCanvas.RenderTransform as ScaleTransform).ScaleY = 0.5;


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 15, 2015 9:43 AM