Hi
Thanks for your inquiry. You can insert such field using the following code:
string start = "INCLUDEPICTURE \"C:\\\\Temp\\\\";
string end = ".jpg\"";
string mergefield = "MERGEFIELD idprof";
// Create new Document and DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert field
FieldStart picStart = builder.InsertField(start, string.Empty);
// Search for field end
Node picEnd = picStart;
while(picEnd.NodeType != NodeType.FieldSeparator)
{
picEnd = picEnd.NextSibling;
}
// Move to document start and insert mergefield
builder.MoveTo(picEnd);
builder.InsertField(mergefield, string.Empty);
// Insert end of Includepicture
builder.InsertNode(new Run(doc, end));
// Execute mail merge (just for testing)
doc.MailMerge.Execute(new string[] { "idprof" }, new object[] { "test" });
// Save output document
doc.Save(@"Test129\out.doc");
But note that you should update fields in the document manually (Ctrl+A and then F9, then your will see included picture.)
Also I think the better way to insert picture in your case is insert it during mail merge. Please see the following link for more information. https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
Hope this helps.
Best regards.
Hi
Thanks for your inquiry. Maybe you can use something like the following.
// Insert field
builder.InsertField(start, string.Empty);
// Search for field end
NodeCollection fieldSeps = builder.CurrentParagraph.GetChildNodes(NodeType.FieldSeparator, true);
Node picEnd = fieldSeps[0];
// Move to document start and insert mergefield
builder.MoveTo(picEnd);
builder.InsertField(mergefield, string.Empty);
// Insert end of Includepicture
picEnd.ParentNode.InsertBefore(new Run(doc, end), picEnd);
Thank you, It’s good, but now when I try to open a word document, my picture (INCLUDEPICTURE) is not visible (with a red cross).
I press F9 to update, and ALT+F9 to change the format of view, but no change.
I must to close and open the document to see the picture.
Have you got informations ?
PS : Aspose.word version 3.5.3.0
Thank you, It’s good, but now when I try to open a word document, my picture (INCLUDEPICTURE) is not visible (with a red cross).
I press F9 to update, and ALT+F9 to change the format of view, but no change.
I must to close and open the document to see the picture.
Hi
Thanks for your request. This can occur because path is not correct. For example if you insert field using the following code you will not see the picture:
Hi
Thanks for your inquiry. Please try using the following code:
builder.InsertField("MERGEFIELD TableStart:myTable", "«TableStart:myTable»");
string start = "INCLUDEPICTURE \"C:\\\\Temp\\\\";
string end = ".jpg\"";
string mergefield = "MERGEFIELD idprof";
builder.InsertField(start, string.Empty);
// Search for field separatop of Includepicture field
NodeCollection fieldSeps = builder.CurrentParagraph.GetChildNodes(NodeType.FieldSeparator, true);
Node picSep = null;
foreach (FieldSeparator sep in fieldSeps)
{
if (sep.FieldType == FieldType.FieldIncludePicture)
{
picSep = sep;
break;
}
}
// Move to field separator and insert mergefield
builder.MoveTo(picSep);
builder.InsertField(mergefield, string.Empty);
// Insert end of Includepicture
picSep.ParentNode.InsertBefore(new Run(doc, end), picSep);
// Search for field end
Node picEnd = picSep;
while (picEnd.NodeType != NodeType.FieldEnd)
{
picEnd = picEnd.NextSibling;
}
// Insert empty run after field end
Run empty = new Run(doc);
picEnd.ParentNode.InsertAfter(empty, picEnd);
// Move DocumentBuidler cursor to the empry run
builder.MoveTo(empty);
// Insert another mergefield
builder.InsertField("MERGEFIELD TableEnd:myTable", "«TableEnd:myTable»");