3

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

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

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

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

      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
4

1 回答 1

12

您想创建一个FixedDocument具有多个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。

高温下,

于 2012-04-13T10:21:46.273 回答