Пример #1
0
 public XWPFTable(CT_Tbl table, IBody part)
 {
     this.part      = part;
     this.ctTbl     = table;
     this.tableRows = new List <XWPFTableRow>();
     if (table.SizeOfTrArray() == 0)
     {
         this.CreateEmptyTable(table);
     }
     foreach (CT_Row tr in (IEnumerable <CT_Row>)table.GetTrList())
     {
         StringBuilder stringBuilder = new StringBuilder();
         tr.Table = table;
         this.tableRows.Add(new XWPFTableRow(tr, this));
         foreach (CT_Tc tc in (IEnumerable <CT_Tc>)tr.GetTcList())
         {
             tc.TableRow = tr;
             foreach (CT_P prgrph in (IEnumerable <CT_P>)tc.GetPList())
             {
                 XWPFParagraph xwpfParagraph = new XWPFParagraph(prgrph, part);
                 if (stringBuilder.Length > 0)
                 {
                     stringBuilder.Append('\t');
                 }
                 stringBuilder.Append(xwpfParagraph.GetText());
             }
         }
         if (stringBuilder.Length > 0)
         {
             this.text.Append((object)stringBuilder);
             this.text.Append('\n');
         }
     }
 }
Пример #2
0
        public XWPFTable(CT_Tbl table, IBody part)
        {
            this.part = part;
            this.ctTbl = table;

            tableRows = new List<XWPFTableRow>();
            // is an empty table: I add one row and one column as default
            if (table.SizeOfTrArray() == 0)
                CreateEmptyTable(table);

            foreach (CT_Row row in table.GetTrList()) {
                StringBuilder rowText = new StringBuilder();
                row.Table = table;
                XWPFTableRow tabRow = new XWPFTableRow(row, this);
                tableRows.Add(tabRow);
                foreach (CT_Tc cell in row.GetTcList()) {
                    cell.TableRow = row;
                    foreach (CT_P ctp in cell.GetPList()) {
                        XWPFParagraph p = new XWPFParagraph(ctp, part);
                        if (rowText.Length > 0) {
                            rowText.Append('\t');
                        }
                        rowText.Append(p.GetText());
                    }
                }
                if (rowText.Length > 0) {
                    this.text.Append(rowText);
                    this.text.Append('\n');
                }
            }
        }
Пример #3
0
        public XWPFComment(CT_Comment comment, XWPFDocument document)
        {
            text = new StringBuilder();
            id = comment.id.ToString();
            author = comment.author;

            foreach(CT_P ctp in comment.GetPList())
            {
                XWPFParagraph p = new XWPFParagraph(ctp, document);
                text.Append(p.GetText());
            }
        }
Пример #4
0
        public XWPFComment(CT_Comment comment, XWPFDocument document)
        {
            text   = new StringBuilder();
            id     = comment.id.ToString();
            author = comment.author;

            foreach (CT_P ctp in comment.GetPList())
            {
                XWPFParagraph p = new XWPFParagraph(ctp, document);
                text.Append(p.GetText());
            }
        }
Пример #5
0
        public void TestHeaderParagraph()
        {
            XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx");

            XWPFHeader hdr = xml.GetHeaderFooterPolicy().GetDefaultHeader();

            Assert.IsNotNull(hdr);

            IList <XWPFParagraph> ps = hdr.Paragraphs;

            Assert.AreEqual(1, ps.Count);
            XWPFParagraph p = ps[(0)];

            Assert.AreEqual(5, p.GetCTP().GetRList().Count);
            Assert.AreEqual("First header column!\tMid header\tRight header!", p.GetText());
        }
Пример #6
0
        public void TestBookmarks()
        {
            XWPFDocument  doc       = XWPFTestDataSamples.OpenSampleDocument("bookmarks.docx");
            XWPFParagraph paragraph = doc.Paragraphs[0];

            Assert.AreEqual("Sample Word Document", paragraph.GetText());
            Assert.AreEqual(1, paragraph.GetCTP().SizeOfBookmarkStartArray());
            Assert.AreEqual(0, paragraph.GetCTP().SizeOfBookmarkEndArray());
            CT_Bookmark ctBookmark = paragraph.GetCTP().GetBookmarkStartArray(0);

            Assert.AreEqual("poi", ctBookmark.name);
            foreach (CT_Bookmark bookmark in paragraph.GetCTP().GetBookmarkStartList())
            {
                Assert.AreEqual("poi", bookmark.name);
            }
        }
Пример #7
0
        public void TestSearchTextFound()
        {
            XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx");

            IList <XWPFParagraph> ps = xml.Paragraphs;

            Assert.AreEqual(10, ps.Count);

            XWPFParagraph p = ps[(0)];

            TextSegement segment = p.SearchText("sample word document", new PositionInParagraph());

            Assert.IsNotNull(segment);

            Assert.AreEqual("sample word document", p.GetText(segment));

            Assert.IsTrue(p.RemoveRun(0));
        }
Пример #8
0
        public XWPFTable(CT_Tbl table, IBody part)
        {
            this.part  = part;
            this.ctTbl = table;

            tableRows = new List <XWPFTableRow>();
            // is an empty table: I add one row and one column as default
            if (table.SizeOfTrArray() == 0)
            {
                CreateEmptyTable(table);
            }

            foreach (CT_Row row in table.GetTrList())
            {
                StringBuilder rowText = new StringBuilder();
                row.Table = table;
                XWPFTableRow tabRow = new XWPFTableRow(row, this);
                tableRows.Add(tabRow);
                foreach (CT_Tc cell in row.GetTcList())
                {
                    cell.TableRow = row;
                    foreach (CT_P ctp in cell.GetPList())
                    {
                        XWPFParagraph p = new XWPFParagraph(ctp, part);
                        if (rowText.Length > 0)
                        {
                            rowText.Append('\t');
                        }
                        rowText.Append(p.GetText());
                    }
                }
                if (rowText.Length > 0)
                {
                    this.text.Append(rowText);
                    this.text.Append('\n');
                }
            }
        }
Пример #9
0
        public void TestExisting()
        {
            XWPFDocument  doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx");
            XWPFParagraph p;
            XWPFRun       run;


            // First paragraph is simple
            p = doc.GetParagraphArray(0);
            Assert.AreEqual("This is a test document.", p.GetText());
            Assert.AreEqual(2, p.GetRuns().Count);

            run = p.GetRuns()[0];
            Assert.AreEqual("This is a test document", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.GetRuns()[1];
            Assert.AreEqual(".", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);


            // Next paragraph is all in one style, but a different one
            p = doc.GetParagraphArray(1);
            Assert.AreEqual("This bit is in bold and italic", p.GetText());
            Assert.AreEqual(1, p.GetRuns().Count);

            run = p.GetRuns()[0];
            Assert.AreEqual("This bit is in bold and italic", run.ToString());
            Assert.AreEqual(true, run.IsBold());
            Assert.AreEqual(true, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(true, run.GetCTR().rPr.IsSetB());
            Assert.AreEqual(false, run.GetCTR().rPr.b.IsSetVal());


            // Back to normal
            p = doc.GetParagraphArray(2);
            Assert.AreEqual("Back to normal", p.GetText());
            Assert.AreEqual(1, p.GetRuns().Count);

            run = p.GetRuns()[(0)];
            Assert.AreEqual("Back to normal", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);


            // Different styles in one paragraph
            p = doc.GetParagraphArray(3);
            Assert.AreEqual("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.GetText());
            Assert.AreEqual(11, p.GetRuns().Count);

            run = p.GetRuns()[(0)];
            Assert.AreEqual("This contains ", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.GetRuns()[(1)];
            Assert.AreEqual("BOLD", run.ToString());
            Assert.AreEqual(true, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());

            run = p.GetRuns()[2];
            Assert.AreEqual(", ", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.GetRuns()[(3)];
            Assert.AreEqual("ITALIC", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(true, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());

            run = p.GetRuns()[(4)];
            Assert.AreEqual(" and ", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.GetRuns()[(5)];
            Assert.AreEqual("BOTH", run.ToString());
            Assert.AreEqual(true, run.IsBold());
            Assert.AreEqual(true, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());

            run = p.GetRuns()[(6)];
            Assert.AreEqual(", as well as ", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.GetRuns()[(7)];
            Assert.AreEqual("RED", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());

            run = p.GetRuns()[(8)];
            Assert.AreEqual(" and ", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.GetRuns()[(9)];
            Assert.AreEqual("YELLOW", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());

            run = p.GetRuns()[(10)];
            Assert.AreEqual(" text.", run.ToString());
            Assert.AreEqual(false, run.IsBold());
            Assert.AreEqual(false, run.IsItalic());
            Assert.AreEqual(false, run.IsStrike());
            Assert.AreEqual(null, run.GetCTR().rPr);
        }
Пример #10
0
        public void TestExisting()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx");
            XWPFParagraph p;
            XWPFRun run;


            // First paragraph is simple
            p = doc.GetParagraphArray(0);
            Assert.AreEqual("This is a test document.", p.GetText());
            Assert.AreEqual(2, p.Runs.Count);

            run = p.Runs[0];
            Assert.AreEqual("This is a test document", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.Runs[1];
            Assert.AreEqual(".", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);


            // Next paragraph is all in one style, but a different one
            p = doc.GetParagraphArray(1);
            Assert.AreEqual("This bit is in bold and italic", p.GetText());
            Assert.AreEqual(1, p.Runs.Count);

            run = p.Runs[0];
            Assert.AreEqual("This bit is in bold and italic", run.ToString());
            Assert.AreEqual(true, run.IsBold);
            Assert.AreEqual(true, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(true, run.GetCTR().rPr.IsSetB());
            Assert.AreEqual(false, run.GetCTR().rPr.b.IsSetVal());


            // Back to normal
            p = doc.GetParagraphArray(2);
            Assert.AreEqual("Back to normal", p.GetText());
            Assert.AreEqual(1, p.Runs.Count);

            run = p.Runs[(0)];
            Assert.AreEqual("Back to normal", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);


            // Different styles in one paragraph
            p = doc.GetParagraphArray(3);
            Assert.AreEqual("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.GetText());
            Assert.AreEqual(11, p.Runs.Count);

            run = p.Runs[(0)];
            Assert.AreEqual("This contains ", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.Runs[(1)];
            Assert.AreEqual("BOLD", run.ToString());
            Assert.AreEqual(true, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);

            run = p.Runs[2];
            Assert.AreEqual(", ", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.Runs[(3)];
            Assert.AreEqual("ITALIC", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(true, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);

            run = p.Runs[(4)];
            Assert.AreEqual(" and ", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.Runs[(5)];
            Assert.AreEqual("BOTH", run.ToString());
            Assert.AreEqual(true, run.IsBold);
            Assert.AreEqual(true, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);

            run = p.Runs[(6)];
            Assert.AreEqual(", as well as ", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.Runs[(7)];
            Assert.AreEqual("RED", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);

            run = p.Runs[(8)];
            Assert.AreEqual(" and ", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);

            run = p.Runs[(9)];
            Assert.AreEqual("YELLOW", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);

            run = p.Runs[(10)];
            Assert.AreEqual(" text.", run.ToString());
            Assert.AreEqual(false, run.IsBold);
            Assert.AreEqual(false, run.IsItalic);
            Assert.AreEqual(false, run.IsStrike);
            Assert.AreEqual(null, run.GetCTR().rPr);
        }