why am i getting e.FieldValue == null in ImageFieldMerging Method?
if I do it in FieldMerging i’m getting a correct value
source is DataTable, doc.MailMerge.Execute(table);
class HandleMergeImageField : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
// Do nothing.
if (args.FieldName.EndsWith("_img"))
{
DocumentBuilder _b = new(args.Document);
_b.MoveToMergeField(args.FieldName);
Shape _shape = _b.InsertImage((byte[])args.FieldValue);
_shape.Height = 100;
_shape.Width = 250;
}
}
// /
// / This is called when mail merge engine encounters Image:XXX merge field in the document.
// / You have a chance to return an Image object, file name or a stream that contains the image.
// /
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
MemoryStream stream = (MemoryStream)e.FieldValue;
if (stream!=null)
{
stream.Position = 0;
// Now the mail merge engine will retrieve the image from the stream.
e.ImageStream = stream;
}
}
}
@erikg You should use merge field with a special name Image:FieldName and used image path, image bytes or image url as a value of FieldName. For example see the following code and template: in.docx (12.2 KB)
string[] fieldNames = new string[] { "FromUrl", "FromPath", "FromBytes" };
object[] fieldValues = new object[] {
@"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png",
@"C:\Temp\aspose-logo.png",
File.ReadAllBytes(@"C:\Temp\aspose-logo.png")
};
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");
i think my field names are correct. i’m using datatable as a datasource and in the “cell” value tried to put local path and bytes, but i always get null value
@erikg Unfortunately, I cannot reproduce the problem on my side. I Have used the following simple code for testing (Template is the same as I have attached in my previous answer).
string[] fieldNames = new string[] { "FromUrl", "FromPath", "FromBytes" };
object[] fieldValues = new object[] {
@"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png",
@"C:\Temp\aspose-logo.png",
File.ReadAllBytes(@"C:\Temp\aspose-logo.png")
};
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new TestFieldMergingCallback();
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");
it’s working now. i had also named fields in datatable “Image:…”
another Q: is it possible to "declare method public void ImageFieldMerging as async Task
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.