Flowdocument does not use full width / height

I have a FlowDocument that I want to fill the entire width and height of my window. I tried using FlowDocumentPageViewer

(no luck) and now I am using DocumentPageView

. I still can't get it to pin / fill all the space; it just sits in the middle, at the smallest size it can create (does this make sense?)

Here is my code:

   public DocumentPageView GetPage()
   {
        FlowDocumentPageViewer viewer = new FlowDocumentPageViewer();           
        StreamReader reader = new StreamReader(location);
        string data = reader.ReadToEnd();
        reader.Close();
        string xamlData = HtmlToXamlConverter.ConvertHtmlToXaml(data, true);
        FlowDocument result = (FlowDocument)System.Windows.Markup.XamlReader.Load(new MemoryStream(System.Text.UnicodeEncoding.Default.GetBytes(xamlData)));

        viewer.Document = result;
        viewer.VerticalAlignment = VerticalAlignment.Center;
        viewer.HorizontalAlignment = HorizontalAlignment.Center;

        DocumentPageView pageView = new DocumentPageView();
        pageView.VerticalAlignment = VerticalAlignment.Center;
        pageView.HorizontalAlignment = HorizontalAlignment.Center;
        pageView.Stretch = System.Windows.Media.Stretch.Uniform;
        pageView.PageNumber = 0;
        pageView.StretchDirection = StretchDirection.Both;
        pageView.DocumentPaginator = ((IDocumentPaginatorSource)result).DocumentPaginator;
        return pageView;
   }

      

Note that this code contains a combination of my two methods, but is currently only used DocumentPageView

. This is the Xaml generated from my HTML source:

<FlowDocument xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph TextAlignment="center" FontSize="22pt" FontFamily="arial">Test Paragraph</Paragraph>
<Paragraph TextAlignment="center" FontFamily="arial">Test second paragraph</Paragraph>
</FlowDocument>

      

If I resize the fonts, the content will only resize vertically (note that the stretch direction is set to both). Any ideas?

+2


source to share


5 answers


I had a similar problem with FlowDocumentScrollView

, but this solution also works with FlowDocumentPageView

:

FlowDocument

centered because the property is PagePadding

set to auto,auto,auto,auto

. Setting PagePadding to on 0

removes this behavior.



<FlowDocumentScrollViewer VerticalScrollBarVisibility="Auto">
    <FlowDocument PagePadding="0">
    </FlowDocument>
</FlowDocumentScrollViewer>

      

+2


source


The following lines make your elements center themselves (this is not the same as stretching):

viewer.VerticalAlignment = VerticalAlignment.Center;
viewer.HorizontalAlignment = HorizontalAlignment.Center;
pageView.VerticalAlignment = VerticalAlignment.Center;
pageView.HorizontalAlignment = HorizontalAlignment.Center;

      

You can safely remove them as the default alignment here is Stretch.



If you still want to center the viewer, I explicitly define the page size (remember that at 96 points in inches and the Margin and PageSize are set in points):

Width = 96 * 8.5

Height = 96 * 11

+1


source


Could you please indicate where and how you are using the result of your GetPage () method? Is it xbap or desktop application?

I am asking this because the following document displays perfectly correctly in Kaxaml :

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
  <FlowDocumentPageViewer>
  <FlowDocument >
<Paragraph TextAlignment="center" FontSize="22pt" FontFamily="arial">Test Paragraph</Paragraph>
<Paragraph TextAlignment="center" FontFamily="arial">Test second paragraph</Paragraph>
</FlowDocument>
  </FlowDocumentPageViewer>
  </Grid>
</Page>

      

PS: If it is a desktop application, you can always find who is causing the problem, the Snoop tool , form Pete Blois.

0


source


Update: its a desktop app, the result of getpage () is sent to a grid that padding / fills nicely.

<Window x:Class="GreenWebPlayerWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="Auto" Width="Auto" WindowStyle="None" WindowState="Maximized" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing">
    <Grid Width="Auto" Height="Auto" Name="TransitionContainer" Background="White" Margin="0">
        //here the result from GetPage() method is inserted
    </Grid>
</Window>

      

-2


source


(this comment was written from another account)

@Anvaka: What I mean is perfectly correct is that the document must "dock in the container", that is, the fonts must be resized, it must fill the container in height and width. Now that I think about it, it might seem like the wrong behavior for a flow document.

When I place the flow document in the container, it is centered in the middle of the parent container (how good is that). but the parent container does not fill its parent container, so when I increase or resize the font, I would like the documentPageView container to grow in width and height, but stay centered.

-2


source







All Articles