I hesitate to post this, as I'm sure there must be a better way, but since no-one else has replied....
A flow document Section
appears to wrap paragraphs with whitespace equivilent to the paragraph's LineHeight
.
LineHeight
cannot be 0, but it can be very small. Setting LineHeight
on the Section
will remove whitespace around ALL paragraphs.
<FlowDocumentScrollViewer>
<FlowDocumentScrollViewer.Resources>
<Style TargetType="Paragraph">
<Setter Property="Background" Value="LightBlue" />
</Style>
</FlowDocumentScrollViewer.Resources>
<FlowDocument>
<Section LineHeight="0.1">
<Paragraph>1</Paragraph>
<Paragraph>2</Paragraph>
<Paragraph/>
<Paragraph>4</Paragraph>
<Paragraph>5</Paragraph>
</Section>
</FlowDocument>
</FlowDocumentScrollViewer>

Setting LineHeight
like this will generally not affect the text inside the paragraphs, because the default LineStackingStrategy
uses the height of the font instead. Note how the blank Paragraph still has height.
You might think setting LineHeight
only on the blank paragraph would work, but Section
will still honour the whitespace of the preceeding paragraph. Since the preceding paragraph has normal LineHeight
, you still get the margin.
So, in order to remove your blank paragraph completely, you need to set LineHeight
on the blank AND the preceeding paragraph, and tell your blank paragraph to use the LineHeight
as its block height:
<FlowDocumentScrollViewer>
<FlowDocumentScrollViewer.Resources>
<Style TargetType="Paragraph">
<Setter Property="Background" Value="LightBlue" />
</Style>
</FlowDocumentScrollViewer.Resources>
<FlowDocument>
<Section>
<Paragraph>1</Paragraph>
<Paragraph LineHeight="0.1">2</Paragraph>
<Paragraph LineHeight="0.1" LineStackingStrategy="BlockLineHeight"/>
<Paragraph>4</Paragraph>
<Paragraph>5</Paragraph>
</Section>
</FlowDocument>
</FlowDocumentScrollViewer>

I tried to write a trigger that would do this automatically for blank paragraphs, but unfortunately Paragraph.Inlines.Count
is not a DependencyProperty, and trying to use it to detect blank paragraphs is unreliable depending on when the paragraph gets populated.