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 Jan 16, 2020 3:40 pm

Hello,

I'm new to Spire.Doc and we are thinking about migrating from Aspose. But I have encountered an issue, which is crucial for us.
I have attached a sample template so you can recreate the issue:
testing.zip


What we want to achieve is to have complex IF merge tags, e.g.
Code: Select all
{IF {mergefiel} = "True" "output a layouted table with other mergefields inside" ""}
In this case the output document is empty.

There also seems to be some kind of bug even with just some multiline output, e.g.
Code: Select all
{IF {mergefiel} = "True" "output a multiline text" ""}
Some parts of the output are beeing repeted at the beginning of the conditional mergefield.

Here is the sample code:

Code: Select all
Document document = new Document();
document.LoadFromFile(@"test.docx");
string[] fieldNames = { "Amount_open__c", "Insured_By_Heidelpay__c", "Invoice_Number__c" };
string[] fieldValues { "101,75", "False", "i97807-Test" };
document.MailMerge.ClearFields = true;
document.MailMerge.HideEmptyParagraphs = true;
document.MailMerge.Execute(fieldNames, fieldValues);
document.SaveToFile("test.pdf", FileFormat.PDF);


And not related, but also a support question: Are \b and \f switches like {MERGEFIELD field \b "some text: "} allowed in the mergefield? In my tests it didn't work.

mrgaijin
 
Posts: 5
Joined: Thu Jan 02, 2020 6:01 pm

Fri Jan 17, 2020 7:31 am

Dear Daniel,

Thanks for your inquiry. Below are my answers to your questions.
I did a test with the latest Spire.Doc Pack Version:8.1.
Question 1: In this case the output document is empty.
I didn't found that the layouted table lost, please see my generated PDF. If you were using an old version, please give the latest one a try. If the issue still happens after trying, please provide your testing environment, such as OS information (E.g. Windows 7, 64bit) and Region setting (E.g. China, Chinese) for further investigation.

Question 2: There also seems to be some kind of bug even with just some multiline output...
I indeed found that some text were repeated after doing mailmerge. The issue has been logged into our Bug tracking system, if it is fixed or there is any update, I will inform you. Apologize for the inconvenience caused.

Question 3: Are \b and \f switches like {MERGEFIELD field \b "some text: "} allowed in the mergefield?
Yes, the switches \b \f are allowed in merge field. I didn't find any switches in your test.docx. Hence, I made a sample word file and tested with your code, the switches could generated in PDF successfully. Attached my documents for your reference. I suggested that you use the latest version to try a again. If there is any misunderstanding, please feel free to write back.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Fri Jan 17, 2020 8:26 am

Hello,

thanks for your reply. I was using Spire.Office 4.1, I downloaded the newest version (both Spire.Office 5.1 and Spire.Doc 8.1) to check the results. Deleted the dll from my sample project, and referenced the new ones in different versions. But the result is the same, although the flags "\b" and "\f" are working now, so question 3 is settled.

I am using the netcore3.0/netcore3.1 version, is it possible that this function is not supported yet for .NET core? My environment is Windows 10 Home x64 with German de-DE settings.

mrgaijin
 
Posts: 5
Joined: Thu Jan 02, 2020 6:01 pm

Fri Jan 17, 2020 8:39 am

Hello again,

I was testing something else in the meantime to check why custom fonts weren't beeing embedded in the output pdf (everything changed to the default Arial), so I changed a line of code and it worked, both the font embedding and question 1 (the table inside the IF merge field). Question 2 is still a bug though.

So I'm not sure if there is some kind of bug with the standard PDF settings or something else when using:

Code: Select all
document.SaveToFile("Sample.pdf", FileFormat.PDF);


I changed it to the following and it worked:

Code: Select all
                ToPdfParameterList parms = new ToPdfParameterList()
                {
                    IsEmbeddedAllFonts = true
                };

                document.SaveToFile("Sample.pdf", parms);

mrgaijin
 
Posts: 5
Joined: Thu Jan 02, 2020 6:01 pm

Fri Jan 17, 2020 10:07 am

Hi,

Thank your for sharing information.
I tested your code in .NET Core 3.0/3.1 application with your same environment (Germany/German ), but still didn't reproduce the question 1(the table inside the IF merge field). To help us investigate your issue further, please save your document to a Word document after doing mailmerge, then send it to us. And to verify if the problem is related to the NET Core app, please also try your code in a .NET Framework app, then tell us your result. Thanks for your assistance.
Code: Select all
document.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx2013);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Fri Jan 17, 2020 11:19 am

Hi, I've tried now saving as DOCX and everything works fine, only saving directly as PDF seems to have some bugs:
Sample.zip


The result is the same with .NET core 3.1 and .NET Framework 4.6

Code: Select all
document.SaveToFile("Sample.docx", FileFormat.Docx2013);

Everything fine

Code: Select all
document.SaveToFile("Sample.pdf", FileFormat.PDF);

Bug nr. 1: Table inside IF merge field is ignored (see workaround with ToPdfParameterList in my previous post)
Bug nr. 2: Merge field with {... \b "Referenz: " } ist beeing merged as "Referenz: " although the value is empty. Expected behaviour: when the value is empty, do not merge anything.

PS. And by the way, is there any way to define the size of an image in a merge field? Something like {MERGEFIELD Image(widthxheight):fieldname }
But I can open a different post for this part if you want.

mrgaijin
 
Posts: 5
Joined: Thu Jan 02, 2020 6:01 pm

Sun Jan 19, 2020 6:50 am

Dear Daniel,

Thanks for your feedback and sorry for the late reply as weekend.
1) Bug nr. 1: Table inside IF merge field is ignored (see workaround with ToPdfParameterList in my previous post):
It is strange, I still can't reproduce this issue. The Table inside IF field can be generated successfully. I also did tests in several other computers, but without success to reproduce it on my side. Here I uploaded an .exe application, please directly run it on your side and tell us your result.

2) Bug nr. 2: Merge field with {... \b "Referenz: " } ist beeing merged as "Referenz: " although the value is empty. Expected behaviour: when the value is empty, do not merge anything.
I have reproduced the issue and logged it into our bug tracking system. If it is fixed or there is any update, I will inform you immediately.

3) Is there any way to define the size of an image in a merge field?
Yes, please try the following code to change the size of image.
Code: Select all
...
//change image size in MergeImageField event
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);
document.MailMerge.Execute(fieldNames, fieldValues);
...
 private static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)
 {
     string filePath = field.FieldValue as string;
     if (!string.IsNullOrEmpty(filePath))
     {
         field.Image = Image.FromFile(filePath);
         //change image size
         field.PictureSize = new SizeF(50, 50);
     }
 }
Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Tue Jan 21, 2020 10:58 am

Hi, thanks for the test programm, it worked. It seems like I had an extra line of code
Code: Select all
document.IsUpdateFields = true;
which had this effect, sorry for not reporting it.
This means, it works ok now, thanks for your time.

But as you saw in my other questions there are still some bugs with the implementation of those functions, which we really need. We have to rely on them, that they work correctly.

As for the image function, thanks for the input. Is there a way to define the size parameteres in the word template itself? Or do I have to implement a custom logic to integrate the size params in the merge field of the template?

mrgaijin
 
Posts: 5
Joined: Thu Jan 02, 2020 6:01 pm

Wed Jan 22, 2020 9:14 am

Hi,

Glad to hear from you that the first issue has been eliminated.
In regard to the other two issues, our Dev team are working on them. If there is any progress, I will inform you immediately.
As for changing the size of image, it is not available to define the size parameteres in the word template itself. Using our Spire.Doc, you can change the image size in MergeImageField event when doing mailmerge. Also you can change the image size after doing mailmerge. See the sample code below. Any question, please feel free to write back.
Code: Select all
...
 document.MailMerge.Execute(fieldNames, fieldValues);
 foreach (Section section in document.Sections)
 {
     foreach (DocumentObject obj in section.Body.ChildObjects)
     {
         if (obj is Paragraph)
         {
             foreach (DocumentObject cobj in (obj as Paragraph).ChildObjects)
             {
                 //Find picture and change its size
                 if (cobj is DocPicture)
                 {
                     DocPicture pic = cobj as DocPicture;
                     pic.Width = 50;
                     pic.Height = 50;
                 }
             }
         }
     }
 }
 document.SaveToFile("result.pdf", Spire.Doc.FileFormat.PDF);
Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Wed Sep 16, 2020 10:22 am

Hi,

Thanks for your patient waiting.
Glad to inform you that the reported issue (some text were repeated after doing mailmerge) has been resolved. Please download the hotfix from the following link.
Website link: Spire.Office Platinum(Hotfix) Version:5.9.2
Nuget link: https://www.nuget.org/packages/Spire.Office/5.9.2

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc