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 Sep 18, 2015 1:54 pm

I am currently using word interop to copy the text on a page, paste the text into a new document, save the new document with an incremental name each time, and then delete the text on the page. I have uploaded the basic vb.net code for this.

Is it possible to do the same job using the Spire.Doc libraries? (I can work with C# if that's the best solution).

Any help would be appreciated. Thanks, Jez.

JezPearson
 
Posts: 10
Joined: Fri Sep 11, 2015 2:05 pm

Mon Sep 21, 2015 6:55 am

Hello,

Thanks for your inquiry.
Sorry that our Spire.Doc has no way to detect when pages starts and ends at present.
You can split your document based on section, e.g., one section from your original document= one new document.
If you accept the solution, please tell us and we will provide you sample code.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Mon Sep 21, 2015 7:42 am

Hi,

Yes, splitting by section would work for me.
Please supply the code sample.

Thanks,
Jez.

JezPearson
 
Posts: 10
Joined: Fri Sep 11, 2015 2:05 pm

Mon Sep 21, 2015 8:12 am

Hello Jez.,

Please refer to the below code snippet:
Code: Select all
Document original = new Document();
            original.LoadFromFile("Test.docx");
            Document newWord;
            for (int i = 0; i < original.Sections.Count; i++)
            {
                newWord = new Document();
                newWord.Sections.Add(original.Sections[i].Clone());
                newWord.SaveToFile(String.Format(@"test\out_{0}.doc", i));
            }


Best wishes,
Amy
E-iceblue support team
User avatar

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

Mon Sep 21, 2015 1:02 pm

Hi Amy,

The code works fine for most of my documents.

Some of my documents have a page break instead of a section break. Is it possible to do the same thing using page breaks instead of section breaks, or do I need to manipulate the document first to get the section breaks in?

Thanks,
Jez.

JezPearson
 
Posts: 10
Joined: Fri Sep 11, 2015 2:05 pm

Tue Sep 22, 2015 7:31 am

Hello Jez.,

Please use the below code snippet to manipulate the documents with page breaks.

Code: Select all
Document original = new Document();
            original.LoadFromFile("123.docx");
             
            Document newWord=new Document();
            Section section = newWord.AddSection();
            int index=0;
         
            foreach (Section sec in original.Sections)
            {
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph para = obj as Paragraph;
                        section.Body.ChildObjects.Add(para.Clone());

                        foreach (DocumentObject parobj in para.ChildObjects)
                        {
                            if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
                            {
                                int i = para.ChildObjects.IndexOf(parobj);

                                for (int j = i; j < para.ChildObjects.Count; j++)
                                {
                                    section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                }
                                newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx);
                             
                                index++;
                                newWord = new Document();
                                section = newWord.AddSection();
                                section.Body.ChildObjects.Add(para.Clone());

                                while (i >= 0)
                                {
                                    section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                    i--;
                                }
                                if (section.Paragraphs[0].ChildObjects.Count == 0)
                                {
                                    section.Body.ChildObjects.RemoveAt(0);
                                }
                            }

                        }
                    }
                    if (obj is Table)
                    {
                        section.Body.ChildObjects.Add(obj.Clone());
                    }
                }
            }
            newWord.SaveToFile(String.Format("result/out-{0}.docx",index),FileFormat.Docx);


Best wishes,
Amy
E-iceblue support team
User avatar

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

Wed Sep 23, 2015 6:40 am

Hello Jez.,

Has your problem been resolved?
Thanks for your feedback.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Wed Sep 23, 2015 3:19 pm

Hi Amy,

Yes, the libraries and the code do everything I need to do, and is a big improvement over using word interop.

Thanks for all your help.

Regards,
Jez.

JezPearson
 
Posts: 10
Joined: Fri Sep 11, 2015 2:05 pm

Thu Sep 24, 2015 1:28 am

Hello Jez.,

Thanks for your feedback. Please feel free to write to us if you have any problems.

Regards,
Amy
E-iceblue support team
User avatar

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

Thu Oct 01, 2020 10:51 pm

Hello there,

Say a document has single section & no page break.

But loading document showing 6 page count. Is it possible to split page by page separately without depending on Page Break?

It will appreciated if you can tell that How do you count Page number perfectly when we load document? can we use for page Split by that method?

Please help me out. Thanks in advance.

Ahmed_zamil
 
Posts: 1
Joined: Thu Oct 01, 2020 10:45 pm

Fri Oct 02, 2020 7:19 am

Hi,

Thanks for your inquiry.
If your document with 6 pages only has one section without page break, I am afraid there is no way to split page by page automatically.
Note MS Word document is flow document and does not contain any information about its layout into lines and pages. In our internal code, we count page numbers by calculating content in the document. It will be very complex to calculate the height of every element accurately, it depends on lots of things, such as the used font and where/how the paragraph content breaks. Unfortunately, there is no public API, which allows you to determine where page starts or ends. Hope you could understand.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Sat Oct 03, 2020 7:39 am

Hello Betsy,

I've a function to extract paragraphs:

private static Document ExtractParagraphsToNewdocument(Document sourceDocument, int startPara, int endPara)
{
Document document = new Document();
document.AddSection();
for (int i = startPara; i < endPara; i++)
{
//copy the content
document.Sections[0].Body.ChildObjects.Add(sourceDocument.Sections[0].Body.ChildObjects[i].Clone());

}

return document;
}

It works when my doc has only one section (sourceDocument.Sections[0].Body.ChildObjects[i].Clone()) but get error if there are multi sections. Is there any way to check section of startPara index and endPara index to split by index automatically? What should I do if start index in section 0 and end index in section 3?

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Mon Oct 05, 2020 3:08 am

Hi,

Thanks for your inquiry and sorry for the late reply as weekend.
Please refer to the following code to extract paragraphs based on index. If this is not what you want, please provide your source document and your desired output for a better investigation. You could attach your files here or send them to our email (support@e-iceblue.com).
Code: Select all
        private static Document ExtractParagraphsToNewdocument(Document sourceDocument)
        {
            Document document = new Document();
            document.AddSection();
            int startSection = 0;
            int endSection = 3;
            int startPara=0;
            int endPara = sourceDocument.Sections[endSection].Body.Count;
           
            for (int s = startSection; s <= endSection; s++)
            {
                Section section = sourceDocument.Sections[s];
                if (s == startSection)
                {
                    for (int i = startPara; i < sourceDocument.Sections[startSection].Body.Count; i++)
                    {
                        document.Sections[0].Body.ChildObjects.Add(sourceDocument.Sections[startSection].Body.ChildObjects[i].Clone());
                    }
                }
                else if (s == endSection)
                {
                    for (int i = 0; i < endPara; i++)
                    {
                        document.Sections[0].Body.ChildObjects.Add(sourceDocument.Sections[endSection].Body.ChildObjects[i].Clone());
                    }
                }
                else
                {
                    for (int i = 0; i < section.Body.Count; i++)
                    {
                        document.Sections[0].Body.ChildObjects.Add(section.Body.ChildObjects[i].Clone());
                    }
                }
               
            }
            return document;
        }

Sincerely,
Nina
E-iceblue support team
User avatar

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

Tue Oct 06, 2020 10:07 am

Thanks Nina,

How can I get index of section that contain a paragraph?

Paragraph para = TextSelection.GetAsOneRange().OwnerParagraph;
int index = para.Owner.ChildObjects.IndexOf(para);

--> I want to find which section containing the Paragraph ?

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Wed Oct 07, 2020 2:35 am

Hello,

Thanks for your reply.
Please refer to the following code snippet to get the section. Feel free to contact us if you have further questions.
Code: Select all
Section section = para.Owner.Owner as Section;


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc