WPF FlowDocument 缩放以适合页面

发布于 2024-07-30 09:34:16 字数 618 浏览 9 评论 0原文

由于 BlockUIContainer 中的 ItemsControl,我有一个 FlowDocument 的高度有所不同。 在某些情况下,ItemsControl 会超出页面高度。 如果需要的话,有没有办法在打印之前缩放 FlowDocument 以适合页面 (8.5" X 11")?

截至目前,FlowDocument 名为“doc”,我使用的打印方法是:

private void Print_Click(object sender, RoutedEventArgs e)
    {

        PrintDialog pd = new PrintDialog();
        doc.PageHeight = pd.PrintableAreaHeight;
        doc.PageWidth = pd.PrintableAreaWidth;
        doc.ColumnGap = 0;
        doc.ColumnWidth = pd.PrintableAreaWidth;
        IDocumentPaginatorSource dps = doc;
        pd.PrintDocument(dps.DocumentPaginator, "Sheet");
    }

I have a FlowDocument that varies in height due to an ItemsControl in a BlockUIContainer. In some cases, the ItemsControl extends beyond the page height. Is there a way to scale the FlowDocument to fit a page (8.5" X 11") right before printing if needed?

As of right now, the FlowDocument is named 'doc' and the method for printing I am using is:

private void Print_Click(object sender, RoutedEventArgs e)
    {

        PrintDialog pd = new PrintDialog();
        doc.PageHeight = pd.PrintableAreaHeight;
        doc.PageWidth = pd.PrintableAreaWidth;
        doc.ColumnGap = 0;
        doc.ColumnWidth = pd.PrintableAreaWidth;
        IDocumentPaginatorSource dps = doc;
        pd.PrintDocument(dps.DocumentPaginator, "Sheet");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2024-08-06 09:34:16

我知道有点晚了,但这是我想出的解决方案。

首先,我们创建一个包装器,它将为我们生成文档页面。 每个页面在返回之前都会应用比例转换。

public class FittedDocumentPaginator : DocumentPaginator
{
    public DocumentPaginator Base { get; private set; }
    public double Scale { get; private set; }
    private readonly ScaleTransform _sTransform;

    public FittedDocumentPaginator( DocumentPaginator baseDp, double scale )
    {
        if ( baseDp == null )
            throw new ArgumentNullException( "baseDp" );

        Base = baseDp;
        Scale = scale;
        _sTransform = new ScaleTransform( scale, scale );
    }

    public override DocumentPage GetPage( int pageNumber )
    {
        var page = Base.GetPage( pageNumber );
        ( (ContainerVisual)page.Visual ).Transform = _sTransform;

        return page;
    }

    public override bool IsPageCountValid
    {
        get { return Base.IsPageCountValid; }
    }

    public override int PageCount
    {
        get { return Base.PageCount; }
    }

    public override Size PageSize
    {
        get { return Base.PageSize; }
        set { Base.PageSize = value; }
    }

    public override IDocumentPaginatorSource Source
    {
        get { return Base.Source; }
    }
}

使用它相当简单:

    private void PrintDocument( PrintDialog pd, FlowDocument document, double scale, string title )
    {
        DocumentPaginator dp = ( (IDocumentPaginatorSource)document ).DocumentPaginator;
        FittedDocumentPaginator fdp = new FittedDocumentPaginator( dp, scale );

        pd.PrintDocument( fdp, title );
    }

如果您有兴趣,以下是我们确定比例的方法。 在我们的例子中,文档延伸超过了页面宽度,但可以轻松修改以适应页面高度。

    private void Print( FlowDocument document, string title, double documentWidth )
    {
        var pd = new PrintDialog();

        if ( pd.ShowDialog() == true )
        {
            // Set the printing margins.
            Thickness pageMargins = document.PagePadding;
            document.PagePadding = new Thickness( 15.0 );

            // In our case, the document's width is NaN so we have to navigate the visual tree to get the ActualWidth, which is represented by 'documentWidth'.
            double scale = documentWidth / pd.PrintableAreaWidth;

            if ( scale < 1.0 )
            {
                // The report fits on the page just fine. Don't scale.
                scale = 1.0;
            }

            double invScale = 1 / scale;

            document.PageHeight = pd.PrintableAreaHeight * scale;
            document.PageWidth = pd.PrintableAreaWidth * scale;

            DocumentPaginator dp = ( (IDocumentPaginatorSource)document ).DocumentPaginator;
            FittedDocumentPaginator fdp = new FittedDocumentPaginator( dp, invScale );

            pd.PrintDocument( fdp, title );

            // Restore the original values so the GUI isn't altered.
            document.PageHeight = Double.NaN;
            document.PageWidth = Double.NaN;
            document.PagePadding = pageMargins;
        }
    }

I know it's a bit late, but here is the solution I came up with.

First, we create a wrapper that will generate the document pages for us. Each page will have a scale transformation applied to it before returning it.

public class FittedDocumentPaginator : DocumentPaginator
{
    public DocumentPaginator Base { get; private set; }
    public double Scale { get; private set; }
    private readonly ScaleTransform _sTransform;

    public FittedDocumentPaginator( DocumentPaginator baseDp, double scale )
    {
        if ( baseDp == null )
            throw new ArgumentNullException( "baseDp" );

        Base = baseDp;
        Scale = scale;
        _sTransform = new ScaleTransform( scale, scale );
    }

    public override DocumentPage GetPage( int pageNumber )
    {
        var page = Base.GetPage( pageNumber );
        ( (ContainerVisual)page.Visual ).Transform = _sTransform;

        return page;
    }

    public override bool IsPageCountValid
    {
        get { return Base.IsPageCountValid; }
    }

    public override int PageCount
    {
        get { return Base.PageCount; }
    }

    public override Size PageSize
    {
        get { return Base.PageSize; }
        set { Base.PageSize = value; }
    }

    public override IDocumentPaginatorSource Source
    {
        get { return Base.Source; }
    }
}

Using it is fairly simple:

    private void PrintDocument( PrintDialog pd, FlowDocument document, double scale, string title )
    {
        DocumentPaginator dp = ( (IDocumentPaginatorSource)document ).DocumentPaginator;
        FittedDocumentPaginator fdp = new FittedDocumentPaginator( dp, scale );

        pd.PrintDocument( fdp, title );
    }

If you're interested, here is how we determined the scale. In our case, the document was extended past the page width, but it can easily be modified to accommodate the page height.

    private void Print( FlowDocument document, string title, double documentWidth )
    {
        var pd = new PrintDialog();

        if ( pd.ShowDialog() == true )
        {
            // Set the printing margins.
            Thickness pageMargins = document.PagePadding;
            document.PagePadding = new Thickness( 15.0 );

            // In our case, the document's width is NaN so we have to navigate the visual tree to get the ActualWidth, which is represented by 'documentWidth'.
            double scale = documentWidth / pd.PrintableAreaWidth;

            if ( scale < 1.0 )
            {
                // The report fits on the page just fine. Don't scale.
                scale = 1.0;
            }

            double invScale = 1 / scale;

            document.PageHeight = pd.PrintableAreaHeight * scale;
            document.PageWidth = pd.PrintableAreaWidth * scale;

            DocumentPaginator dp = ( (IDocumentPaginatorSource)document ).DocumentPaginator;
            FittedDocumentPaginator fdp = new FittedDocumentPaginator( dp, invScale );

            pd.PrintDocument( fdp, title );

            // Restore the original values so the GUI isn't altered.
            document.PageHeight = Double.NaN;
            document.PageWidth = Double.NaN;
            document.PagePadding = pageMargins;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文