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 Feb 05, 2020 3:13 am

Summary
I am having issues extracting data from a textbox within a Word document. This document has been converted from PDF and I did trial Spire.PDF, but had even less luck with extracting information. All other text, not in text boxes, is able to be extracted with the below code.

Code: Select all
Document document = new Document();
document.LoadFromFile(@".\19. Super Simple Plant-Based by Running on real food_Min.docx");
Section S = document.Sections[2];
Spire.Doc.Collections.ParagraphCollection Array = S.Paragraphs;
foreach (Paragraph P in Array)
{
   Console.WriteLine(P.Text);
}


Context
My end goal is to build middleware to extract this recipe data, in a usable, identifiable manner (either from DOCX or PDF - Both attached) and create a recipe class to store information such as Ingredients, Instructions, Times, Nutritional information etc, to later use in API integration.

Attempts
Being fairly new to the Spire library (I've only started using it for this project), I've spent a lot of time looking at the documentation and examples provided to get the information I need.
I welcome any constructive feedback to my code structure, or efficiencies.

This does actually produce the output for the ingredients section, however it makes it difficult to identify the parts that I need.
Code: Select all
Document document = new Document();
document.LoadFromFile(@".\19. Super Simple Plant-Based by Running on real food_Min.docx");
Console.WriteLine(document.GetText());


This was a hopeful attempt, but actually doesn't produce any output.
Code: Select all
Document document = new Document();
document.LoadFromFile(@".\19. Super Simple Plant-Based by Running on real food_Min.docx");

Spire.Doc.Collections.TextBoxCollection TBS = document.TextBoxes;

StringBuilder sb = new StringBuilder();
Loop through the paragraphs of the table and extract text to a .txt file
foreach (TextBox TB in TBS)
{
    foreach (Table table in TB.Body.Tables)
    {
        foreach (TableRow row in table.Rows)
        {
            foreach (TableCell cell in row.Cells)
            {
                foreach (Paragraph paragraph in cell.Paragraphs)
                {
                    sb.AppendLine(paragraph.Text);
                }
            }
        }
    }
}
Console.WriteLine(sb.ToString());


I got this attempt from
Code: Select all
https://www.e-iceblue.com/tag/extract-text-word.html

However it only appears to extract
MARINATED CHICKPEAS RECIPE

Code: Select all
Document document = new Document();
document.LoadFromFile(@".\19. Super Simple Plant-Based by Running on real food_Min.docx");
if (document.TextBoxes.Count > 0)
{
   using (StreamWriter sw = File.CreateText(@"C:\temp\result.txt"))
   {
      foreach (Section section in document.Sections)
      {
         foreach (Paragraph p in section.Paragraphs)
         {
            foreach (DocumentObject obj in p.ChildObjects)
            {
               if (obj.DocumentObjectType == DocumentObjectType.TextBox)
               {
                  TextBox textbox = obj as TextBox;
                  foreach (DocumentObject objt in textbox.ChildObjects)
                  {
                     if (objt.DocumentObjectType == DocumentObjectType.Paragraph)
                     {
                        sw.Write((objt as Paragraph).Text);
                     }

                     if (objt.DocumentObjectType == DocumentObjectType.Table)
                     {
                        Table table = objt as Table;
                        ExtractTextFromTables(table, sw);
                     }
                  }
               }
            }
         }
      }
   }
}


Any assistance would be appreciated and I have attached the DOC and PDF version of this for reference.

marck007
 
Posts: 2
Joined: Tue Feb 04, 2020 10:37 pm

Wed Feb 05, 2020 8:35 am

Hi,

Thanks for your inquiry.
After analyzing your document, I found the textboxes you want are placed in ShapeGroup. So your code didn't work. Please refer to following code to extract text from those textboxes.
Code: Select all
            Document document = new Document();
            document.LoadFromFile(FilePath + @"19. Super Simple Plant-Based by Running on real food_Min.docx");
            StringBuilder sb = new StringBuilder();

            foreach (Section section in document.Sections)
            {
                foreach (Paragraph para in section.Paragraphs)
                {
                    foreach (DocumentObject obj in para.ChildObjects)
                    {
                        if(obj.DocumentObjectType == DocumentObjectType.ShapeGroup)
                        {
                            foreach (DocumentObject sobj in (obj as ShapeGroup).ChildObjects)
                            {
                                if (sobj is TextBox)
                                {
                                    TextBox tb = sobj as TextBox;
                                    foreach (DocumentObject tobj in tb.ChildObjects)
                                    {
                                        if (tobj is Paragraph)
                                        {
                                            Paragraph tpara = tobj as Paragraph;
                                            sb.Append(tpara.ListText + tpara.Text + "\r\n");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            File.WriteAllText("20441.txt",sb.ToString());


Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Thu Feb 06, 2020 10:24 pm

That's worked, thank you so much for your prompt assistance!

marck007
 
Posts: 2
Joined: Tue Feb 04, 2020 10:37 pm

Fri Feb 07, 2020 1:49 am

Hi,

Thanks for your feedback.
If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Return to Spire.Doc

cron