wpf - 打印 ScrollViewer 内容

标签 wpf printing

我有以下几点:
带有滚动查看器和打印按钮的 wpf 窗口。

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

最佳答案

为了在 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>

'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

关于wpf - 打印 ScrollViewer 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8702924/

相关文章:

c# - 用 C# 编写的带有点击移动的 WPF 游戏

c# - 使用 MVVM 在代码中动态设置资源

WPF:旋转后获取新坐标

sql - Sybase - 设置控制台输出以打印日志语句

php - Magento - 在后端打印发票时空白/空白页

c# - 如何将作为参数传递的 KeyValuePair 转换为 Command_Executed?

c# - 设计具有可扩展 TextBlock 的 WPF 控件

html - 打印时 IE10 边框问题 w/Rowspan

c# - PrintSystemJobInfo.JobStream 坏了吗?

java - 数组仅打印某些元素而不打印其他元素