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

Free Support Forum - aspose.com

Implement IFieldMergingCallback and remove last empty paragaph using C#

Hello,

Attached is the document with merge region which contains a table and an empty paragraph.

Is it possible to implement a merging callback which will delete the line after the last paragraph (the desired output attached)?

Best regards, Evgeniy

Hi Evgeniy,

Please use the following code snippet to remove empty paragraphs.

Document doc = new Document(MyDir + "Test01.doc");

DocumentBuilder db = new DocumentBuilder(doc);

DataTable employee = new DataTable("Persons");

employee.Columns.Add("title1");

employee.Columns.Add("name1");

employee.Columns.Add("birthday1");

employee.Rows.Add("Mr.", "Smith", "01.01.1970");

employee.Rows.Add("Ms.", "Brown", "01.01.1972");

doc.MailMerge.ExecuteWithRegions(employee);

RemoveEmptyParagraph remover = new RemoveEmptyParagraph();

doc.Accept(remover);

//Node[] paras = doc.GetChildNodes(NodeType.Paragraph, true).ToArray();

//foreach (Paragraph para in paras)

//{

// if (para.ToTxt().Trim() == "")

// para.Remove();

//}

doc.Save(MyDir + "out.doc");

public class RemoveEmptyParagraph : DocumentVisitor

{

public override VisitorAction VisitParagraphStart(Paragraph paragraph)

{

// Check if string is empty

if (string.IsNullOrEmpty(paragraph.ToTxt().Trim()))

paragraph.Remove();

return VisitorAction.Continue;

}

}