I don’t understand, How it would help me to replace the Text ##Table to the table developed using DocumentBuilder?? Can’t DocumnetBuilder be pass to the object add_data as I told in the above problem???
Hi Monika,
Thanks for your request and sorry that my explanation was not clear. I supposed you would figure out how to implement this from the article.
In the field merging callback, you can create DocumentBuilder object, move its cursor to the field and insert any element you like. In your case this is a table. I create a simple code example that demonstrates how to insert a table in IFieldMergingCallback:
[Test]
public void Test001()
{
// Open template.
Document doc = new Document(@"Test001\in.doc");
// Set field merging callback.
doc.MailMerge.FieldMergingCallback = new MyFieldMergingCallback();
// Execute mail merge. (just to demonstrate the technique i put one mergefield into the document.)
// As a value of the first you can use the source from which you build a table. in my case this is a string,
// wehre cells are separated by tabs and rows are separated by line breaks.
doc.MailMerge.Execute(new string[] { "table" }, new object[] { "test\ttest\ttest\ntest\ttest\ttest" });
// Save output.
doc.Save(@"Test001\out.doc");
}
public class MyFieldMergingCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
if (args.FieldName == "table")
{
// Create a DocumentBuilder object and build move its cusror to the field.
DocumentBuilder builder = new DocumentBuilder(args.Document);
builder.MoveToField(args.Field, true);
// Here you can configure the table.
// For exampel set borders.
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
// Parse data string and build a table.
string[] rows = ((string)args.FieldValue).Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string row in rows)
{
// Parse rows.
string[] cells = row.Split(new char[] { '\t' });
foreach (string cell in cells)
{
builder.InsertCell();
builder.Write(cell);
}
builder.EndRow();
}
builder.EndTable();
// We alrety inserted a value so set text of the first to empty string.
args.Text = "";
}
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing here.
}
}
Hope this helps.
Best regards,
OleDbDataAdapter dap = new OleDbDataAdapter(cm);
DataSet LineItem = new DataSet();
dap.Fill(LineItem);
con3.Close();
con3.Dispose();
cm.Dispose();
dap.Dispose();
StringBuilder SuperScriptPhyMsg = new StringBuilder();
DocumentBuilder docbld = new DocumentBuilder(doc);
DocumentBuilder docbld1 = new DocumentBuilder(doc);
PageSetup pageSetup = docbld.CurrentSection.PageSetup;
double width = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
double firstColunm = 0.5;
docbld.StartTable();
docbld.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Azure;
docbld.CellFormat.Width = width * firstColunm;
docbld.RowFormat.Height = 30;
docbld.Font.Size = 10;
docbld.Font.Bold = false;
docbld.RowFormat.HeightRule = HeightRule.Exactly;
docbld1.RowFormat.Height = 30;
docbld1.Font.Size = 10;
docbld1.Font.Bold = false;
docbld1.RowFormat.Height = 30;
docbld1.RowFormat.HeightRule = HeightRule.Exactly;
if (LineItem.Tables[0].Rows.Count != 0)
{
int cnt = 0;
int count = 0;
for (int l = 0; l < LineItem.Tables[0].Rows.Count; l++)
{
string Modifer = "";
if (LineItem.Tables[0].Rows[l]["blinpsModifier1"].ToString().Trim() != "")
Modifer = LineItem.Tables[0].Rows[l]["blinpsModifier1"].ToString().Trim() + ",";
if (LineItem.Tables[0].Rows[l]["blinpsModifier2"].ToString().Trim() != "")
Modifer += LineItem.Tables[0].Rows[l]["blinpsModifier2"].ToString().Trim() + ",";
if (LineItem.Tables[0].Rows[l]["blinpsModifier3"].ToString().Trim() != "")
Modifer += LineItem.Tables[0].Rows[l]["blinpsModifier3"].ToString().Trim() + ",";
if (LineItem.Tables[0].Rows[l]["blinpsModifier4"].ToString().Trim() != "")
Modifer += LineItem.Tables[0].Rows[l]["blinpsModifier4"].ToString().Trim() + " ";
if (Modifer.ToString().Trim() != "")
{
Modifer = Modifer.Trim().ToString().Substring(0, Modifer.Length - 1);
}
else
{
Modifer = Modifer.Trim().ToString();
}
if ((LineItem.Tables[0].Rows[l]["blinpsInvalid"].ToString().Trim().Equals("False")) || (LineItem.Tables[0].Rows[l]["blinpsInvalid"].ToString().Trim() == ""))
{
if (LineItem.Tables[0].Rows[l]["blinpsCPT_HCPCS"].ToString().Trim().Substring(0, 1).ToString().ToUpper().Trim() != "J")
{
if (LineItem.Tables[0].Rows[l]["blinpsServiceDateFrom"].ToString().Trim() != "")
{
if ((LineItem.Tables[0].Rows[l]["blinPhysAdminNDC"].ToString().Trim() != "") || (LineItem.Tables[0].Rows[l]["blinphyCrossWalkNDC"].ToString().Trim() != "") || (LineItem.Tables[0].Rows[l]["blinawpNDC"].ToString().Trim() != ""))
{
count = count + 1;
docbld.InsertCell();
docbld.Write(LineItem.Tables[0].Columns["blinpsServiceDateFrom"].ColumnName);
docbld1.InsertCell();
docbld.Write(Convert.ToDateTime(LineItem.Tables[0].Rows[l]["blinpsServiceDateFrom"].ToString().Trim()).ToShortDateString());
}
else
{
docbld.InsertCell();
docbld.Write(LineItem.Tables[0].Columns["blinpsServiceDateFrom"].ColumnName);
docbld1.InsertCell();
docbld1.Write(Convert.ToDateTime(LineItem.Tables[0].Rows[l]["blinpsServiceDateFrom"].ToString().Trim()).ToShortDateString());
}
}
else
{
docbld.InsertCell();
docbld.Write(LineItem.Tables[0].Columns["blinpsServiceDateFrom"].ColumnName);
docbld1.InsertCell();
docbld1.Write(LineItem.Tables[0].Rows[l]["blinpsServiceDateFrom"].ToString().Trim());
}
docbld.InsertCell();
docbld.Write(LineItem.Tables[0].Columns["blinpsCPT_HCPCS"].ColumnName);
docbld1.InsertCell();
docbld1.Write(LineItem.Tables[0].Rows[l]["blinpsCPT_HCPCS"].ToString().Trim());
I simply want to add documentbuilder to the object add_data[32]=?? which will replace ##table from the template. I am not getting your point
I am having two files attached with this mail. The file EOBProviderCMS1500New.xml is a template in which I have to replcae data from database. I have done the job of replacing data to this template. But now As in another file 839-1_EOBProvider, I have to develop a table Physian Service in which data will be inserted from database and I have to replace it in place of #table of template. Can u please guide how could I do this and provide code for this???
Hi Monika,
Thanks for your request. Actually, the code I provided above does exactly what you are asking for. Have you tried using the suggested code?
In your data source object, you should put data that should be used to build the table (in my example it was a string). Then in IFieldMergingCallback, you create a DocumentBuidler object, move it to the appropriate field in your template and build a table based on the data. Nothing difficult.
Best regards,
Will u please explain me the following line with datatable
doc.MailMerge.Execute(new string[] { “table” }, new object[] { “test\ttest\ttest\ntest\ttest\ttest” }) instead of string
Hi Monika,
Thanks for your request. This line executes simple mail merge and uses array of names and array of values as a data source. In your case, data source is a DataTable object. Please see the documentation:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Best regards,