繁体   English   中英

在WPF中每张纸上打印具有多个FixedPage的FixedDocument

[英]Print FixedDocument with multiple FixedPage in pages per sheet in WPF

我有一个非常简单的WPF应用程序,上面只有一个按钮。

<Button x:Name="btnPrintCard" Grid.Row="2" HorizontalAlignment="Center" Content="Print Card" MinWidth="140" Foreground="White"
                Cursor="Hand" Background="#008080" Click="btnPrintCard_Click" />

我正在尝试在“典型A4”页面上打印多张尺寸为3.370 x 2.125的卡片。 如果布置正确,则从左到右的顺序应该每张可容纳10张卡片。 就像adobe reader的打印命令一样,并将每张纸的自定义页面设置为2 x 5。

Abode Reader打印设置

我正在使用以下代码生成和打印卡:

private void btnPrintCard_Click(object sender, RoutedEventArgs e)
{
    try
    {
        PrintDialog printDialog = new PrintDialog();
        bool? pdResult = printDialog.ShowDialog();
        if (pdResult != null && pdResult.Value)
        {
            FixedDocument document = CreateFixedDocument();
            printDialog.PrintDocument(document.DocumentPaginator, "ID Card Printing");
        }
        MessageBox.Show("Printing done.");
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message + " :: " + ex.InnerException);
    }
}

private FixedDocument CreateFixedDocument()
{
    FixedDocument fixedDocument = new FixedDocument();
    // fixedDocument.DocumentPaginator.PageSize = new Size(96 * 3.370, 96 *2.125);
    fixedDocument.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);

    for (int i = 0; i < 10; i++)
    {
        PageContent page = new PageContent();
        FixedPage fixedPage = CreateOneFixedPage();
        ((IAddChild)page).AddChild(fixedPage);
        fixedDocument.Pages.Add(page);
    }
    return fixedDocument;
}

private FixedPage CreateOneFixedPage()
{
    FixedPage page = new FixedPage();
    page.Background = Brushes.Red;
    page.Width = 96 * 3.370;
    page.Height = 96 * 2.125;

    TextBlock tbTitle = new TextBlock();
    tbTitle.Text = "xxx xxxxx Public School";
    tbTitle.FontSize = 24;
    tbTitle.Foreground = new SolidColorBrush(Colors.White);
    tbTitle.FontFamily = new FontFamily("Arial");
    FixedPage.SetLeft(tbTitle, 96 * 0.4); // left margin
    FixedPage.SetTop(tbTitle, 96 * 0.04); // top margin
    page.Children.Add((UIElement)tbTitle);

    Image image = new Image
    {
        Source = new BitmapImage(new Uri("http://www.ready-range.co.uk/_assets/images/products/BHSRR40R0R.jpg")),
        Height = 30,
        Width = 30
    };

    Border b = new Border();
    b.BorderThickness = new Thickness(1);
    b.BorderBrush = Brushes.Yellow;
    b.Child = image;

    FixedPage.SetLeft(b, 96 * 0.3);
    FixedPage.SetTop(b, 96 * 0.6); // top margin
    page.Children.Add((UIElement)b);

    //measure size of the layout
    Size sz = new Size(96 * 3.370, 96 * 2.125);
    page.Measure(sz);
    page.Arrange(new Rect(new Point(), sz));
    page.UpdateLayout();

    return page;
}

这样可以成功打印,但是每页每张卡都这样打印:

打印的图像

问题是我想像上面的第一张图像一样打印,即每张纸自定义2 x 5。

非常感谢。

另一个stackoverflow问题的答案很有帮助。 为了获得期望的结果,我进行了一些更改,这是在WpfPrint类中添加了自己的方法。

/// <summary>
/// Add fixed page to current fixed document
/// </summary>
/// <param name="card"></param>
/// <param name="flags"></param>
public void AddFixedPage(FixedPage card, ElementFlags flags)
{
    card.Measure(_infiniteSize);
    if (CurX > _fixedDocument.DocumentPaginator.PageSize.Width - MarginX)
    {
        CurY += card.DesiredSize.Height + MarginY;
        CurX = MarginX;
    }
    double extraCheck = 0;
    if ((flags & ElementFlags.BottomCheck2) == ElementFlags.BottomCheck2)
        extraCheck = card.DesiredSize.Height;
    if (CurY > _fixedDocument.DocumentPaginator.PageSize.Height - MarginY - extraCheck)
        StartPage();


    _curCanvas.Children.Add(card);
    card.SetValue(Canvas.LeftProperty, CurX);
    card.SetValue(Canvas.TopProperty, CurY);


    CurX += card.DesiredSize.Width + MarginX; //Added margin x for proper display             

    if (((flags & ElementFlags.NewLine) == ElementFlags.NewLine)  || CurX + card.DesiredSize.Width > _fixedDocument.DocumentPaginator.PageSize.Width) 
    {
        CurX = MarginX;
        CurY += card.DesiredSize.Height + MarginY;
    }
}

现在,我们可以像这样简单地做到这一点:

WpfPrint printer = new WpfPrint(new Size(96 * 9, 96 * 11));
printer.CurrentElementMargin = new Thickness(4);
printer.CurrentFontFamily = new FontFamily("Arial");
printer.MarginX = 20;
printer.MarginY = 10;

现在我们可以遍历每个FixedPage:

 for (int i = 0; i < 10; i++)
    printer.AddFixedPage(CreateOneFixedPage(), WpfPrint.ElementFlags.BottomCheck2);

最后,发送打印命令,如下所示:

PrintDialog printDialog = new PrintDialog();
bool? pdResult = printDialog.ShowDialog();
if (pdResult != null && pdResult.Value)                              
    printDialog.PrintDocument(printer.CurrentFixedDocument.DocumentPaginator, "Card Printing");

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM