We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How do I remove extra linefeed that is inserted when using InsertHTML?

We have a Word Document that will have some merge fields like this;


Some text<<html_field>> then some more text.

Note the field is in the middle of a sentence and also sometimes there is a space before and after the <<html_field>> and sometimes there is not. Also, we are using the IFieldMergingCallback.FieldMerging logic that is seen in many examples.

When we use InsertHTML to add some text which might be like;

builder.InsertHTML(" that is bold ")

What we get is

Some text that is bold <— Paragraph mark is inserted that we don’t want
then some more text.

Instead of

Some text that is bold then some more text.

There are many examples that remove the paragraph mark but I cannot get any to work where there is text hard up against the end of the <<html_field>> code.

Thanks
Hi Brian,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 17.4 with following code example and have not found the shared issue. Please use Aspose.Words for .NET 17.4. We have attached the input and output documents with this post for your kind reference.

If you still face problem, please share your input document and code example to reproduce this issue at our end. We will then provide you more information on this.

private class HandleMergeFieldInsertHtml : IFieldMergingCallback

{

///

/// This is called when merge field is actually merged with data in the document.

///

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

// 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.

}

}

// Create an empty document. It contains one empty paragraph.

Document doc = new Document(MyDir + "in.docx");

// Add a handler for the MergeField event.

doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();

// Execute mail merge.

doc.MailMerge.Execute(new string[] { "html_field" }, new string[] { @" that is bold " });

doc.Save(MyDir + "17.4.0.docx");

Hi



Thanks for the response. I fixed my own problem but your response put me on the right track.



My issue actually turned out to be some code that was replacing CF/LF with [p] instead of [br]. ([] characters used in place of LT & GT)



Thanks Again

Hi Brian,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.