public ToxyDocument Parse()
        {
            if (!File.Exists(Context.Path))
            {
                throw new FileNotFoundException("File " + Context.Path + " is not found");
            }

            bool extractHeader = false;

            if (Context.Properties.ContainsKey("ExtractHeader"))
            {
                extractHeader = Utility.IsTrue(Context.Properties["ExtractHeader"]);
            }
            bool extractFooter = false;

            if (Context.Properties.ContainsKey("ExtractFooter"))
            {
                extractFooter = Utility.IsTrue(Context.Properties["ExtractFooter"]);
            }

            ToxyDocument rdoc = new ToxyDocument();


            using (FileStream stream = File.OpenRead(Context.Path))
            {
                HWPFDocument worddoc = new HWPFDocument(stream);
                if (extractHeader && worddoc.GetHeaderStoryRange() != null)
                {
                    StringBuilder sb = new StringBuilder();
                    rdoc.Header = worddoc.GetHeaderStoryRange().Text;
                }
                if (extractFooter && worddoc.GetFootnoteRange() != null)
                {
                    StringBuilder sb = new StringBuilder();
                    rdoc.Footer = worddoc.GetFootnoteRange().Text;
                }
                for (int i = 0; i < worddoc.GetRange().NumParagraphs; i++)
                {
                    Paragraph     para = worddoc.GetRange().GetParagraph(i);
                    string        text = para.Text;
                    ToxyParagraph p    = new ToxyParagraph();
                    p.Text = text;
                    //var runs = para.Runs;
                    p.StyleID = para.GetStyleIndex().ToString();

                    //for (int i = 0; i < runs.Count; i++)
                    //{
                    //    var run = runs[i];

                    //}
                    rdoc.Paragraphs.Add(p);
                }
            }
            return(rdoc);
        }
Beispiel #2
0
        protected void ProcessNoteAnchor(HWPFDocument doc, CharacterRun characterRun, XmlElement block)
        {
            {
                Notes footnotes = doc.GetFootnotes();
                int   noteIndex = footnotes.GetNoteIndexByAnchorPosition(characterRun.StartOffset);
                if (noteIndex != -1)
                {
                    Range footnoteRange       = doc.GetFootnoteRange();
                    int   rangeStartOffset    = footnoteRange.StartOffset;
                    int   noteTextStartOffset = footnotes.GetNoteTextStartOffSet(noteIndex);
                    int   noteTextEndOffset   = footnotes.GetNoteTextEndOffSet(noteIndex);

                    Range noteTextRange = new Range(rangeStartOffset + noteTextStartOffset, rangeStartOffset + noteTextEndOffset, doc);

                    ProcessFootnoteAutonumbered(doc, noteIndex, block, noteTextRange);
                    return;
                }
            }
            {
                Notes endnotes  = doc.GetEndnotes();
                int   noteIndex = endnotes.GetNoteIndexByAnchorPosition(characterRun.StartOffset);
                if (noteIndex != -1)
                {
                    Range endnoteRange        = doc.GetEndnoteRange();
                    int   rangeStartOffset    = endnoteRange.StartOffset;
                    int   noteTextStartOffset = endnotes.GetNoteTextStartOffSet(noteIndex);
                    int   noteTextEndOffset   = endnotes.GetNoteTextEndOffSet(noteIndex);

                    Range noteTextRange = new Range(rangeStartOffset + noteTextStartOffset, rangeStartOffset + noteTextEndOffset, doc);

                    ProcessEndnoteAutonumbered(doc, noteIndex, block, noteTextRange);
                    return;
                }
            }
            throw new NotImplementedException();
        }