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 30, 2017 3:39 pm

Hi,
if somebody know how to split document by bookmarks

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

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: 3099
Joined: Tue Sep 06, 2016 8:30 am

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: 3099
Joined: Tue Sep 06, 2016 8:30 am

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: 76
Joined: Tue Jun 13, 2017 4:03 pm

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: 3099
Joined: Tue Sep 06, 2016 8:30 am

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: 76
Joined: Tue Jun 13, 2017 4:03 pm

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: 3099
Joined: Tue Sep 06, 2016 8:30 am

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: 76
Joined: Tue Jun 13, 2017 4:03 pm

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: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Sep 05, 2018 11:16 pm

Hi, i have issue again with split document by bookmarks.
When i split doc with bookmark, and save to pdf, it loosing Headers and footers


thank you

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

Thu Sep 06, 2018 4:56 am

Dear sserra,

Thanks for your inquiry.
After analyzing your word document, I found that the header in your document is the FirstPageHeader, you need to use the FirstPageHeader property to get it rather than Header property. Please modify your code as below. If there is any question, welcome to write back.
Code: Select all
public void SplitDoc_SpireDoc()
{
    Document newWord = new Document();

    Section section = newWord.AddSection();
    List<DocumentObject> HeaderObj = new List<DocumentObject>();
    List<DocumentObject> FooterObj = new List<DocumentObject>();
    //Select the DifferentFirstPage option
    section.PageSetup.DifferentFirstPageHeaderFooter = true;
    foreach (Section sec in this.SPD.Sections)
    {
        if (!sec.HeadersFooters.IsEmpty)
        {
            foreach (DocumentObject objH in sec.HeadersFooters.FirstPageHeader.ChildObjects)
                HeaderObj.Add(objH.Clone());
            foreach (DocumentObject objH in sec.HeadersFooters.FirstPageFooter.ChildObjects)
                FooterObj.Add(objH.Clone());
            break;
        }
    }
    foreach (DocumentObject Ho in HeaderObj)
        section.HeadersFooters.FirstPageHeader.ChildObjects.Add(Ho.Clone());
    foreach (DocumentObject Fo in FooterObj)
        section.HeadersFooters.FirstPageFooter.ChildObjects.Add(Fo.Clone());

    int index = 0;
    string DocFile = "";

    foreach (Section sec in this.SPD.Sections)
    {
        foreach (DocumentObject obj in sec.Body.ChildObjects)
        {
            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("uLSF_"))
                    {
                        DocFile = @"d:\\" + ((BookmarkEnd)parobj).Name + ".pdf";

                        int i = para.ChildObjects.IndexOf(parobj);
                        newWord.SaveToFile(DocFile, FileFormat.PDF);
                        index++;
                        newWord = new Document();
                        section = newWord.AddSection();
                        section.PageSetup.DifferentFirstPageHeaderFooter = true;
                        foreach (DocumentObject Ho in HeaderObj)
                            section.HeadersFooters.FirstPageHeader.ChildObjects.Add(Ho.Clone());
                        foreach (DocumentObject Fo in FooterObj)
                            section.HeadersFooters.FirstPageFooter.ChildObjects.Add(Fo.Clone());

                        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());
        }
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

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

Thu Sep 06, 2018 2:45 pm

thank you, that works, but still has some issues
1) please look file uLSF_6209295_2780258_245919_UN_2.pdf - it has extra space between header and "BOD", that doc file does not.
2) please another doc file "6209854_11-59-54.doc" and related Pdf "uLSF_6209854_2780037_4076364_PO_1.pdf".
In pdf file image is cut.

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

Fri Sep 07, 2018 7:46 am

Dear sserra,

Thanks for your feedback.
1) Actually, in your document, there is a LineBreak element after the BookmarkEnd. And after executing the section.Body.ChildObjects.Add(para.Clone()) command, the LineBreak is cloned in the new document. That's why you see the extra space between header and BOD. For your document, you could add below code to remove the break.
Code: Select all
...
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--;
    }
    //Remove the break
    for (int c = 0; c < section.Paragraphs[0].ChildObjects.Count; c++)
    {
        if (section.Paragraphs[0].ChildObjects[c] is Break)
        {
            section.Paragraphs[0].ChildObjects.Remove(section.Paragraphs[0].ChildObjects[c]);
        }
    }
}
...

2) The margin of your document is (36,36,36,36), but the margin of the created word is default (20,50,20,50), different margins led to the image cropping issue. To avoid it, please add below code to keep the same margin.
Code: Select all
public void SplitDoc_SpireDoc()
{
    Document newWord = new Document();
    Section section = newWord.AddSection();
    //Keep the same size
    section.PageSetup.PageSize = this.SPD.Sections[0].PageSetup.PageSize;
    //Keep the same margin
    section.PageSetup.Margins = this.SPD.Sections[0].PageSetup.Margins;
...

Sincerely,
Nina
E-iceblue support team
User avatar

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

Fri Sep 07, 2018 3:51 pm

Hi Nina
thank you for your detail response

1) I found another issue with this document, doc has image in footer and pdf does not.

Also, i think it's to many code and params that only for split document. Is it possible to do request to your develop team about make it more easier? Have new method, like SplitBy Bookmark and this method will copy all params and settings.

thank you

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

Mon Sep 10, 2018 6:24 am

Hi,

Thanks for your feedback and sorry for late reply as weekend.
Please note that the footer in your file (6209854_11-59-54.doc) is not FirstPageFooter, hence, using FirstPageFooter property to get the footer is unsuitable. Please use below code to get all header/footer in different situations. If there is any question, welcome to write back. Besides, since different document structures may need different solutions, I'm afraid that our Dev team will not encapsulate a method of splitting by bookmark. Hope you can understand.
Code: Select all
public void SplitDoc_SpireDoc()
{
    Document newWord = new Document();
    Section section = newWord.AddSection();
    //Keep the same size
    section.PageSetup.PageSize = this.SPD.Sections[0].PageSetup.PageSize;
    //Keep the same margin
    section.PageSetup.Margins = this.SPD.Sections[0].PageSetup.Margins;
    //keep the same header/footer distance
    section.PageSetup.HeaderDistance = this.SPD.Sections[0].PageSetup.HeaderDistance;
    section.PageSetup.FooterDistance = this.SPD.Sections[0].PageSetup.FooterDistance;
    //Select the same header/footer option
    section.PageSetup.DifferentFirstPageHeaderFooter = this.SPD.Sections[0].PageSetup.DifferentFirstPageHeaderFooter;
    section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = this.SPD.Sections[0].PageSetup.DifferentOddAndEvenPagesHeaderFooter;
    //Clone header and footer
    for (int i = 0; i < 6; i++)
    {
        foreach (DocumentObject item in this.SPD.Sections[0].HeadersFooters[i].ChildObjects)
        {
            section.HeadersFooters[i].ChildObjects.Add(item.Clone());
        }
    }

    int index = 0;
    string DocFile = "";
    foreach (Section sec in this.SPD.Sections)
    {
        foreach (DocumentObject obj in sec.Body.ChildObjects)
        {
            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("uLSF_"))
                    {
                        DocFile = "d:\\" + ((BookmarkEnd)parobj).Name + ".pdf";

                        int i = para.ChildObjects.IndexOf(parobj);
                        newWord.SaveToFile(DocFile, FileFormat.PDF);
                        index++;
                        newWord = new Document();
                        section = newWord.AddSection();

                        section.PageSetup.PageSize = sec.PageSetup.PageSize;
                        section.PageSetup.Margins = sec.PageSetup.Margins;

                        section.PageSetup.HeaderDistance = sec.PageSetup.HeaderDistance;
                        section.PageSetup.FooterDistance = sec.PageSetup.FooterDistance;

                        section.PageSetup.DifferentFirstPageHeaderFooter = sec.PageSetup.DifferentFirstPageHeaderFooter;
                        section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = sec.PageSetup.DifferentOddAndEvenPagesHeaderFooter;

                        //Clone header and footer
                        for (int j = 0; j < 6; j++)
                        {
                            foreach (DocumentObject item in sec.HeadersFooters[j].ChildObjects)
                            {
                                section.HeadersFooters[j].ChildObjects.Add(item.Clone());
                            }
                        }

                        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--;
                            }
                            for (int c = 0; c < section.Paragraphs[0].ChildObjects.Count; c++)
                            {
                                if (section.Paragraphs[0].ChildObjects[c] is Break)
                                {
                                    section.Paragraphs[0].ChildObjects.Remove(section.Paragraphs[0].ChildObjects[c]);
                                }
                            }
                        }
                    }
                }
            }
            if (obj is Table) section.Body.ChildObjects.Add(obj.Clone());

        }
    }
}

Sincerely,
Nina
E-iceblue support team
User avatar

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

Return to Spire.Doc

cron