locked
How to print Date RRS feed

  • Question

  • I have two Pages used for printing.

    1) PagePrinting.xaml
    2) FormattedPage.xaml


    in PagePrinting.xaml , I used the code below to reference the FormattedPage.xaml


     FrameworkElement page1;
     page1 = new FormattedPage();
     CanvasPrintContainer.Children.Add(page1);


    The problem:

     - The txtBlkDatePrint is blank when it is shown in PagePrinting.xaml

     How to solve it?

    in CodeBehind of FormattedPage.xaml :

     protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                txtBlkDatePrint.Text = DateTime.Today.ToString("d");
            }


    FormattedPage.xaml:

    <StackPanel x:Name="header" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="0" Height="60">
           <StackPanel Orientation="Horizontal" >
               <RichTextBlock Foreground="Black" FontSize="20" TextAlignment="Left" FontFamily="Segoe UI">
             <Paragraph>
       <TextBlock x:Name="txtBlkDatePrint" Margin="10,10,0,0" HorizontalAlignment="Left"                 TextWrapping="Wrap" FontSize="28" Text="" VerticalAlignment="Top" Height="39" Width="204"/>
            </Paragraph>
             </RichTextBlock>
           </StackPanel>
    </StackPanel>

    Monday, November 18, 2013 8:08 AM

Answers

  • I don't think the OnNavigatedTo of your FormattedPage is executed.

    You might want to check Quickstart: Printing from your app. In the RegisterForPrinting() method you'll see the next statement:

    printDocument.Paginate += CreatePrintPreviewPages;

    It's the CreatePreviewPages event handler that creates the print pages. Any code to assign or bind values should go there (or before the call).
    • Marked as answer by FireDance Thursday, November 21, 2013 1:33 AM
    Monday, November 18, 2013 8:40 AM

All replies

  • I don't think the OnNavigatedTo of your FormattedPage is executed.

    You might want to check Quickstart: Printing from your app. In the RegisterForPrinting() method you'll see the next statement:

    printDocument.Paginate += CreatePrintPreviewPages;

    It's the CreatePreviewPages event handler that creates the print pages. Any code to assign or bind values should go there (or before the call).
    • Marked as answer by FireDance Thursday, November 21, 2013 1:33 AM
    Monday, November 18, 2013 8:40 AM
  • I am new to printing.

    1) In PagePrintting.xaml  , I have the following:

    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                 document = new PrintDocument();
                source = document.DocumentSource;

                document.Paginate += printDocument_Paginate;
                document.GetPreviewPage += printDocument_GetPreviewPage;
                document.AddPages += printDocument_AddPages;


                PrintManager manager = PrintManager.GetForCurrentView();
                manager.PrintTaskRequested += manager_PrintTaskRequested;


                pages = new List<UIElement>();

          }

     

    2) Here the code which is the same as  printDocument.Paginate += CreatePrintPreviewPages;

    void printDocument_Paginate(object sender, PaginateEventArgs e)
            {
                pages.Clear();

                CanvasPrintContainer.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);


            }

    2) I have this code for FormattedPage.xaml's CodeBehind.

     protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                txtBlkDatePrint.Text = DateTime.Today.ToString("d");
            }

    <StackPanel x:Name="header" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="0" Height="60">
                <StackPanel Orientation="Horizontal" >
                    <RichTextBlock Foreground="Black" FontSize="20" TextAlignment="Left" FontFamily="Segoe UI">
                        <Paragraph>
                            <InlineUIContainer>
                            <TextBlock x:Name="txtBlkDatePrint" Margin="10,70,0,0" HorizontalAlignment="Left" TextWrapping="Wrap" FontSize="28" Foreground="Black" Text="" VerticalAlignment="Top" Height="39" Width="204"/>
                            </InlineUIContainer>
                        </Paragraph>
                    </RichTextBlock>
                </StackPanel>
            </StackPanel>

     

    Thanks

    Monday, November 18, 2013 8:58 AM
  • The print (preview) pages are created in the AddOnePrintPreviewPage call. The content of your FormattedPage.xaml is embedded in the print document. So your OnNavigatedTo is never executed. You can try assigning values in the constructor of FormattedPage, or in AddOnePrintPreviewPage.
    Monday, November 18, 2013 9:50 AM
  • I have added but still no show :

    txtBlkCompany is in FormattedPage.

     

     private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription)
            {
                FrameworkElement page;
                RichTextBlockOverflow link;

                if (lastRTBOAdded == null)
                {
                    page = page1;
                    StackPanel footer = (StackPanel)page.FindName("footer");
                    footer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                    TextBlock txtBlkCpy = (TextBlock)page.FindName("txtBlkCompany");

                    txtBlkCpy.Text = "Abc";

                }
                else
                {
                    page = new PrintContinuationPage(lastRTBOAdded);
                }


                page.Width = printPageDescription.PageSize.Width;
                page.Height = printPageDescription.PageSize.Height;


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


                double marginWidth = Math.Max(printPageDescription.PageSize.Width - printPageDescription.ImageableRect.Width, printPageDescription.PageSize.Width * left * 2);
                double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * top * 2);


                printableArea.Width = page1.Width - marginWidth;
                printableArea.Height = page1.Height - marginHeight;

                CanvasPrintContainer.Children.Add(page);
                CanvasPrintContainer.InvalidateMeasure();
                CanvasPrintContainer.UpdateLayout();


                // Find the last text container and see if the content is overflowing
                link = (RichTextBlockOverflow)page.FindName("continuationPageLinkedContainer");


                // Check if this is the last page
                if (!link.HasOverflowContent && link.Visibility == Windows.UI.Xaml.Visibility.Visible)
                {
                    StackPanel footer = (StackPanel)page.FindName("footer");
                    footer.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }


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


                return link;
            }

    Tuesday, November 19, 2013 2:37 AM
  • At first sight this code should work. Did you try adding this to the page, or to the tekst blocks? Maybe you're successfully printing ... in white:

    Foreground="Black"

    Tuesday, November 19, 2013 8:16 AM
  • Thanks. OnNavigatedTo is never executed.

    I don't know what is the reason why FormattedPage's Events are not executed like OnNavigatedTo.

    I pass the date to the Constructor.

    Thanks

    Thursday, November 21, 2013 1:36 AM