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.

Wed Jun 08, 2016 6:27 am

When I upload a word document,I want to find such as "Background" in the document,I can use codes as following.

TextSelection textSelectBackground = document.FindString("Background", false, true);

But I want to get the whole paragraph from the "Background" to the next title.

The word document sample is as following:

One. Background
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXX(these are the texts)
Two.Analysis
XXXXXXXXXXXXXXXX

How can I get these texts? Hope to give me an example to see. Thank you.

dim_best
 
Posts: 12
Joined: Wed Jun 08, 2016 6:17 am

Wed Jun 08, 2016 7:05 am

Hi,

Thanks for your posting.
Please try the two lines:
Code: Select all
  TextSelection textSelectBackground = document.FindString("Background", false, true);
            Paragraph paragraph= textSelectBackground.GetAsOneRange(true).OwnerParagraph;


Best Regards,
Amy
E-iceblue support team
User avatar

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

Wed Jun 08, 2016 7:30 am

I use this code and get the paragraph.text.It returns "Background",but I want to get the text below the Background.I think they are different paragraph.So how can I get it?Thank you
Last edited by dim_best on Wed Jun 08, 2016 7:49 am, edited 1 time in total.

dim_best
 
Posts: 12
Joined: Wed Jun 08, 2016 6:17 am

Wed Jun 08, 2016 7:34 am

Hi,

Please provide us your word document so that the code we will provide can resolve your issue.
Thank you.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Wed Jun 08, 2016 8:13 am

this is the example.At the bottom of the word document.There is a table.I also want to get the title and then get the value of each line in the table,so how can I get it?

dim_best
 
Posts: 12
Joined: Wed Jun 08, 2016 6:17 am

Wed Jun 08, 2016 8:53 am

Hi,

Do you want to get each cell's value of the last table after "八、项目负责人及主要研究人员"?
If so, here is an example to get the value of first cell of first table. Maybe you can refer to it to finish your scenario.
Code: Select all
 Table table = document.Sections[0].Tables[0] as Table;
            TableCell cell=table.Rows[0].Cells[0];
            string value = null;
            for (int i = 0; i < cell.Paragraphs.Count; i++)
            {
                value += cell.Paragraphs[i].Text;
            }

If I misunderstand your requirements, please give us further explanation. The better is to do some marks in document.
And please also tell us what is the title you want to get?
Thank you for further assistance.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Wed Jun 08, 2016 9:02 am

I want to get all the title in the document.For example "立项背景" when the program read the "立项背景",it can get the text "随着公司的快速发展,科技项目不断增多,科技成果不断涌现,科技信息日新月异,企业转向科技型,知识型,企业员工...." all this paragraph.And when the program read the "国内外研究概况及可行性分析", it can read the "目前报表系统化软件,国内的开发研究水平已经成熟...."

dim_best
 
Posts: 12
Joined: Wed Jun 08, 2016 6:17 am

Wed Jun 08, 2016 9:33 am

Hi,

Please refer to the following code.
Code: Select all
  Document document = new Document();
            document.LoadFromFile("..\\..\\input\\7816\\中港疏浚立项申请书(科技管理系统).docx");
            TextSelection selection1 = document.FindString("一、立项背景", false, true);
            TextSelection selection2 = document.FindString("二、国内外研究概况及可行性分析", false, true);
            Paragraph paragraph1 = selection1.GetAsOneRange(false).OwnerParagraph;
            Paragraph paragraph2 = selection2.GetAsOneRange(false).OwnerParagraph;
            Body body = paragraph1.OwnerTextBody;
            int index1 = body.ChildObjects.IndexOf(paragraph1);
            int index2 = body.ChildObjects.IndexOf(paragraph2);

            string text = null;
            for (int i = index1; i < index2; i++)
            {
                Paragraph expectedParagraph = body.ChildObjects[i] as Paragraph;
                text += expectedParagraph.Text;
            }


Best Regards,
Amy
E-iceblue support team
User avatar

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

Thu Jun 09, 2016 8:45 am

Hi,

Does the above solution meet your requirement?
Please feel free to contact us if you need further assistance.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Sun Jun 12, 2016 7:05 am

Thank you for your help.It really meets my requirement.But I don't know why these codes can help.Could you tell me the meaning of these sentences? Especially GetAsOneRange,OwnerTextBody,ChildObjects
Paragraph paragraph1 = selection1.GetAsOneRange(false).OwnerParagraph;
Paragraph paragraph2 = selection2.GetAsOneRange(false).OwnerParagraph;
Body body = paragraph1.OwnerTextBody;
int index1 = body.ChildObjects.IndexOf(paragraph1);
int index2 = body.ChildObjects.IndexOf(paragraph2);

dim_best
 
Posts: 12
Joined: Wed Jun 08, 2016 6:17 am

Mon Jun 13, 2016 2:42 am

Hi,

Code: Select all
        //selection1.GetAsOneRange(false) is to get textRange that selection1 in
            //selection1.GetAsOneRange(false).OwnerParagraph is to get paragraph that selection1 in
            Paragraph paragraph1 = selection1.GetAsOneRange(false).OwnerParagraph;
            //Get text body that paragraph1 in
            Body body = paragraph1.OwnerTextBody;
            //Get the index of paragraph1 in its text body
            int index1 = body.ChildObjects.IndexOf(paragraph1);


Hope the above explanation can help you. Please tell us if you need further assistance.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Mon Jun 13, 2016 5:09 am

Thank you very much.If I need further information,I will still call for help.

dim_best
 
Posts: 12
Joined: Wed Jun 08, 2016 6:17 am

Thu Sep 01, 2016 3:56 pm

Mission accomplished. It's good =)

OvelagahFoelo
 
Posts: 2
Joined: Wed Dec 05, 2012 7:49 am

Fri Sep 02, 2016 1:41 am

Dear OvelagahFoelo,

Glad to hear that news.
Please feel free to contact us if there is any question or needs.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc