HtmlFormatProvider adds extra empty paragrpah on import. (How to remove empty paragraph "NormalWeb")

1 Answer 179 Views
RichTextBox
Kamran
Top achievements
Rank 3
Iron
Iron
Veteran
Kamran asked on 01 Oct 2021, 12:53 PM | edited on 01 Oct 2021, 12:54 PM

When I import html document to HtmlFormatProvider, I noticed empty line at beginning of each paragraph. When I exported same document as html document. I found empty paragraph is added, which causing extra spacing. Our challenge is to remove such extra spaces, so that all text in columns are same aligned. 

 

Following is actual document content

<body><p class="NormalWeb "><span class="s_B0779B8D">some text here</span></p><p class="Normal ">&nbsp;</p></body>

While following is generated html document (First para is not part of actual document. and same rendered in richtextbox editor. which adds extra space on top)

<p class="NormalWeb ">
	<span class="s_DED8E654"> </span>
</p>
<p class="NormalWeb ">
	<span class="s_BCF031D1">some text here</span>
</p>
<p class="Normal ">&nbsp;</p>

I am using following code to import html into editor


using (Stream input = new MemoryStream(Encoding.Default.GetBytes(html)))
{
	HtmlFormatProvider provider = new HtmlFormatProvider();
	provider.ImportSettings = new HtmlImportSettings
	{
		UseDefaultStylesheetForFontProperties = false,
		UseHtmlHeadingStyles = false
	};
	RadDocument document = provider.Import(input);
	editor.Document = document;

	editor.Document.StyleRepository["NormalWeb"].ParagraphProperties.SpacingBefore = 0;
	editor.Document.StyleRepository["NormalWeb"].ParagraphProperties.SpacingAfter = 0;
	editor.Document.StyleRepository["NormalWeb"].ParagraphProperties.AutomaticSpacingAfter = false;

}

 

For reference I am attaching actual html document, exported html document and rendering in user interface.

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan Ivanov
Telerik team
answered on 06 Oct 2021, 01:11 PM

Hello Kamran,

By design, there is a trailing paragraph in each table cell. The LineSpacing property should be the key to shrinking this unwanted space. You can either stick to your current approach and add the setter to LineSpacing, or detect these paragraphs and apply the formatting directly. I tested the following approach on my side and it does the trick:

foreach (Paragraph p in this.radRichTextBox.Document.EnumerateChildrenOfType<Paragraph>().Where(s => s.PreviousSibling is Table && s.Parent is TableCell && s.IsEmpty))
            {
                MinifyParagraph(p);
            }
            radRichTextBox.UpdateLayout();
            radRichTextBox.UpdateEditorLayout();
        


        public void MinifyParagraph(Paragraph p)
        {
            p.LineSpacing = 0;
            p.SpacingAfter = 0;
            p.SpacingBefore = 0;
            p.FontSize = 0;
        }

Regards,
Ivan Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
RichTextBox
Asked by
Kamran
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Ivan Ivanov
Telerik team
Share this question
or