Hi !
I want to add 2 fields (INCLUDEPICTURE and MERGEFIELD)
Exemple :
{INCLUDEPICTURE “Directory\{MERGEFIELD idprof}.jpg”}
If I insert fields with DocumentBuilder, the “MERGEFIELD” field is not considered.
builder.InsertField(“INCLUDEPICTURE “Directory\{MERGEFIELD idprof}.jpg””, string.Empty);
How Can I perform this ?
Thxs
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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.
Hope this helps.
Best regards.
Thank you, but I used Apose.word in version 3.5.3.0.
InsertField return void
and InsertNode doesn’t exists
If you’ve got another solution, I search also
Thxs
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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);
Hope this helps.
Best regards.
Hi !
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
Hi !
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 ?
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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:
builder.InsertField("INCLUDEPICTURE \"C:\\Temp\\test.jpg\"", string.Empty);
But if you insert field using the following code it will work fine:
builder.InsertField("INCLUDEPICTURE \"C:\\\\Temp\\\\test.jpg\"", string.Empty);
(I highlighted differences)
Also please attach your output document for testing.
Best regards.
Yes thank you very much !!!
And now if I want to add
{MERGEFIELD TableStart}
{INCLUDEPICTURE “C:\\Temp\\{MERGEFIELD idprof}.jpg”}
{MERGEFIELD TableEnd}
I try this, but error… :
builder.InsertField(“MERGEFIELD TableStart”, string.Empty);
string start = “INCLUDEPICTURE “C:\\Temp\\”;
string end = “.jpg””;
string mergefield = “MERGEFIELD idprof”;
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);
builder.InsertField(“MERGEFIELD TableEnd”, string.Empty);
Thxs
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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»");
Hope this helps.
Best regards,
FieldType is not defined for sep
Excuse me for all questions …
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks for your inquiry. Please try using the following snippet of code:
Node picSep = null;
Node picEnd = null;
foreach (FieldSeparator sep in fieldSeps)
{
picSep = sep;
picEnd = picSep;
//Search for field end
while (picEnd.NodeType != NodeType.FieldEnd)
{
picEnd = picEnd.NextSibling;
}
if ((picEnd as FieldEnd).FieldType == FieldType.FieldIncludePicture)
{
break;
}
}
Hope this helps.
Best regards.
Thank you, you’re the best