如何在一个文档中打印多个 WPF 页面 [英] How to print multiple WPF pages in one document

查看:58
本文介绍了如何在一个文档中打印多个 WPF 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含多页的文档,并且每页都描绘一个 wpf 窗口.

I want to create a document with several pages and with one wpf window depicted on each page.

我设法使用 PrintDialog 和 PrintVisual 打印了一个窗口.然而,这似乎只适用于一页?任何想法如何构建包含多页的文档并打印完整的文档集.

I managed to print one window using PrintDialog and PrintVisual. However this seem to only work with one single page? Any ideas how I can build a document with several pages and print the complete set.

我可以插入该视觉对象(在代码中引用)并将其作为页面插入文档中,然后打印出来吗?

Can I insert that visual (referred in code) and insert it as a page in a document and print it after that?

方法真的很糟糕吗?有没有其他方法可以解决这个问题?

Is approach really bad? Is there som other way to solve this problem?

      Sub Print (Dim ele As FrameWorkElement)
            Dim margin As Double = 30
            Dim titlePadding As Double = 10

            Dim printDlg As PrintDialog = New PrintDialog()
            printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape
            If (printDlg.ShowDialog() <> True) Then Return

            Dim formattedText As FormattedText = New FormattedText(Name, CultureInfo.GetCultureInfo("en-us"),
                                                            FlowDirection.LeftToRight, New Typeface("Verdana"), 25, Brushes.Black)

            formattedText.MaxTextWidth = printDlg.PrintableAreaWidth

            Dim scale As Double = Math.Min(printDlg.PrintableAreaWidth / (ele.ActualWidth + (Margin * 2)),
                                    (printDlg.PrintableAreaHeight - (formattedText.Height + titlePadding)) / (ele.ActualHeight + (Margin * 2)))

            Dim visual As DrawingVisual = New DrawingVisual()
            Using context As DrawingContext = visual.RenderOpen()

                Dim brush As VisualBrush = New VisualBrush(ele)
                context.DrawRectangle(brush, Nothing, New Rect(New Point(margin, margin + formattedText.Height + titlePadding),
                                                            New Size(ele.ActualWidth, ele.ActualHeight)))

                context.DrawText(formattedText, New Point(margin, margin))
            End Using
            visual.Transform = New ScaleTransform(scale, scale)
            printDlg.PrintVisual(visual, "")
     End Sub

推荐答案

您想要创建一个带有多个 FixedPagesFixedDocument.

You want to create a FixedDocument with multiple FixedPages.

示例:

如何新建文档和单页.

var pageSize = new Size(8.26 * 96, 11.69 * 96); // A4 page, at 96 dpi
var document = new FixedDocument();
document.DocumentPaginator.PageSize = pageSize;

// Create FixedPage
var fixedPage = new FixedPage();
fixedPage.Width = pageSize.Width;
fixedPage.Height = pageSize.Height;
// Add visual, measure/arrange page.
fixedPage.Children.Add((UIElement)visual);
fixedPage.Measure(pageSize);
fixedPage.Arrange(new Rect(new Point(), pageSize));
fixedPage.UpdateLayout();

// Add page to document
var pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
document.Pages.Add(pageContent);

// Send to the printer.
var pd = new PrintDialog();
pd.PrintDocument(document.DocumentPaginator, "My Document");

用 C# 编写,但是您应该能够转换为 VB.

Written in C#, however you should be able to convert to VB.

HTH,

这篇关于如何在一个文档中打印多个 WPF 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆