Windowscommunitytoolkit: Page break using Print Helper extension of windows-community-toolkit

Created on 12 Jul 2020  路  8Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

I'm using PrintHelper extension of windows-community-toolkit and printing some data. The problem is when data is large it overlaps the page and i can't find a way to dynamically add a new page and shift data to next page. Is there a way ?

public PrintHelper PrintHelper;

public void StartPrint(Panel container, List<CompanyDetail> PrintSampleItems)
{
    try
    {
        PrintHelper = new PrintHelper(container);
        PrintHelper.OnPrintSucceeded += PrintHelper_OnPrintSucceeded;
        PrintHelper.OnPrintFailed += PrintHelper_OnPrintFailed;
        PrintHelper.OnPreviewPagesCreated += PrintHelperOnOnPreviewPagesCreated;
        var pageNumber = 0;

        var data = PrintSampleItems;

        var grid = new Grid();
        grid.RowDefinitions.Add(new RowDefinition() {Height = GridLength.Auto});
        grid.RowDefinitions.Add(new RowDefinition() {Height = new GridLength(1, GridUnitType.Star)});
        grid.RowDefinitions.Add(new RowDefinition() {Height = GridLength.Auto});

        // Static header
        var header = new TextBlock
        {
            Text = "Departments Print",
            Margin = new Thickness(0, 0, 0, 20),
            HorizontalAlignment = HorizontalAlignment.Center,
            FontSize = 18,
            FontWeight = FontWeights.Bold
        };
        Grid.SetRow(header, 0);
        grid.Children.Add(header);

        var dataGrid = new CompanyDetailReportTemplateControl
        {
            HorizontalAlignment = HorizontalAlignment.Center
        };
        dataGrid.SetValues(PrintSampleItems);

        Grid.SetRow(dataGrid, 1);
        grid.Children.Add(dataGrid);


        // Footer with page number
        pageNumber++;
        var footer = new TextBlock
        {
            Text = $"Page [ {pageNumber} ]", Margin = new Thickness(0, 20, 0, 0),
            HorizontalAlignment = HorizontalAlignment.Right
        };
        Grid.SetRow(footer, 2);
        grid.Children.Add(footer);


        PrintHelper.AddFrameworkElementToPrint(grid);

    }
    catch
    {
    }
}

The result looks like this. As can be seen if data is large it is not printed because next page is not added automatically. I can do that be manually looping data and defining exactly how many rows to print on one page and how many on next but that's not what i want cuz if some cell data is large then my logic won't work.

Print image

feature request help wanted helpers question

All 8 comments

Hello RaoHammas, thank you for your interest in Windows Community Toolkit!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible.. Other community members may also answer the question and provide feedback 馃檶

Thanks for highlighting this and it's a great question. Let's see if our devs are aware of this and if there is any option we have to automatically adjust a large amount of data.

@RaoHammas by default the control breaks pages by child framework element, so the grid itself is being only added to a single page I believe. I think the original intent was that the developer would still be in charge of creating pages for more complex scenarios. See the example in our docs for "Print a list with each item on a separate page with static header and page number".

I'm not sure how easy/possible it'd be for the helper to have more helpers for this type of scenario, I'm not too familiar with this code. Wondering if @avknaidu, @deanchalk, and @XamlBrewer have any thoughts/ideas here?

@RaoHammas by default the control breaks pages by child framework element, so the grid itself is being only added to a single page I believe. I think the original intent was that the developer would still be in charge of creating pages for more complex scenarios. See the example in our docs for "Print a list with each item on a separate page with static header and page number".

I'm not sure how easy/possible it'd be for the helper to have more helpers for this type of scenario, I'm not too familiar with this code. Wondering if @avknaidu, @deanchalk, and @XamlBrewer have any thoughts/ideas here?

I know. That's why example includes looping. So on each iteration a new framework element is added and that takes one page.

But this logic is incomplete and false. As i mentioned as user have no control on it because if my grid rows are more in number ? And let's say i decide that I'll print only 12 rows on each page ..still if a cell element is large ..in that case logic will break because row will take extra space and some data will not be printed.

@RaoHammas the logic you are stating makes sense. Let's see what the team mentioned above have to say regarding the actual code.

Not sure the Print helper can do this, but you need your main control to be a RichTextBlock and generate Paragraphs for the items. More details here: https://xamlbrewer.wordpress.com/2016/10/25/a-recipe-for-printing-in-uwp-mvvm-apps/ .

It seems to me that this issue could be because your data control (CompanyDetailReportTemplateControl) is virtualised, or inside a ScrollViewer. You have the first row control (header) in the print and the third row control (footer) in the print, but the central datagrid is sized to the remaining space - but it there's not enough so maybe its virtualising or clipping ? which is the correct behaviour in a view, but maybe you need to switch of virtualising so the data control will span pages for printing. It means you'll loose the footer (it will not work) but you may get the data printed the way you want.
I might have this wrong, but it's something I would try

@deanchalk no sir, it's not because of virtualization. i think there is no automatic page adding mechanism. So that if data is large and need to overflow page then it should automatically add a new page and shift remaining data to that page. But it doesn't happen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ilinkmobility picture ilinkmobility  路  4Comments

karmaecrivain94 picture karmaecrivain94  路  3Comments

kusanagi2k2 picture kusanagi2k2  路  4Comments

Joebeazelman picture Joebeazelman  路  3Comments

ranjeshj picture ranjeshj  路  3Comments