How to Split document by BookMarks

Technical support for Spire.Doc

Moderator: iceblue support

How to Split document by BookMarks

Postby sserra » Fri Jun 30, 2017 3:39 pm

Hi,
if somebody know how to split document by bookmarks
sserra
 
Posts: 10
Joined: Tue Jun 13, 2017 4:03 pm

Re: How to Split document by BookMarks

Postby Betsy.jiang » Mon Jul 03, 2017 8:20 am

Dear sserra,

Sorry for late reply as weekend.
Here is sample code for your kind reference.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"F:\testing\doc form\original document\10993.docx");

            Document newWord = new Document();
            Section section = newWord.AddSection();
            int index = 0;       
                for (int a = 0; a < doc.Sections.Count; a++)
                {
                    Section sec = doc.Sections[a];
                    for (int c = 0; c < sec.Body.ChildObjects.Count; c++)
                    {
                        DocumentObject obj = sec.Body.ChildObjects[c];
                        if (obj is Paragraph)
                        {
                            Paragraph para = obj as Paragraph;
                            section.Body.ChildObjects.Add(para.Clone());
                            for (int d = 0; d < para.ChildObjects.Count; d++)
                            {
                                DocumentObject parobj = para.ChildObjects[d];
                                if (parobj is BookmarkEnd && !(parobj as BookmarkEnd).Name.Contains("_GoBack"))
                                {
                                       int i = para.ChildObjects.IndexOf(parobj);
                                        newWord.SaveToFile(String.Format("10993out-{0}.docx", index), FileFormat.Docx);
                                        index++;

                                        newWord = new Document();
                                        section = newWord.AddSection();
                                        section.Body.ChildObjects.Add(para.Clone());
                                        if (section.Paragraphs[0].ChildObjects.Count == 0)
                                        {
                                            section.Body.ChildObjects.RemoveAt(0);
                                        }
                                        else
                                        {
                                            while (i >= 0)
                                            {
                                                section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                                i--;
                                            }
                                        }
                                }
                            }
                        }
                        if (obj is Table)
                        {
                            section.Body.ChildObjects.Add(obj.Clone());
                        }
                    }
                }
                newWord.SaveToFile(String.Format("10993out-{0}.docx", index), FileFormat.Docx);

Hope this helps. If this doesn't work, please provide us with the input document so that we could know the structure of your document. Then we will investigate it and provide corresponding code for you.

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1505
Joined: Tue Sep 06, 2016 8:30 am

Re: How to Split document by BookMarks

Postby Betsy.jiang » Fri Jul 07, 2017 8:36 am

Dear sserra,

Did you try the code I provided ? Did it solve your issue ?
Could you please give us some feedback ?

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1505
Joined: Tue Sep 06, 2016 8:30 am

Re: How to Split document by BookMarks

Postby sserra » Mon Jul 10, 2017 2:09 pm

Hi, no yet.
Thank you for your response.

now I use my Solution
each my document has specific bookmark.
Your example a lot bigger that my.
Is my approach wrong?

foreach (Section item in doc.Sections)
{
using (Document newWord = new Document())
{
newWord.Sections.Add(item.Clone());
DocFile = newWord.Bookmarks[0].Name + ".pdf";
PdfFileName = DocFile;
DocFile = Path.Combine(dir, DocFile);

newWord.SaveToFile(DocFile, FileFormat.PDF);
}

}
sserra
 
Posts: 10
Joined: Tue Jun 13, 2017 4:03 pm

Re: How to Split document by BookMarks

Postby Betsy.jiang » Tue Jul 11, 2017 2:56 am

Dear sserra,

Thanks for your feedback.
I checked the code you provided and found it is used to split document by Sections instead of bookmarks.
Did the code I provided work for you ? if not, could you please provide us with the input file so that we could clearly know the structure of your document?

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1505
Joined: Tue Sep 06, 2016 8:30 am

Re: How to Split document by BookMarks

Postby sserra » Tue Jul 11, 2017 11:18 pm

just tested your code.
please see attachment
inside original doc files and splited files by your code and my.

in your code issue with Header and Footer. it is not copy to new doc.

thank you
Last edited by sserra on Tue Jul 18, 2017 7:44 pm, edited 1 time in total.
sserra
 
Posts: 10
Joined: Tue Jun 13, 2017 4:03 pm

Re: How to Split document by BookMarks

Postby Betsy.jiang » Wed Jul 12, 2017 2:41 am

Dear sserra,

Thanks for your feedback.
I checked the input file you provided, and found it has several sections, and every section just contains one specific bookmark. So the code you were using and the code I provided both can split the document exactly.

With the code I provided, please add following code when adding a new section to new document, then the header/footer will not be lost.
Code: Select all
            ...
            Document newWord = new Document();
            Section section = newWord.AddSection();

            List<DocumentObject> HeaderObj = new List<DocumentObject>();
            List<DocumentObject> FooterObj = new List<DocumentObject>();

            foreach (Section sec in doc.Sections)
            {
                if (!sec.HeadersFooters.IsEmpty)
                {
                    foreach(DocumentObject objH in sec.HeadersFooters.Header.ChildObjects)
                    {
                        HeaderObj.Add(objH.Clone());
                    }
                    foreach (DocumentObject objH in sec.HeadersFooters.Footer.ChildObjects)
                    {
                        FooterObj.Add(objH.Clone());
                    }
                    break;
                }
            }
            foreach (DocumentObject Ho in HeaderObj)
            {
                section.HeadersFooters.Header.ChildObjects.Add(Ho.Clone());
            }
            foreach (DocumentObject Fo in FooterObj)
            {
                section.HeadersFooters.Header.ChildObjects.Add(Fo.Clone());
            }
            int index = 0;
            ...
                                        newWord = new Document();
                                        section = newWord.AddSection();
                                        foreach (DocumentObject Ho in HeaderObj)
                                        {
                                            section.HeadersFooters.Header.ChildObjects.Add(Ho.Clone());
                                        }
                                        foreach (DocumentObject Fo in FooterObj)
                                        {
                                            section.HeadersFooters.Footer.ChildObjects.Add(Fo.Clone());
                                        }

                                        section.Body.ChildObjects.Add(para.Clone());
           ...

If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
Last edited by Betsy.jiang on Thu Jul 13, 2017 1:30 am, edited 1 time in total.
User avatar
Betsy.jiang
 
Posts: 1505
Joined: Tue Sep 06, 2016 8:30 am

Re: How to Split document by BookMarks

Postby sserra » Wed Jul 12, 2017 4:48 pm

thank you, works perfect
only small change

foreach (DocumentObject Fo in FooterObj)
{
section.HeadersFooters.Header.ChildObjects.Add(Fo.Clone());
}

not Header, it should be Footer
sserra
 
Posts: 10
Joined: Tue Jun 13, 2017 4:03 pm

Re: How to Split document by BookMarks

Postby Betsy.jiang » Thu Jul 13, 2017 1:27 am

Dear sserra,

Sorry for my carelessness.
Please feel free to contact us if there is any question.

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1505
Joined: Tue Sep 06, 2016 8:30 am


Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 0 guests