Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove the space taken by an empty paragraph?

How to get rid of space from a paragraph? I have tried using negative margin/padding but it doesn't accept negative values for these properties. Any idea?

My code is mentioned below:

<FlowDocument>
    <Section>
        <Paragraph>1</Paragraph>
        <Paragraph>2</Paragraph>
        <Paragraph></Paragraph>
        <Paragraph>4</Paragraph>
    </Section>
</FlowDocument>

And, outputs for the above code is given below:

enter image description here

EDIT: Here is an example that would make a bit more sense(as per the comments):

<FlowDocument>
    <Section>
        <Paragraph>
            <TextBlock Text="1" Visibility="Visible"/>
        </Paragraph>
        <Paragraph>
            <TextBlock Text="2" Visibility="Visible"/>
        </Paragraph>
        <Paragraph>
            <TextBlock Text="3" Visibility="Collapsed"/>
        </Paragraph>
        <Paragraph>
            <TextBlock Text="4" Visibility="Visible"/>
        </Paragraph>
    </Section>
</FlowDocument>

Which makes the exact same result.

like image 387
Maxime Tremblay-Savard Avatar asked Jun 16 '15 19:06

Maxime Tremblay-Savard


People also ask

How do I make no space between paragraphs in Word?

Go to Design > Paragraph Spacing. Choose an option. To single space your document, select No Paragraph Space.

Why is there extra space between paragraphs in Word?

Word adds space between paragraphs—whether you want it to or not. If you display paragraph marks, you'll not find any extra paragraph marks. This behavior is part of Word's styling. When you press Enter to create a new paragraph, Word increases the line spacing to mark the change from one paragraph to another.


1 Answers

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.

like image 133
GazTheDestroyer Avatar answered Nov 15 '22 08:11

GazTheDestroyer