Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use a DocumentViewer control to display a document created on the fly from paragraphs held in the database. I can construct a FlowDocument and display it in a RichTextBox and it works as expected. When I convert the FlowDocument to a FixedDocument and set it as the Document property of the DocumentViewer, it is coming out in two columns, something like a newspaper page. I just want the content to be full, or stretched, or justified, basically no formatting; just use all the width available.
I don't know if it's my code that converts the FlowDocument to FixedDocument that is introducing some formatting, or if it is a property on the DocumentViewer that I need to set.

What I have tried:

I have tried so many online code examples I can't begin to list them. Here is my code for converting FlowDocument to FixedDocument. After that I simply assign the FixedDocument to the Document property of a DocumentViewer.

Private Function GetFixedDocument2(ByVal flowDoc As FlowDocument) As FixedDocument

    ' Create a FixedDocument with the same dimensions as A4 paper
    Dim fixedDoc As New FixedDocument()
    fixedDoc.DocumentPaginator.PageSize = New Size(793.7, 1122.5) ' 210mm by 297mm converted to points

    ' Create a FixedPage for each page of content in the FlowDocument
    Dim paginator As DocumentPaginator = CType(flowDoc, IDocumentPaginatorSource).DocumentPaginator
    paginator.ComputePageCount()
    Dim pageCount As Integer = paginator.PageCount
    For i As Integer = 0 To pageCount - 1
        Dim pageContent As New PageContent()
        Dim fixedPage As New FixedPage()

        ' Set the dimensions of the FixedPage to match the FixedDocument
        fixedPage.Width = fixedDoc.DocumentPaginator.PageSize.Width
        fixedPage.Height = fixedDoc.DocumentPaginator.PageSize.Height

        ' Get the content of the current page of the FlowDocument
        Dim flowContent As DocumentPage = paginator.GetPage(i)
        ' Create a DrawingVisual to render the content
        Dim drawingVisual As New DrawingVisual()
        Using drawingContext As DrawingContext = drawingVisual.RenderOpen()
            drawingContext.DrawRectangle(New VisualBrush(flowContent.Visual), Nothing, New Rect(New Point(), flowContent.Size))
        End Using

        ' Create an Image and set its source to the DrawingVisual
        Dim pageImage As New Image()
        pageImage.Source = New DrawingImage(drawingVisual.Drawing)

        ' Add the Image to the FixedPage
        fixedPage.Children.Add(pageImage)

        ' Add the FixedPage to the PageContent
        pageContent.Child = fixedPage

        ' Add the PageContent to the FixedDocument
        fixedDoc.Pages.Add(pageContent)
    Next

    Return fixedDoc

End Function
Posted
Updated 4-May-23 18:12pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900