Hello All,
I am new to C# i have problem while reading the document.Actually my requirement is i have word document in this format:
1 Purpose
2 Test Specification
2.1 Test Details
2.2 Test Description
2.3 Test Strategy
3 Results
4 Remarks
1 Purpose: this will contain info
2 Test Specification : this will contain info
2.1 Test Details :: this will contain info
2.2 Test Description: this will contain info
2.3 Test Strategy: this will contain info
3 Results: this will contain info
4 Remarks: this will contain info
my question is i want to get the text of each item,how can this be acheived in C#.Kindly help me
Thank you,
Sampath

Selva GanapathyRank #560 · 4.3K reputationPosted Sep 29, 2014, 7:09 AM EDT
Ziggy Ram2 reputationPosted Oct 2, 2014, 5:21 AM EDT
http://msdn.microsoft.com/en-US/library/vstudio/microsoft.office.tools.word
http://msdn.microsoft.com/library/vstudio/bb157891
I am currently implementing this feature in an internal-use application using leadtools OCR features that loads the MS Word DOC or DOX files as raster image, pass it to the OCR engine, recognize all the text in the rasterized image and then save the recognized text to any supported text or document format (including PDF, TXT, Doc, etc.). You can find more information here.
Marina Eberhardt24 reputationPosted Dec 2, 2014, 12:37 AM EST
I'm not sure but I presume your document is formatted in a way that those listed items represent your document's chapters and that they have a heading styled title (at least that is a common thing to do in order to get a nice Table of Content from them).
So what you can do is iterate through all document's paragraphs and look for the ones that have appropriate styling. For example "1 Purpose", "2 Test Specification", "3 Results" and "4 Remarks" will probably have the "heading 1" style, while the "2.1 Test Details", "2.2 Test Description" and "2.3 Test Strategy" will have the "heading 2" style. And the easiest way that comes to my mind is to simply retrieve the text content between the two heading styled paragraphs.
For example try the following (note that I used a C# component for processing word files instead of calling a part of an PIA from C# (aka Word Interop):
public sealed class DocumentChapterParser { private readonly DocumentModel document; private Paragraph currentChapter, currentSubChapter; private string currentContent; public DocumentChapterParser(string documentPath) { this.document = DocumentModel.Load(documentPath); this.currentChapter = this.currentSubChapter = null; this.currentContent = string.Empty; } public void ReadChapters() { foreach (var paragraph in this.document.GetChildElements(true, ElementType.Paragraph) .CastParagraph>()) { var content = paragraph.Content.ToString(); if (string.IsNullOrWhiteSpace(content)) continue; if (paragraph.ParagraphFormat.Style.Name.Equals("heading 1")) { this.WritePreviousBody(); this.currentChapter = paragraph; Console.WriteLine("Chapter Title: {0}", content); } else if (paragraph.ParagraphFormat.Style.Name.Equals("heading 2")) { this.WritePreviousBody(); this.currentSubChapter = paragraph; Console.WriteLine("Sub Chapter Title: {0}", content); } else if (this.currentSubChapter != null || this.currentChapter != null) this.currentContent += content; } this.WritePreviousBody(); } private void WritePreviousBody() { if (string.IsNullOrEmpty(this.currentContent)) return; // Previous sub chapter's body. if (this.currentSubChapter != null) { Console.WriteLine("Sub Chapter Body: {0}", this.currentContent); this.currentSubChapter = null; } // Previous chapter's body. else Console.WriteLine("Chapter Body: {0}", this.currentContent); this.currentContent = string.Empty; } }And here is how you can use it:
Also just to point out the above sample shows how to process a DOCX file format in C#, but note that the same solution can be applied for processing a DOC file format in C# as well.