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.