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.

Fri Jun 17, 2016 9:20 am

Hi.
We are generating a pdf document via a Mail Merge and are trying to figure out how we can hide entire rows of data (without displaying a blank row).

Our word docs consist purely of Ms Word table rows with 2 columns - one for the label and one for the data. The data field is a MergeField.
In the example below - if we wanted to hide the entire email row of data how could we do this?
Playing around with it - I have managed to hide the label on that row but it still displays the row. The row just contains no data. This results in a blank line displaying on the doc which we do not want. The mobile should display directly undernearh the last name if the email is empty.

Any idea how we can do this?
Thanks

EXAMPLE WORD DOC TEMPLATE DEFINITION
First Name: <<First_Name>>
Last Name: <<Last_Name>>
Email: <<Email>>
Mobile: <<Mobile>>


C# CODE THAT PERFORMS THE MERGE
Code: Select all
       
       private PdfDocument RunMailMerge(string docLocation, Tuple<DataSet, List<DictionaryEntry>> dsData)
        {
            try
            {
                Document document = new Document();
                document.LoadFromFile(docLocation);

                document.MailMerge.ExecuteWidthNestedRegion(dsData.Item1, dsData.Item2);

                Spire.Pdf.PdfDocument pdfdoc;
                using (MemoryStream stream = new MemoryStream())
                {
                    document.SaveToStream(stream, Spire.Doc.FileFormat.PDF);
                    document.Close();
                    pdfdoc = new Spire.Pdf.PdfDocument(stream);
                }

                return pdfdoc;
            }
            catch(Exception ex)
            {
                throw new InvalidOperationException("RunMailMerge error for document " + docLocation, ex);
            }
        }

brycare
 
Posts: 9
Joined: Fri Apr 15, 2016 1:31 pm

Fri Jun 17, 2016 9:42 am

Hi,

Thanks for your posting.
We will do some investigation and work out a solution for you, but we can give you an update on June 20 as we will have a weekend with two days. Sorry for the inconvenience.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Jun 20, 2016 11:31 am

Hi guys.

Is there any update on this?
Surely there is a way to conditionally exclude template fields and remove blank lines?

Thanks

brycare
 
Posts: 9
Joined: Fri Apr 15, 2016 1:31 pm

Tue Jun 21, 2016 3:26 am

Hi,

Sorry for the delayed update.
There is no ways to conditionally exclude template fields to remove blank lines, sorry. You have to remove the row contains the cell with blank data after filling data to merge fields.
Here is a solution for the reference.
Code: Select all
 
           //load template file
            Spire.Doc.Document document = new Spire.Doc.Document();
            document.LoadFromFile("..\\..\\input\\7904\\template7904.docx");
           
             //execute merge fields
            DataSet ds = new DataSet();
            ds.ReadXml("..\\..\\input\\7904\\dataTest.xml");
            List<DictionaryEntry> list = new List<DictionaryEntry>();
            DictionaryEntry dictionaryEntry = new DictionaryEntry("people", string.Empty);
            list.Add(dictionaryEntry);
            document.MailMerge.ExecuteWidthNestedRegion(ds, list);

            //remove the row has blank cell
            Table table = document.Sections[0].Tables[0] as Spire.Doc.Table;
            for (int i = 0; i < table.Rows.Count;i++ )
            {
                if (String.IsNullOrEmpty(table.Rows[i].Cells[1].Paragraphs[0].Text))
                {
                    table.Rows.Remove(table.Rows[i]);
                }
            }
           
           //save to pdf document
            document.SaveToFile("7904.pdf", FileFormat.PDF);

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Jun 24, 2016 7:55 am

Hi,

Did the above solution help you?
Thanks for your feedback in advance.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc