WPF FlowDocument缩放到适合页面

时间:2009-08-12 15:46:32

标签: wpf itemscontrol scale flowdocument

由于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");
    }

1 个答案:

答案 0 :(得分:3)

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

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

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;
        }
    }