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

Free Support Forum - aspose.com

Use dynamic picture in word

hi

i use this code to create word document
Document doc = Execute();


public Document Execute()
{
Document doc = new Document();
doc = new Document(System.IO.Path.Combine(DocPath, “myTemplate”));
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
create a dataset
fill data set

foreach (DataRow r in eds.Tables[0].Rows)
{
string[] fieldNames = new string[eds.Tables[0].Columns.Count];
object[] fieldValues = new object[eds.Tables[0].Columns.Count];
for (int i = 0; i < eds.Tables[0].Columns.Count; ++i)
{
fieldNames[i] = eds.Tables[0].Columns[i].ColumnName;
fieldValues[i] = r[i];
}
doc.MailMerge.Execute(fieldNames, fieldValues);
}
        // </div><div>doc.MailMerge.Execute(eds.Tables[0].Columns, eds.Tables[0]); </div><div> return doc;</div><div><br></div><div>I want use a pitcure in word file</div><div>this pitcture must change in deferent status</div><div>how can i do this?</div>

Hi Mohsen,


Thanks for your inquiry. Please try running the following simple code against attached template document:

Document doc = new Document(MyDir + @“inp.docx”);

doc.MailMerge.FieldMergingCallback = new HandleMergeField();

doc.MailMerge.Execute(GetDataTable());

doc.Save(MyDir + @"16.1.0.docx");


private class HandleMergeField : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

if (e.FieldValue != null)

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

builder.InsertImage(e.FieldValue.ToString());

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)

{

// Do nothing.

}

}

private static DataTable GetDataTable()

{

DataTable dataTable = new DataTable("tbl");

dataTable.Columns.Add(new DataColumn("mf"));

DataRow dataRow;

for (int i = 0; i < 5; i++)

{

dataRow = dataTable.NewRow();

dataRow[0] = "D:\\Temp\\Aspose.Words" + i + ".jpg";

dataTable.Rows.Add(dataRow);

}

return dataTable;

}


Hope, this helps.

Best regards,