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.

Sat Mar 29, 2014 3:17 pm

Good-morning,

We are currently using a different component to handle mail merges in a Word doc. It has been working ok, but there are issues and we are evaluating alternative components.

One of the primary scenarios that we are trying to address is merging content in a manner that will suppress blank lines and/or spaces. The typical objective is merging address information. For example:
<First Name><Last Name><Designations>
<Organization>
<Department>
<Address line 1>
<Address line 2>
<City><State><Zip Code>
<Country>

Objective 1
If the data being merged for fields like Organization and Department do not exist, the blank line needs to be removed/suppressed so the address appears without the extra blank lines.

Objective 2
Furthermore, ideally when the designations is added there would be a way to specify directly in the Word doc if a comma would be added*.
Note: Word field options support "Text to be inserted before:" and "Text to be inserted after":, but I am not sure if the Spire.Doc respects those field options.

Questions:
1. Can Spire.Doc facilitate the process of suppressing blank lines/spaces?
2. Can Spire.Doc facilitate the process of adding text depending if data exists (does it support the Word field options)?
3. If yes to either of the questions above, can you provide a brief description of how it would be handled?

Regards,
Minasu

minasu
 
Posts: 1
Joined: Sat Mar 29, 2014 2:54 pm

Mon Mar 31, 2014 10:00 am

Hello,

Sorry for late reply as weekend.
Thanks for your inquiry.
Regarding your issue, you could refer to the below method to achieve it.
Code: Select all
Document document = new Document();
document.LoadFromFile("test.docx");
string[] filedNames = new string[]{"Contact Name","Fax","Date"};
string[] filedValues = new string[]{"","+1 (69) 123456",System.DateTime.Now.Date.ToString()};
document.MailMerge.HideEmptyParagraphs = true;//suppress blank lines/spaces
document.MailMerge.Execute(filedNames, filedValues);
document.SaveToFile("Sample.doc", FileFormat.Doc);

//Text to be inserted after
static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
        {
            MergeField mergefield = args.CurrentMergeField as MergeField;
            if (mergefield.FieldName == "designations ")
            {
                mergefield.TextAfter = ",";
            }
        }

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Thu Apr 03, 2014 9:38 am

Hello,

Has the issue been resolved? Could you please give us some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Aug 27, 2014 11:43 pm

I have a similar issue but I'm not using MailMerge. We have a set of bookmarks, some in simple paragraphs such as the original post, and some inside tables.

When we used Interop, we were able to supress empty lines as well as empty rows in tables. However, that doesn't happen with Spire.Doc the way we are using it. I tried DeleteBookmarkContext as well as docs.Bookmarks.Remove(bookmark) and all this seems to do is remove the actual bookmark (and text) but not get rid of the line or cell (I guess the paragraph).

So, how do I go about suppressing empty bookmarks entirely, getting rid of the line (assuming it is the only thing on that line)?

bobradu
 
Posts: 9
Joined: Wed Aug 20, 2014 7:06 pm

Thu Aug 28, 2014 6:35 am

Hello,

Thanks for your inquiry.
Those methods you tried only delete the bookmark(and text), please refer the following method to suppress empty bookmarks entirely and getting rid of the line.
Code: Select all
 Paragraph p1 = doc.Bookmarks["name"].BookmarkStart.OwnerParagraph;
if (doc.Sections[0].HeadersFooters.Header.ChildObjects.IndexOf(p1) > -1)
            {
                if (p1.IsInCell)
                {
                    p1.Owner.Owner.Owner.ChildObjects.Remove(p1.Owner.Owner);
                }
                else
                {
                    doc.Sections[0].HeadersFooters.Header.ChildObjects.Remove(p1);
                }
            }
            else if (doc.Sections[0].HeadersFooters.Footer.ChildObjects.IndexOf(p1) > -1)
            {
                if (p1.IsInCell)
                {
                    p1.Owner.Owner.Owner.ChildObjects.Remove(p1.Owner.Owner);
                }
                else
                {
                    doc.Sections[0].HeadersFooters.Footer.ChildObjects.Remove(p1);
                }
            }
            else if (p1 != null)
            {
                if (p1.IsInCell)
                {
                    p1.Owner.Owner.Owner.ChildObjects.Remove(p1.Owner.Owner);
                }
                else
                {
                    doc.Sections[0].Body.ChildObjects.Remove(p1);
                }
            }

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Tue Sep 02, 2014 9:04 am

Hello,

Has the issue been resolved? Could you please give us some feedback if convenience?

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Sep 08, 2014 9:17 am

Thank you for this. However, the requirements have changed so we no longer have to suppress empty bookmarks. I will add this code to our project, however, because that may change.

bobradu
 
Posts: 9
Joined: Wed Aug 20, 2014 7:06 pm

Tue Sep 09, 2014 1:19 am

Hello,

Thanks for your response.

If there are any questions, welcome to get it back to us.

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc