Extra line comes with paragraph when using insertHtml

Hello I am using the InsertHtml for Inserting a html
… in the document when i insert it, a extra line is coming after the paragraph. how i remove that line

Hi Ravesh,

Thanks for your interest in Aspose products.

I have observed the requirements shared by you and request you to please share the complete requirements on your end. Please also share that which product that you are using on your end to serve the purpose.

Many Thanks,

Hi Ravesh,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you load your input html in MS Word, you will get the same output.

You can use DocumentBuilder.InsertHtml method to insert an HTML fragment or whole HTML document into the document.

It would be great if you please share the following detail for investigation purposes.

  • Please attach your input Word document
  • Please attach your HTML
  • Please attach the output document that shows the undesired behavior.
  • Please
    attach your target document showing the desired behavior.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir ,

I am inserting an html paragraph into the document using the builder.InserHtml(),and Replace the mail marge field like <> in input document then after insertion a blank line comes in output that i highlighted.

Hi Ravesh,

Thanks
for sharing the input and output document. Could you please share your HTML documents (as requested in my previous post) which you are inserting in the input document? We need the HTML documents for testing purposes. I will investigate the issue on my side and provide you more information.

Please also create a simple application (for example a Console Application Project) that helps us reproduce the same problem on our end and attach it here for testing.

Following code example shows how to mail merge HTML data into a document.

// File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it.
// File 'MailMerge.HtmlData.html' contains some valid Html data.
// The same approach can be used when merging HTML data from database.
public void MailMergeInsertHtml()
{
    Document doc = new Document(MyDir + "MailMerge.InsertHtml.doc");
    // Add a handler for the MergeField event.
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
    // Load some Html from file.
    StreamReader sr = File.OpenText(MyDir + "MailMerge.HtmlData.html");
    string htmltext = sr.ReadToEnd();
    sr.Close();
    // Execute mail merge.
    doc.MailMerge.Execute(new string[] { "htmlField1" }, new string[] { htmltext });
    // Save resulting document with a new name.
    doc.Save(MyDir + "MailMerge.InsertHtml Out.doc");
}
private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    // / 
    // / This is called when merge field is actually merged with data in the document.
    // / 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (e.DocumentFieldName.StartsWith("html"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.InsertHtml((string)e.FieldValue);
            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}

Hi Tahir,

We are fetching the html tag from the database like
This is a sample text
, this tag we saving in the database using the html editor in our application. There is no html document it just a tag.

Hi Ravesh,

Thanks
for sharing the detail. In your case, I suggest you please remove the empty Paragraph after inserting the HTML using DocumentBuilder.InsertHtml method. Please check the following highlighted code snippet for your kind reference. Hope this helps you. Please let us know if you have any more queries.

If you still face problem, please share the HTML fragment from you database here for our reference. We will then provide you more information about your query.

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
    if (e.DocumentFieldName.StartsWith("SiteDescription"))
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);
        builder.MoveToMergeField(e.DocumentFieldName, true, true);
        builder.InsertHtml("Paragraph This is a sample text" +
        "Paragraph This is a sample text");
        if (builder.CurrentParagraph != null && builder.CurrentParagraph.ToString(SaveFormat.Text).Trim() == "")
            builder.CurrentParagraph.Remove();
        e.Text = "";
    }
}

Hi Tahir,

Thanks for your reply , but i have another query now ,i have set some pagragraph formatiing on the inserted paragraph using the INodeChangingCallback.NodeInserted how i will now apply it now with this functionality. below is the code for that:

void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
    // Change the font of inserted text contained in the Run nodes.
    if (args.Node.NodeType == NodeType.Run)
    {
        Aspose.Words.Font font = ((Run)args.Node).Font;

        font.Size = FontSize;
        font.Name = "Arial";
        font.Bold = IsBold;
        font.Italic = false;
        font.Underline =Underline.None;

        Aspose.Words.Paragraph para = ((Run)args.Node).ParentParagraph;
        para.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
        para.ParagraphFormat.LineSpacing = 18;
        para.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
        para.ParagraphFormat.SpaceAfterAuto = false;
        para.ParagraphFormat.SpaceAfter = SpaceAfter;
        para.ParagraphFormat.SpaceBeforeAuto = true;
    }
}

Hi Ravesh,

Thanks for your inquiry. Following code example shows how to implement custom logic over node insertion in the document by changing the font of inserted HTML content.

Hope this helps you. Please let us know if you have any more queries.

// Create a blank document object
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
HandleNodeChangingFontChanger obj = new HandleNodeChangingFontChanger();
obj.FontSize = 12;
obj.IsBold = false;
obj.SpaceAfter = 0.0;
// Set up and pass the object which implements the handler methods.
doc.NodeChangingCallback = obj;
// Insert sample HTML content
builder.InsertHtml("Hello World");
doc.Save(MyDir + "Document.FontChanger Out.docx");
public class HandleNodeChangingFontChanger : INodeChangingCallback
{
    public double FontSize;
    public Boolean IsBold;
    public double SpaceAfter;
    void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
    {
        // Change the font of inserted text contained in the Run nodes.
        if (args.Node.NodeType == NodeType.Run)
        {
            Aspose.Words.Font font = ((Run)args.Node).Font;
            font.Size = FontSize;
            font.Name = "Arial";
            font.Bold = IsBold;
            font.Italic = false;
            font.Underline = Underline.None;
            Aspose.Words.Paragraph para = ((Run)args.Node).ParentParagraph;
            para.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
            para.ParagraphFormat.LineSpacing = 18;
            para.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
            para.ParagraphFormat.SpaceAfterAuto = false;
            para.ParagraphFormat.SpaceAfter = SpaceAfter;
            para.ParagraphFormat.SpaceBeforeAuto = true;
        }
    }
    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {
        // Do Nothing
    }
}

Hi Tahi ,
Thanks for the feedback,
your previous solution is working & they remove the extra line & with it my custom font setting are also working. but still there is on problem.

like if there are three marge field in document like:

in same order in document as seen. Now if i called the below code than insdie the Handler
Handler.HandlerMargeFieldInsertHtml the error comes because it showing the fieldname proppicture
& corresponding to which there is no value the proppicture i replace with the image after the PropAnalysis

i saw that there is if condition in your handle exmaple:
if (e.DocumentFieldName.StartsWith("html")
but my field name always not start with a html the custome means name may be proppicture or propanalyais or somethisng else but the data i send ,will be the html
most of the time
on which i want to call this method & if possible pleace also give me an example of the another method of same handler :
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
can u use this method for the text wraping arround a inserted image

***code for data insertion

Document doc = new Document(TempPath);

try
{
    doc.MailMerge.FieldMergingCallback = new Handler.HandlerMargeFieldInsertHtml();
    doc.NodeChangingCallback = new Handler.HandlerParagrapFormat();
    doc.MailMerge.Execute(new string[] { "PropAnalysis" }, new string[] { "hi hello this my text" });
}
catch (Exception ex)
{

}

the Code for Handler is

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
    // All merge fields that expect HTML data should be marked with some prefix, e.g. ‘html’.
    // if (e.DocumentFieldName.StartsWith("html"))
    // {
    // Insert the text for this merge field as HTML data, using DocumentBuilder.
    try
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);

        builder.MoveToMergeField(e.DocumentFieldName);

        builder.InsertHtml((string)e.FieldValue);

        if (builder.CurrentParagraph != null && builder.CurrentParagraph.ToString(SaveFormat.Text).Trim() == "")

            builder.CurrentParagraph.Remove();

        // The HTML text itself should not be inserted.
        // We have already inserted it as an HTML.
        e.Text = "";
    }
    catch (Exception ex)
    { }
    // }
}

Hi Ravesh,

Thanks for your inquiry.

Ravesh:
,
i saw that there is if condition in your handle exmaple:
if (e.DocumentFieldName.StartsWith(“html”)
but my field name always not start with a html the custome means name may be proppicture or propanalyais or somethisng else…

In this case, you need to modify the if condition according to your requirement.

Ravesh:
,
& if possible pleace also give me an example of the another method of same handler :
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
can u use this method for the text wraping arround a inserted image

Please specify a field name prefix like **Image:**MyFieldName in the document to be able to directly merge images during Mail Merge. Please check the following code snippet for your kind reference.

private class HandleMergeImageFieldFromBlob : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // Do nothing.
    }
    // / 
    // / This is called when mail merge engine encounters Image:XXX merge field in the document.
    // / You have a chance to return an Image object, file name or a stream that contains the image.
    // / 
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // The field value is a byte array, just cast it and create a stream on it.
        MemoryStream imageStream = new MemoryStream((byte[])e.FieldValue);
        // Now the mail merge engine will retrieve the image from the stream.
        e.ImageStream = imageStream;
    }
}

Hi Tahir,

I have one more issue i am using a talerick editor which ask for saving the ms word formation
if copy & paste the data from the word file. into the editor
& give this

output:
The
value stated herein is based on our understanding of the site and improvement
descriptions as represented to us by the owner’s representative, client, public
records, our inspection and other available sources. It is your responsibility to read the report
and inform the appraisers of any errors or omissions you are aware of prior to
utilizing
…Continue
which method i used to insert this type of paragraph

Hi Ravesh,

Thanks for your inquiry. First of all please note that Aspose.Words for .NET is a class library that enables your applications to perform a great range of document processing tasks. Aspose.Words supports DOC, DOCX, RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats. With Aspose.Words you can generate, modify, convert, render and print documents without utilizing Microsoft Word®. For more information, please go through the documentation below:
https://docs.aspose.com/words/net/

Secondly, Aspose.Words does not offer any UI or web control for copy & paste data or editing document. However, you can perform document processing tasks using Aspose.Words.

Ravesh:
which method i used to insert this type of paragraph

You can insert a Paragraph from one document into another using NodeImporter class. This class allows to efficiently perform repeated import of nodes from one document to another.

Could you please share some more detail about this query? We will then provide you more information on this along with code.