Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jun 20, 2024 12:57 pm

We have a problem in our project with rendering images.
The data for the image is contained in a dataset.
In Debug-Mode I can see that in my dataSet the byteArray with the imageData is filled.
But in the MailMerge_MergeImageField-Function the field.FieldValue is empty.
Below is the code we use.

Code: Select all
       
private static Document FillTemplateFromCore(byte[] template, DataSet dataSet)
        {
            //...

            document.MailMerge.ClearFields = true;
            document.MailMerge.HideEmptyParagraphs = true;
            document.MailMerge.MergeField += MailMerge_MergeField;
            document.MailMerge.MergeImageField += MailMerge_MergeImageField;

            //Executing Mailmerge for Multiple Datasets so that Multiple Repeat Table in Target Object are Possible
            var dataTableRelationShips = BuildRelationShipEntries(dataSet);
            document.MailMerge.ExecuteWidthNestedRegion(dataSet, dataTableRelationShips);

            return document;

            void MailMerge_MergeField(object sender, MergeFieldEventArgs field)
            {
                var CurrentMergeFieldOwner = field.CurrentMergeField.Owner.Clone();
                var FieldRowIndex = field.RowIndex;

                //Create Dictionaries to Prevent Null Exception Error
                if (!iteratedFieldnames.ContainsKey(CurrentMergeFieldOwner)) iteratedFieldnames.Add(CurrentMergeFieldOwner, new Dictionary<int, List<string>>());
                if (!iteratedFieldnames[CurrentMergeFieldOwner].ContainsKey(FieldRowIndex)) iteratedFieldnames[CurrentMergeFieldOwner].Add(FieldRowIndex, new List<string>());

                bool alreadyIterated = iteratedFieldnames[CurrentMergeFieldOwner][FieldRowIndex].Contains(field.FieldName);

                //The merge fields that expect HTML data should be marked with some prefix, for instance,'[Html]'.
                if (field.FieldValue is string && (field.FieldValue as string).StartsWith(HTMLPREFIX))
                {
                    if (!alreadyIterated)
                    {
                        var fieldWithoutPrefix = (field.FieldValue as string).Replace(HTMLPREFIX, String.Empty);
                        field.CurrentMergeField.OwnerParagraph.AppendHTML(fieldWithoutPrefix);

                        if (!iteratedFieldnames.ContainsKey(CurrentMergeFieldOwner)) throw new Exception(String.Format("Key '{0}' not Present in IteratedFieldnames", CurrentMergeFieldOwner));
                        if (!iteratedFieldnames[CurrentMergeFieldOwner].ContainsKey(FieldRowIndex)) throw new Exception(String.Format("Key '{0}' not Present in IteratedFieldnames[{1}]", FieldRowIndex, CurrentMergeFieldOwner));
                        iteratedFieldnames[CurrentMergeFieldOwner][FieldRowIndex].Add(field.FieldName);
                    }

                    //Needs to be set to Empty String in every Iteration otherwise Text will be shown
                    field.Text = "";
                }
                else if (field.FieldValue is DateTime)
                {
                    MergeField mergefield = field.CurrentMergeField as MergeField;

                    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("de-DE");
                    DateTime d = Convert.ToDateTime(field.Text);
                    string s = d.ToString(mergefield.DateFormat, culture);
                    field.Text = s;
                }
                else if (field.FieldValue is Decimal)
                {
                    MergeField mergefield = field.CurrentMergeField as MergeField;

                    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
                    decimal d = Convert.ToDecimal(field.Text);
                    string s = d.ToString(mergefield.NumberFormat, culture);
                    field.Text = s;
                }
            }

            void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)
            {
                var CurrentMergeFieldOwner = field.CurrentMergeField.Owner.Clone();
                var FieldRowIndex = field.RowIndex;

                if (!iteratedFieldnames.ContainsKey(CurrentMergeFieldOwner)) iteratedFieldnames.Add(CurrentMergeFieldOwner, new Dictionary<int, List<string>>());
                if (!iteratedFieldnames[CurrentMergeFieldOwner].ContainsKey(FieldRowIndex)) iteratedFieldnames[CurrentMergeFieldOwner].Add(FieldRowIndex, new List<string>());

                if (field.FieldValue is byte[])
                {
                    var imageStream = new MemoryStream(field.FieldValue as byte[]);
                    field.SetImage(imageStream);
                }
            }
        }

        private static List<DictionaryEntry> BuildRelationShipEntries(DataSet dataSet, IEnumerable<MailMergeRelation> relationships)
        {
            List<DictionaryEntry> resultEntries = new List<DictionaryEntry>();

            IEnumerable<DataTable> dataTables = EnumerableOf(dataSet.Tables);

            foreach (var dataTable in dataTables.Where(tbl => !relationships.Any(rel => rel.childEntity == tbl.TableName)))
            {
                resultEntries.Add(new DictionaryEntry(dataTable.TableName, string.Empty));
            }

            foreach (var relationship in relationships)
            {
                var childTable = dataTables.SingleOrDefault(tbl => tbl.TableName == relationship.childEntity);
                if (childTable == null) throw new ArgumentException("childTable not found: " + relationship.childEntity);
                var childColumnIndex = childTable.Columns.IndexOf(relationship.childField);
                if (childColumnIndex == -1) throw new ArgumentException("chieldField not found: " + relationship.childField);
                var childColumn = childTable.Columns[childColumnIndex];

                var parentTable = dataTables.SingleOrDefault(tbl => tbl.TableName == relationship.parentEntity);
                if (parentTable == null) throw new ArgumentException("parentTable not found: " + relationship.parentEntity);
                var parentColumnIndex = parentTable.Columns.IndexOf(relationship.parentField);
                if (parentColumnIndex == -1) throw new ArgumentException("parentField not found: " + relationship.parentField);
                var parentColumn = parentTable.Columns[parentColumnIndex];

                resultEntries.Add(new DictionaryEntry(relationship.childEntity, string.Format("{0} = %{1}.{2}%", relationship.childField, relationship.parentEntity, relationship.parentField)));
            }
            return resultEntries;
        }

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Fri Jun 21, 2024 2:32 am

Hello,

Thanks for your inquiry.
Since you only provided some code snippets and lacked input files, we cannot debug directly. To help us further investigate your issue, please provide us with your complete test code and test files. Or you can provide us with a simplified project that can be run directly. You can upload them here as an attachment or send them to this email: [email protected]. Thanks in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Tue Jun 25, 2024 7:25 am

In the attachment i provide a sample Code with the necessary snippets from my project.
Also there is an example-Dataset in a form like the DataSet we use in our project.

Hopefully this helps to reproduce the Effect, that i only get empty images in my Word-Document.

ExampleCode.zip

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Tue Jun 25, 2024 9:04 am

Hello,

Thanks for your reply.
Sorry, we still can't run the latest code. You can provide us with your template file and the images to be merged, and we will try to provide you with a demo according to your needs. Thank you in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Tue Jun 25, 2024 10:22 am

Hello,

i added the files to a sample project including a Console-Application which runs the code.
I also included an example-Picture and a simple Template-File.

ExampleProject.zip


Thank you for your help,
Till

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Wed Jun 26, 2024 9:01 am

Hello,
The problem is extremely urgent for us as we are currently unable to provide our customers with a new version. When can we expect a solution?

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Wed Jun 26, 2024 9:59 am

Hello,

Thanks for your patience.
Kindly note that you need to add "document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);" event to merge pictures. I have modified your project and uploaded it to the attachment for your reference. If you have any other questions, please feel free to write back.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Wed Jun 26, 2024 11:30 am

Thank you for your awnser.
Unfortunately we don't have the images stored at certain filepaths. We load the data from a database and get the images as a byte-Array. Like i filled the dataset in my example-project.
Therefore your solution doesn't work in our environment.
Also the problem occured after the update from Spire.Doc Version 10.7.16 to 12.3.12.
Before the code worked without any issues and the images got rendered into the Document.

Sincerly,
Till

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Wed Jun 26, 2024 5:13 pm

Hello William,

do you have a solution for us? Again, it is extremly urgent for us to fix this error.
Thanks
Boris Schaffrin
CEO

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Thu Jun 27, 2024 1:58 am

Hello,

Thanks for your reply.
I rolled back my version to 10.7.16 and ran the project you provided. The result was no different from 12.3.12, the pictures were not merged.Can you confirm this for us? Regarding your scenario, here is another solution. When creating a datatable, specify the data type of the "Picture" column as byte[]. The following code snippet is for your reference. If you have any other questions, please feel free to write back.
Code: Select all
DataTable table1 = new DataTable("example");
table1.Columns.Add("Name");
//table1.Columns.Add("Picture");
table1.Columns.Add("Picture", typeof(byte[]));


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Thu Jun 27, 2024 12:35 pm

Hi,

we figured out that everything worked with version 9.7.14.
ExampleProject_9.7.14.zip

But with version 12.3.12 it doesn't work
ExampleProject_12.3.12.zip


In this project i only changed the version of Spire.Doc.
Also i added a third column with the picture as a base64-String. But the byte-Array would be preferred from us.

Regards,
Till

simplements
 
Posts: 11
Joined: Sun Aug 08, 2021 10:48 am

Fri Jun 28, 2024 2:32 am

Hello,

Thanks for more information.
After more comparison tests, there are indeed differences between the new and old versions when merging byte[] type images. I have logged this issue into our tracking system with the ticket SPIREDOC-10644. Our dev team will further investigate and fix this issue. Once there is any progress, I will inform you in time. Regarding your situation, we recommend that you use the "base64-String" solution first. Sorry for the inconvenience caused to you.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Fri Jul 05, 2024 3:13 am

Hello,

Thank you for your patience.
Glad to inform that we just released Spire.Doc Pack(hot fix) Version:12.7.3 which fixed the issue of SPIREDOC-10644, welcome to test it.
Website link: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget link: https://www.nuget.org/packages/Spire.Doc/

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc

cron