在UWP中打印

时间:2017-12-11 09:19:22

标签: c# xaml printing uwp

我使用PrintManager类在UWP应用程序中进行打印。我想在行和列中打印数据。我在后面的代码中创建了一个网格控件,并尝试将该网格传递给SetPreviewPage()并将其设置为Preview Page。但是当数据变得太多并且它没有被渲染到下一页时我会遇到问题。

我还尝试在RichTextBlock上呈现此数据,但我无法在行和列中排列数据并显示边框。

无论如何,我可以使用网格控件在多个打印预览页面上呈现数据。

以下是创建Grid

的代码
 public Grid FormatData()
    {
        CreateLayoutGrid(PickLists);
        PopulateData();

        ParentGrid.Children.Add(ChildGrid);
        Grid.SetColumn(ChildGrid, 1);
        Grid.SetRow(ChildGrid, 3);
        return ParentGrid;
    }


    private void PopulateData()
    {
        var row = 0;
        var column = 0;

        CreatePageHeader();

        foreach (var item in items)
        {
            CreateCells(item , ref row, column);
            column = 0;
            row++;
        }
    }

    private void CreatePageHeader()
    {
       ------

        Grid.SetColumnSpan(pspsServices, 3);
        Grid.SetRow(pspsServices, 0);

        Grid.SetColumn(control1, 0);
        Grid.SetRow(control1, 1);

        Grid.SetColumn(control2, 2);
        Grid.SetRow(control2, 1);

        ParentGrid.Children.Add(control1);
        ParentGrid.Children.Add(control2);
    }

    private void CreateCells(model items, ref int row, int column)
    {
        var top = row == 0 ? 1D : 0D;

        var descriptionBorder = new Border
        {
            BorderThickness = new Thickness(1, top, 1, 1),
            BorderBrush = new SolidColorBrush(Colors.Black)
        };

        var quantityBorder = new Border
        {
            BorderThickness = new Thickness(0, top, 1, 1),
            BorderBrush = new SolidColorBrush(Colors.Black)
        };

        var emptyBorder = new Border
        {
            BorderThickness = new Thickness(0, top, 1, 1),
            BorderBrush = new SolidColorBrush(Colors.Black),
            Width = 100
        };

        var description = (row % 30) == 0 ? CreateBoldTextBlock(Constants.Description)
            : CreateMediumTextBlock(pickList.Description);

        var quantity = (row % 30) == 0 ? CreateBoldTextBlock(Constants.Quantity)
            : CreateMediumTextBlock(pickList.QuantityOrdered.ToString());

        descriptionBorder.Child = description;
        quantityBorder.Child = quantity;

        ChildGrid.Children.Add(descriptionBorder);
        ChildGrid.Children.Add(quantityBorder);
        ChildGrid.Children.Add(emptyBorder);

        Grid.SetRow(descriptionBorder, row);
        Grid.SetRow(quantityBorder, row);
        Grid.SetRow(emptyBorder, row);

        Grid.SetColumn(descriptionBorder, column++);
        Grid.SetColumn(quantityBorder, column++);
        Grid.SetColumn(emptyBorder, column);
    }

    private void CreateLayoutGrid(List<PickListItemsModel> pickLists)
    {
        var firstColumn = new ColumnDefinition
        {
            Width = new GridLength(1, GridUnitType.Star)
        };

        var secondColumn = new ColumnDefinition
        {
            Width = GridLength.Auto
        };

        var thirdColumn = new ColumnDefinition
        {
            Width = new GridLength(1, GridUnitType.Star)
        };


        ParentGrid.ColumnDefinitions.Add(firstColumn);
        ParentGrid.ColumnDefinitions.Add(secondColumn);
        ParentGrid.ColumnDefinitions.Add(thirdColumn);

        ParentGrid.RowDefinitions.Add(new RowDefinition
        {
            Height = GridLength.Auto
        });

        ParentGrid.RowDefinitions.Add(new RowDefinition
        {
            Height = GridLength.Auto
        });

        ParentGrid.RowDefinitions.Add(new RowDefinition
        {
            Height = GridLength.Auto
        });

        ParentGrid.RowDefinitions.Add(new RowDefinition());

        CreateColumnsInChild();

        foreach (var pickList in pickLists)
        {
            ChildGrid.RowDefinitions.Add(new RowDefinition());
        }
    }

    private void CreateColumnsInChild()
    {
        ChildGrid.ColumnDefinitions.Add(new ColumnDefinition
        {
            Width = new GridLength(3, GridUnitType.Star)
        }); 

        ChildGrid.ColumnDefinitions.Add(new ColumnDefinition());

        ChildGrid.ColumnDefinitions.Add(new ColumnDefinition());
    }

    private TextBlock CreateBoldTextBlock(string text)
    {
        return new TextBlock
        {
            FontSize = 14,
            Foreground = new SolidColorBrush(Colors.Black),
            FontWeight = FontWeights.Bold,
            Text = text,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Padding = new Thickness(10, 5, 10, 5)
        };
    }

    private TextBlock CreateMediumTextBlock(string text)
    {
        return new TextBlock
        {
            FontSize = 14,
            Foreground = new SolidColorBrush(Colors.Black),
            FontWeight = FontWeights.Medium,
            Text = text,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Padding = new Thickness(10, 5, 10, 5)
        };
    }

Here is the print format

1 个答案:

答案 0 :(得分:0)

查看官方Printing示例并查看PrintHelper课程,您将了解如何添加要从AddOnePrintPreviewPage方法显示的新页面。

与此问题类似,您可以尝试使用自己的网格页面替换示例中的PageToPrint页面。

<强>更新

您可以尝试使用RenderTargetBitmap类将网格渲染为图像,然后将打印样本Scenario5Photos打印在不同页面中的图像。