@shripad.maryland,
Sample documents are attached: Docs.zip (92.0 KB)
You can build logic on the following code to get the desired output:
Document document = new Document("E:\\Temp\\Aspose.Words.POC\\B1_Template.docx");
DocumentBuilder builder = new DocumentBuilder(document);
#region pre-process
document.FirstSection.Body.GetChildNodes(NodeType.Shape, true).Clear();
document.FirstSection.Body.GetChildNodes(NodeType.Comment, true).Clear();
foreach(Run run in document.FirstSection.Body.GetChildNodes(NodeType.Run, true))
{
run.Font.Underline = Underline.None;
}
#endregion
#region add hidden bookmarks before every merge field
int i = 0;
foreach(Field field in document.Range.Fields)
{
if (field.Type == FieldType.FieldMergeField)
{
FieldMergeField mf = (FieldMergeField)field;
builder.MoveToMergeField(mf.FieldName, false, false);
builder.StartBookmark("_bm" + i);
builder.EndBookmark("_bm" + i);
i++;
}
}
#endregion
#region perform mail merge
DataTable dt = new DataTable();
dt.Columns.Add("NOIDate");
dt.Columns.Add("RecordOwner1Name");
dt.Columns.Add("RecordOwner2Name");
dt.Columns.Add("RecordOwner3Name");
dt.Columns.Add("RecordOwner4Name");
dt.Columns.Add("RecordOwner1Address");
dt.Columns.Add("RecordOwner2Address");
dt.Columns.Add("RecordOwner3Address");
dt.Columns.Add("RecordOwner4Address");
dt.Columns.Add("PropertyAddress");
dt.Columns.Add("SecuredPartyName");
dt.Columns.Add("SecuredPartyPhone");
dt.Rows.Add("06/26/2019", "ABC INC (RECORD OWNER 1); ", "Tom Hanks (RECORD OWNER 2); ", "Johnny Depp (RECORD OWNER 3); ", "Will Smith (RECORD OWNER 4)",
"10109 REDOAKPINK WAY, ELLERSLIE, MD, 21529 (RECORD OWNER 1); ", "20108 JEFFERSON WAY, Unit:4205, ELLERSLIE, MD, 21529, 4654 (RECORD OWNER 2); ",
"30104 REDPINE Park, ELLERSLIE, MD, 21529, 4654 (RECORD OWNER 3); ", "12578 Elkridge WAY, Unit:4205, ELLERSLIE, MD, 21529, 4646 (RECORD OWNER 4)",
"10100 PINK WAY, Unit:4205, ELLERSLIE, MD, 21529", "ABC Bank", "4654654564");
document.MailMerge.Execute(dt.Rows[0]);
#endregion
#region draw lines underneath each mail-merged line
var collector = new LayoutCollector(document);
var it = new LayoutEnumerator(document);
foreach (Paragraph paragraph in document.FirstSection.Body.GetChildNodes(NodeType.Paragraph, true))
{
bool hasBookmark = false;
Bookmark bmFirst = null;
foreach (Bookmark bm in paragraph.Range.Bookmarks)
{
if (bm.Name.StartsWith("_bm"))
{
bmFirst = bm;
hasBookmark = true;
break;
}
}
if (hasBookmark)
{
var paraBreak = collector.GetEntity(paragraph);
object stop = null;
var prevItem = paragraph.PreviousSibling;
if (prevItem != null)
{
var prevBreak = collector.GetEntity(prevItem);
if (prevItem is Paragraph)
{
it.Current = collector.GetEntity(prevItem); // para break
it.MoveParent(); // last line
stop = it.Current;
}
else if (prevItem is Table)
{
var table = (Table)prevItem;
it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break
it.MoveParent(); // cell
it.MoveParent(); // row
stop = it.Current;
}
else
{
throw new Exception();
}
}
it.Current = paraBreak;
it.MoveParent();
if (it.Rectangle.Width > 0)
AddLine(paragraph, it.Rectangle.Bottom, it.Rectangle.Left, it.Rectangle.Width);
while (it.Current != stop)
{
if (!it.MovePreviousLogical())
break;
if (it.Rectangle.Width > 0)
AddLine(paragraph, it.Rectangle.Bottom, it.Rectangle.Left, it.Rectangle.Width);
}
//foreach (Shape shape in paragraph.GetChildNodes(NodeType.Shape, true))
//{
// shape.Top -= 7;
//}
it.Current = collector.GetEntity(bmFirst.BookmarkStart);
PageSetup ps = paragraph.ParentSection.PageSetup;
Shape shape = (Shape)paragraph.LastChild;
shape.Left = it.Rectangle.Left;
shape.Width = shape.Width - it.Rectangle.Left + ps.LeftMargin;
}
}
#endregion
#region remove hidden bookmarks
foreach (Bookmark bm in document.Range.Bookmarks)
{
if (bm.Name.StartsWith("_bm"))
{
bm.Remove();
}
}
#endregion
document.UpdatePageLayout();
document.Save("E:\\Temp\\Aspose.Words.POC\\19.7.pdf");
public static void AddLine(Paragraph para, double top, double left, double width)
{
DocumentBuilder builder = new DocumentBuilder((Document)para.Document);
builder.MoveTo(para);
Shape line = new Shape((Document)para.Document, ShapeType.Line);
line.StrokeColor = Color.Blue;
line.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
line.RelativeVerticalPosition = RelativeVerticalPosition.Page;
line.WrapType = WrapType.None;
builder.InsertNode(line);
line.Width = width;
line.Left = left;
line.Top = top;
}