c# - WPF 如何将 FlowDocument BGImage 转换为 FixedDocument 并在 DocumentViewer 中显示?

标签 c# wpf

我有一个带有背景图像的 FlowDocument。目前它不能在 FlowDocumentReader 中正确显示,因为当文档上下滚动时背景图像保持居中。如何将这个FlowDocument转为FixedDocument,在DocumentViewer中显示,背景图也固定了?

我使用来自 here 的转换逻辑.但它不显示 FlowDocument.Background 图像。

    private FixedDocument convert(FlowDocument flowDocument)
    {
        if (flowDocument == null)
            return null;
        var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
        var package = Package.Open(new MemoryStream(), FileMode.Create, FileAccess.ReadWrite);
        var packUri = new Uri("pack://temp.xps");
        PackageStore.RemovePackage(packUri);
        PackageStore.AddPackage(packUri, package);
        var xps = new XpsDocument(package, CompressionOption.NotCompressed, packUri.ToString());
        XpsDocument.CreateXpsDocumentWriter(xps).Write(paginator);
        FixedDocument doc = xps.GetFixedDocumentSequence().References[0].GetDocument(true);
        return doc;
    }

最佳答案

你写的

I have a FlowDocument with Background image. Currently it does not display properly in FlowDocumentReader since the background image stays centered when document is scrolled up and down.

不完全是您特定问题的答案,但要避免这种情况,请设置 ImageBrush.ViewportUnitsBrushMappingMode.Absolute .然后,设置 ImageBrush.Viewport到您的背景图像的所需尺寸:

        <FlowDocumentReader ViewingMode="Scroll">
            <FlowDocument>
               <FlowDocument.Background>
                    <ImageBrush TileMode="Tile" Stretch="Fill" Viewport="0,0,1000,400" ViewportUnits="Absolute">
                        <ImageBrush.ImageSource>
                            <!--Image source here...-->
                        </ImageBrush.ImageSource>
                    </ImageBrush>

可选择设置ImageBrush.StretchStretch.Fill用你的图像填充你指定的视口(viewport),并设置ImageBrush.TileModeTileMode.Tile使背景图像重复。

关于c# - WPF 如何将 FlowDocument BGImage 转换为 FixedDocument 并在 DocumentViewer 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021961/

相关文章:

c# - 使用 Sort() 按特定条件对列表数据进行排序,

c# - 使用 C# 的 AWS Elasticache Redis

WPF DataBinding 在绑定(bind)中指定数组索引不起作用

wpf 数据绑定(bind)网格行可见性

wpf - 如何在窗口标题中设置静态资源绑定(bind)

c# - 尝试激活 DbContext 时无法解析 Microsoft.EntityFrameworkCore.DbContextOptions 类型的服务

c# - 在 Visual Studio 2012 RC 中看不到 Oracle Data Provider for .NET

c# - WPF - 折线/路径周围的 HitTest 半径

c# - 窗口的默认构造函数是否必须公开?

c# - WPF 应用程序不会运行 UI/MainWindow(更新 : starting after 6+ min of waiting!)