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.

Tue Feb 20, 2018 10:04 pm

I am doing a mail merge. The template has 5 "Paragraphs" and I want to make 1 master doc with many records. I am currently using:

masterDoc.Sections.Add(templateDoc.Sections.FirstItem.Clone());

This works, but it inserts a page break between each section. How can I add sections without page breaks?

Thanks
Ray

vbisbest
 
Posts: 2
Joined: Tue Feb 20, 2018 3:27 pm

Wed Feb 21, 2018 2:36 am

Hello Ray,

Thanks for your inquiry.
When adding a section, it is normal to create a page break. To avoid creating page breaks, please directly add the elements of templateDoc into source section of masterDoc. Here are sample code for reference.
Code: Select all
 foreach (DocumentObject e in templateDoc.Sections[0].Body.ChildObjects)
            {
                masterDoc.Sections[0].Body.ChildObjects.Add(e.Clone());
            }


Sincerely,
Amy
E-iceblue support team
User avatar

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

Wed Feb 21, 2018 10:15 am

This worked perfectly, thank you! However I have 1 more challenge. The merge doc's lines are actually "Paragraphs" and I need to keep the whole section together. Here is the code I have that does not work:

Paragraph p = masterDoc.Sections[0].AddParagraph();
p.Format.KeepLines = true;

foreach (DocumentObject e in templateDoc.Sections[0].Body.ChildObjects)
{
p.ChildObjects.Add(e.Clone());

}

masterDoc.Sections[0].Body.ChildObjects.Add(p);

I get "Index out of bounds" on the p.ChildObjects.Add(e.Clone()) call. Any ideas on this one?

vbisbest
 
Posts: 2
Joined: Tue Feb 20, 2018 3:27 pm

Thu Feb 22, 2018 1:57 am

Hello Ray,

Please use the following code.
Code: Select all
//in the case, templateDoc.Sections[0].Body.ChildObjects are some paragraphs.
foreach (DocumentObject e in templateDoc.Sections[0].Body.ChildObjects)
            {
                  //get the child objects of current paragraph
                foreach(DocumentObject obj in e.ChildObjects)
                {
                    //append each child object to the target paragraph
                    p.ChildObjects.Add(obj.Clone());
                }
            }

Sincerely,
Amy
E-iceblue support team
User avatar

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

Mon Feb 26, 2018 6:58 am

Hello Ray,

How is your issue now? Your feedback would be greatly appreciated.

Sincerely,
Amy
E-iceblue support team
User avatar

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

Return to Spire.Doc