打印ScrollViewer内容

phm*_*phm 5 printing wpf

我有以下内容:带有scrollviewer和Print按钮的wpf窗口.

我试图使用PrintDialog打印scrollviewer的内容,但它只适用于xps.如果我选择我的打印机或文档编写器,那么最终结果很糟糕(半页边距,控件剪切等).如何在不调整/缩放滚动查看器内容的情况下解决此问题?

Ste*_*bob 5

为了在 WPF 中进行体面(且相对容易)的打印,您应该使用 FlowDocumentScrollViewer 而不是 ScrollViewer。在 FlowDocumentScrollViewer 内,您可以放置​​一个 FlowDocument,其中将包含您要打印的内容。

示例 XAML:

<FlowDocumentScrollViewer>
    <FlowDocument PagePadding="48">
        <Section>
            <Paragraph>
                <Run Text="sample"/>
            </Paragraph>
        </Section>
        <Section>
            <BlockUIContainer>
                <my:myUserControl/>
            </BlockUIContainer>
        </Section>
    </FlowDocument>
</FlowDocumentScrollViewer>
Run Code Online (Sandbox Code Playgroud)

'BlockUIContainer' 对象非常适合用于保存可以包含您需要的任何内容的用户控件。FlowDocument 的“PagePadding”属性设置边距。48 相当于 1/2 英寸。(96 dpi)。

示例打印代码:

Dim pd As New PrintDialog
If pd.ShowDialog Then

    Dim fd As FlowDocument = docOutput

    Dim pg As DocumentPaginator = CType(fd, IDocumentPaginatorSource).DocumentPaginator

    pd.PrintDocument(pg, "my document")

End If
Run Code Online (Sandbox Code Playgroud)