【问题标题】:Printing ScrollViewer contents打印 ScrollViewer 内容
【发布时间】:2012-01-02 16:07:55
【问题描述】:

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

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

【问题讨论】:

    标签: wpf printing


    【解决方案1】:

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

    【讨论】:

    • 感谢您的想法。代码生成并打印第一页,但内容仍被剪切。我是否必须调整一些边距或 w/h?
    • @phm - 您能否发布一些屏幕截图和/或代码,以便我们确切了解问题所在?
    【解决方案2】:

    FlowDocument 可能是动态内容和动态打印大小的更好解决方案,即未知或可能会改变。对于我的问题,我知道内容和打印尺寸。

    我做的第一件事是将 ScrollViewer(在我的例子中是一个网格)中的内容设置为 A4 大小,这可以轻松完成

    <Grid x:Name="gridReport" Height="29.7cm" Width="21cm">
    

    这意味着网格精确映射到打印区域,网格内的任何内容在打印时都不应失真。

    如果在使用 PrintDialog 时 ScrollViewer 没有滚动到顶部,这仍然会切掉顶部区域。要解决此问题,请在打印之前以编程方式滚动到顶部

    Myscrollviewer.ScrollToTop();
    
    PrintDialog printDialog = new PrintDialog();
    if(printDialog.ShowDialog() == true)
    {
        printDialog.PrintVisual(gridReport, "Print Report");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-02
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2011-01-16
      • 2011-04-26
      相关资源
      最近更新 更多