示例#1
0
        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;
        }
示例#2
0
        public void TestInnerTableCellsDetection()
        {
            HWPFDocument hwpfDocument = new HWPFDocument(POIDataSamples
                    .GetDocumentInstance().OpenResourceAsStream("innertable.doc"));
            hwpfDocument.GetRange();

            Range documentRange = hwpfDocument.GetRange();
            Paragraph startOfInnerTable = documentRange.GetParagraph(6);

            Table innerTable = documentRange.GetTable(startOfInnerTable);
            Assert.AreEqual(2, innerTable.NumRows);

            TableRow tableRow = innerTable.GetRow(0);
            Assert.AreEqual(2, tableRow.NumCells());
        }
示例#3
0
 public string Parse()
 {
     if (!File.Exists(Context.Path))
         throw new FileNotFoundException("File " + Context.Path + " is not found");
     StringBuilder sb = new StringBuilder();
     using (FileStream stream = File.OpenRead(Context.Path))
     {
         HWPFDocument worddoc = new HWPFDocument(stream);
         return worddoc.GetRange().Text;
     }
 }
示例#4
0
文件: Program.cs 项目: 89sos98/npoi
        static void Main(string[] args)
        {
            // POI apparently can't create a document from scratch,
            // so we need an existing empty dummy document
            POIFSFileSystem fs = new POIFSFileSystem(File.OpenRead("empty.doc"));
            HWPFDocument doc = new HWPFDocument(fs);

            // centered paragraph with large font size
            Range range = doc.GetRange();
            CharacterRun run1 = range.InsertAfter("one");
            //par1.SetSpacingAfter(200);
            //par1.SetJustification((byte)1);
            // justification: 0=left, 1=center, 2=right, 3=left and right

            //CharacterRun run1 = par1.InsertAfter("one");
            run1.SetFontSize(2 * 18);
            // font size: twice the point size

            // paragraph with bold typeface
            Paragraph par2 = run1.InsertAfter(new ParagraphProperties(), 0);
            par2.SetSpacingAfter(200);
            CharacterRun run2 = par2.InsertAfter("two two two two two two two two two two two two two");
            run2.SetBold(true);

            // paragraph with italic typeface and a line indent in the first line
            Paragraph par3 = run2.InsertAfter(new ParagraphProperties(), 0);
            par3.SetFirstLineIndent(200);
            par3.SetSpacingAfter(200);
            CharacterRun run3 = par3.InsertAfter("three three three three three three three three three "
                + "three three three three three three three three three three three three three three "
                + "three three three three three three three three three three three three three three");
            run3.SetItalic(true);

            // add a custom document property (needs POI 3.5; POI 3.2 doesn't save custom properties)
            DocumentSummaryInformation dsi = doc.DocumentSummaryInformation;
            CustomProperties cp = dsi.CustomProperties;
            if (cp == null)
                cp = new CustomProperties();
            cp.Put("myProperty", "foo bar baz");

            doc.Write(File.OpenWrite("new-hwpf-file.doc"));
        }