Jeson Martajaya
Jeson Martajaya

Reputation: 7352

WPF how to convert FlowDocument BGImage to FixedDocument and show it in DocumentViewer?

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. How to convert this FlowDocument to FixedDocument and display it in DocumentViewer, so the background image will be fixed as well ?

I use the conversion logic from here. But it does not display the FlowDocument.Background image.

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

Upvotes: 2

Views: 645

Answers (1)

dbc
dbc

Reputation: 116670

You wrote

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.

Not exactly an answer to your specific question, but to avoid this, set ImageBrush.ViewportUnits to BrushMappingMode.Absolute. Then, set ImageBrush.Viewport to the desired dimension of your background image:

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

Optionally set ImageBrush.Stretch to Stretch.Fill to fill the your specified viewport with your image, and set ImageBrush.TileMode to TileMode.Tile to make the background image repeat.

Upvotes: 1

Related Questions