Пример #1
0
        public void TestCheckboxes()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("checkboxes.docx");

            Console.WriteLine(doc);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.AreEqual("This is a small test for checkboxes \nunchecked: |_| \n" +
                            "Or checked: |X|\n\n\n\n\n" +
                            "Test a checkbox within a textbox: |_| -> |X|\n\n\n" +
                            "In Table:\n|_|\t|X|\n\n\n" +
                            "In Sequence:\n|X||_||X|\n", extractor.Text);
            extractor.Close();
        }
Пример #2
0
        public void TestAddPicture()
        {
            XWPFDocument  doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx");
            XWPFParagraph p   = doc.GetParagraphArray(2);
            XWPFRun       r   = p.Runs[0];

            Assert.AreEqual(0, doc.AllPictures.Count);
            Assert.AreEqual(0, r.GetEmbeddedPictures().Count);

            r.AddPicture(new MemoryStream(new byte[0]), (int)PictureType.JPEG, "test.jpg", 21, 32);

            Assert.AreEqual(1, doc.AllPictures.Count);
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
        }
Пример #3
0
        public void TestMetadataBasics()
        {
            XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("sample.docx");

            Assert.IsNotNull(xml.GetProperties().GetCoreProperties());
            Assert.IsNotNull(xml.GetProperties().GetExtendedProperties());

            Assert.AreEqual("Microsoft Office Word", xml.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Application);
            Assert.AreEqual(1315, xml.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Characters);
            Assert.AreEqual(10, xml.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Lines);

            Assert.AreEqual(null, xml.GetProperties().GetCoreProperties().GetTitle());
            Assert.AreEqual(null, xml.GetProperties().GetCoreProperties().GetUnderlyingProperties().GetSubjectProperty());
        }
Пример #4
0
        public void TestMetadataComplex()
        {
            XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("IllustrativeCases.docx");

            Assert.IsNotNull(xml.GetProperties().GetCoreProperties());
            Assert.IsNotNull(xml.GetProperties().GetExtendedProperties());

            Assert.AreEqual("Microsoft Office Outlook", xml.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Application);
            Assert.AreEqual(5184, xml.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Characters);
            Assert.AreEqual(0, xml.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Lines);

            Assert.AreEqual(" ", xml.GetProperties().GetCoreProperties().GetTitle());
            Assert.AreEqual(" ", xml.GetProperties().GetCoreProperties().GetUnderlyingProperties().GetSubjectProperty());
        }
Пример #5
0
        public void TestFile()
        {
            XWPFDocument      doc       = XWPFTestDataSamples.OpenSampleDocument("ExternalEntityInText.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            String text = extractor.Text;

            Assert.IsTrue(text.Length > 0);

            // Check contents, they should not contain the text from POI web site After colon!
            Assert.AreEqual("Here should not be the POI web site: \"\"", text.Trim());

            extractor.Close();
        }
Пример #6
0
        public void TestAddPicture()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx");

            byte[] jpeg       = XWPFTestDataSamples.GetImage("nature1.jpg");
            String relationId = doc.AddPictureData(jpeg, (int)PictureType.JPEG);

            byte[] newJpeg = ((XWPFPictureData)doc.GetRelationById(relationId)).GetData();
            Assert.AreEqual(newJpeg.Length, jpeg.Length);
            for (int i = 0; i < jpeg.Length; i++)
            {
                Assert.AreEqual(newJpeg[i], jpeg[i]);
            }
        }
Пример #7
0
        public void TestCompareAbstractNum()
        {
            XWPFDocument  doc       = XWPFTestDataSamples.OpenSampleDocument("Numbering.docx");
            XWPFNumbering numbering = doc.GetNumbering();
            int           numId     = 1;

            Assert.IsTrue(numbering.NumExist(numId.ToString()));
            XWPFNum         num                = numbering.GetNum(numId.ToString());
            string          abstrNumId         = num.GetCTNum().abstractNumId.val;
            XWPFAbstractNum abstractNum        = numbering.GetAbstractNum(abstrNumId);
            string          CompareAbstractNum = numbering.GetIdOfAbstractNum(abstractNum);

            Assert.AreEqual(abstrNumId, CompareAbstractNum);
        }
Пример #8
0
        public void Test61787_1()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("61787-1.docx");

            StringBuilder str = new StringBuilder();

            foreach (XWPFParagraph par in doc.Paragraphs)
            {
                str.Append(par.Text).Append("\n");
            }
            String s = str.ToString();

            Assert.IsFalse(s.Contains("This is another Test"));
        }
Пример #9
0
        public void Test61787()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("61787.docx");

            StringBuilder str = new StringBuilder();

            foreach (XWPFParagraph par in doc.Paragraphs)
            {
                str.Append(par.Text).Append("\n");
            }
            String s = str.ToString();

            Assert.IsTrue(s.Trim().Length > 0, "Having text: \n" + s + "\nTrimmed lenght: " + s.Trim().Length);
        }
Пример #10
0
        public void TestCellVerticalAlign()
        {
            XWPFDocument      docx   = XWPFTestDataSamples.OpenSampleDocument("59030.docx");
            IList <XWPFTable> tables = docx.Tables;

            Assert.AreEqual(1, tables.Count);
            XWPFTable           table     = tables[0];
            List <XWPFTableRow> tableRows = table.Rows;

            Assert.AreEqual(2, tableRows.Count);
            Assert.AreEqual(XWPFVertAlign.TOP, tableRows[0].GetCell(0).GetVerticalAlignment());
            Assert.AreEqual(XWPFVertAlign.BOTTOM, tableRows[0].GetCell(1).GetVerticalAlignment());
            Assert.AreEqual(XWPFVertAlign.CENTER, tableRows[1].GetCell(0).GetVerticalAlignment());
            Assert.AreEqual(XWPFVertAlign.TOP, tableRows[1].GetCell(1).GetVerticalAlignment());
        }
Пример #11
0
        public void TestWriteFromReadOnlyOPC()
        {
            OPCPackage opc = OPCPackage.Open(
                POIDataSamples.GetDocumentInstance().GetFileInfo("SampleDoc.docx"),
                PackageAccess.READ
                );
            XWPFDocument      doc      = new XWPFDocument(opc);
            XWPFWordExtractor ext      = new XWPFWordExtractor(doc);
            String            origText = ext.Text;

            doc = XWPFTestDataSamples.WriteOutAndReadBack(doc);
            ext = new XWPFWordExtractor(doc);

            Assert.AreEqual(origText, ext.Text);
        }
Пример #12
0
        public void TestAddParagraphsToFootnote()
        {
            // Add a run to the first paragraph:

            XWPFParagraph p1 = footnote.CreateParagraph();

            p1.CreateRun().SetText(p1Text);

            // Create a second paragraph:

            XWPFParagraph p = footnote.CreateParagraph();

            Assert.IsNotNull(p, "Paragraph is null");
            p.CreateRun().SetText(p2Text);

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            XWPFFootnote testFootnote = docIn.GetFootnoteByID(footnoteId);

            Assert.IsNotNull(testFootnote);

            Assert.AreEqual(2, testFootnote.GetParagraphs().Count);
            XWPFParagraph testP1 = testFootnote.GetParagraphs()[0];

            Assert.AreEqual(p1Text, testP1.Text);

            XWPFParagraph testP2 = testFootnote.GetParagraphs()[1];

            Assert.AreEqual(p2Text, testP2.Text);

            // The first paragraph added using CreateParagraph() should
            // have the required footnote reference added to the first
            // run.

            // Verify that we have a footnote reference in the first paragraph and not
            // in the second paragraph.

            XWPFRun r1 = testP1.Runs[0];

            Assert.IsNotNull(r1);
            Assert.IsTrue(r1.GetCTR().GetFootnoteRefList().Count > 0, "No footnote reference in testP1");
            Assert.IsNotNull(r1.GetCTR().GetFootnoteRefArray(0), "No footnote reference in testP1");

            XWPFRun r2 = testP2.Runs[0];

            Assert.IsNotNull(r2, "Expected a run in testP2");
            Assert.IsTrue(r2.GetCTR().GetFootnoteRefList().Count == 0, "Found a footnote reference in testP2");
        }
Пример #13
0
        public void TestSetWatermark()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
            // no header is Set (yet)
            XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy();

            Assert.IsNull(policy.GetDefaultHeader());
            Assert.IsNull(policy.GetFirstPageHeader());
            Assert.IsNull(policy.GetDefaultFooter());

            policy.CreateWatermark("DRAFT");

            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNotNull(policy.GetEvenPageHeader());
        }
Пример #14
0
        public void TestTransitiveSetters()
        {
            XWPFDocument   doc = new XWPFDocument();
            CoreProperties cp  = doc.GetProperties().CoreProperties;

            DateTime dateCreated = new DateTime(2010, 6, 15, 10, 0, 0);

            cp.Created = new DateTime(2010, 6, 15, 10, 0, 0);
            Assert.AreEqual(dateCreated.ToString(), cp.Created.ToString());

            doc = XWPFTestDataSamples.WriteOutAndReadBack(doc);
            cp  = doc.GetProperties().CoreProperties;
            DateTime?dt3 = cp.Created;

            Assert.AreEqual(dateCreated.ToString(), dt3.ToString());
        }
Пример #15
0
        public void TestBookmarks()
        {
            XWPFDocument  doc       = XWPFTestDataSamples.OpenSampleDocument("bookmarks.docx");
            XWPFParagraph paragraph = doc.Paragraphs[0];

            Assert.AreEqual("Sample Word Document", paragraph.Text);
            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);
            }
        }
Пример #16
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.Text);
        }
Пример #17
0
        public void TestPictureHandlingSimpleFile()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("issue_51265_1.docx");

            Assert.AreEqual(1, doc.AllPackagePictures.Count);
            byte[] newPic = XWPFTestDataSamples.GetImage("abstract4.jpg");
            String id1    = doc.AddPictureData(newPic, (int)PictureType.JPEG);

            Assert.AreEqual(2, doc.AllPackagePictures.Count);
            /* copy data, to avoid instance-Equality */
            byte[] newPicCopy = Arrays.CopyOf(newPic, newPic.Length);
            String id2        = doc.AddPictureData(newPicCopy, (int)PictureType.JPEG);

            Assert.AreEqual(id1, id2);
            doc.Package.Revert();
        }
Пример #18
0
        public void TestLvlText()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("Numbering.docx");

            Assert.AreEqual("%1.%2.%3.", doc.Paragraphs[(12)].NumLevelText);

            Assert.AreEqual("NEW-%1-FORMAT", doc.Paragraphs[(14)].NumLevelText);

            XWPFParagraph p = doc.Paragraphs[(18)];

            Assert.AreEqual("%1.", p.NumLevelText);
            //test that null doesn't throw NPE
            //Assert.IsNull(p.GetNumFmt());

            //C# enum is never null
            Assert.AreEqual([email protected](), p.GetNumFmt());
        }
Пример #19
0
        public void TestOpen()
        {
            XWPFDocument xml;

            // Simple file
            xml = XWPFTestDataSamples.OpenSampleDocument("sample.docx");
            // Check it has key parts
            Assert.IsNotNull(xml.Document);
            Assert.IsNotNull(xml.Document.body);
            Assert.IsNotNull(xml.GetStyles());

            // Complex file
            xml = XWPFTestDataSamples.OpenSampleDocument("IllustrativeCases.docx");
            Assert.IsNotNull(xml.Document);
            Assert.IsNotNull(xml.Document.body);
            Assert.IsNotNull(xml.GetStyles());
        }
Пример #20
0
        public void TestAddStylesToDocument()
        {
            XWPFDocument docOut = new XWPFDocument();
            XWPFStyles   styles = docOut.CreateStyles();

            String   strStyleName = "headline1";
            CT_Style ctStyle      = new CT_Style();

            ctStyle.styleId = (strStyleName);
            XWPFStyle s = new XWPFStyle(ctStyle);

            styles.AddStyle(s);

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            styles = docIn.GetStyles();
            Assert.IsTrue(styles.StyleExist(strStyleName));
        }
Пример #21
0
        public void TestSetStyleId()
        {
            XWPFDocument document = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");

            XWPFRun run = document.CreateParagraph().CreateRun();

            String styleId = "bolditalic";

            run.SetStyle(styleId);
            String candStyleId = run.GetCTR().rPr.rStyle.val;

            Assert.IsNotNull(candStyleId, "Expected to find a run style ID");
            Assert.AreEqual(styleId, candStyleId);

            Assert.AreEqual(styleId, run.GetStyle());

            document.Close();
        }
Пример #22
0
        public void TestPictureHandlingComplex()
        {
            XWPFDocument doc        = XWPFTestDataSamples.OpenSampleDocument("issue_51265_3.docx");
            XWPFHeader   xwpfHeader = doc.HeaderList[(0)];

            Assert.AreEqual(3, doc.AllPictures.Count);
            Assert.AreEqual(3, xwpfHeader.AllPictures.Count);
            Assert.AreEqual(5, doc.AllPackagePictures.Count);

            byte[]             nature1 = XWPFTestDataSamples.GetImage("nature1.jpg");
            String             id      = doc.AddPictureData(nature1, (int)PictureType.JPEG);
            POIXMLDocumentPart part1   = xwpfHeader.GetRelationById("rId1");
            XWPFPictureData    part2   = (XWPFPictureData)doc.GetRelationById(id);

            Assert.AreSame(part1, part2);

            doc.Package.Revert();
        }
Пример #23
0
        public void TestZeroLengthLibreOfficeDocumentWithWaterMarkHeader()
        {
            XWPFDocument     doc        = XWPFTestDataSamples.OpenSampleDocument("zero-length.docx");
            POIXMLProperties properties = doc.GetProperties();

            Assert.IsNotNull(properties.CoreProperties);

            XWPFHeader headerArray = doc.GetHeaderArray(0);

            Assert.AreEqual(1, headerArray.AllPictures.Count);
            Assert.AreEqual("image1.png", headerArray.AllPictures[0].FileName);
            Assert.AreEqual("", headerArray.Text);

            ExtendedProperties extendedProperties = properties.ExtendedProperties;

            Assert.IsNotNull(extendedProperties);
            Assert.AreEqual(0, extendedProperties.GetUnderlyingProperties().Characters);
        }
Пример #24
0
        public void TestSearchTextFound()
        {
            XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx");

            IList <XWPFParagraph> ps = xml.Paragraphs;

            Assert.AreEqual(10, ps.Count);

            XWPFParagraph p = ps[(0)];

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

            Assert.IsNotNull(segment);

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

            Assert.IsTrue(p.RemoveRun(0));
        }
Пример #25
0
        public void TestAddFootnotesToDocument()
        {
            XWPFDocument docOut = new XWPFDocument();

            // NOTE: XWPFDocument.createFootnote() delegates directly
            //       to XWPFFootnotes.createFootnote() so this tests
            //       both creation of new XWPFFootnotes in document
            //       and XWPFFootnotes.createFootnote();
            XWPFFootnote footnote = docOut.CreateFootnote();
            int          noteId   = footnote.Id;

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            XWPFFootnote note = docIn.GetFootnoteByID(noteId);

            Assert.IsNotNull(note);
            Assert.AreEqual(note.GetCTFtnEdn().type, ST_FtnEdn.normal);
        }
Пример #26
0
        public void TestCellVerticalAlign2()
        {
            XWPFDocument      docx   = XWPFTestDataSamples.OpenSampleDocument("TestTableCellAlign.docx");
            IList <XWPFTable> tables = docx.Tables;

            foreach (XWPFTable table in tables)
            {
                List <XWPFTableRow> tableRows = table.Rows;
                foreach (XWPFTableRow tableRow in tableRows)
                {
                    List <XWPFTableCell> tableCells = tableRow.GetTableCells();
                    foreach (XWPFTableCell tableCell in tableCells)
                    {
                        Assert.IsNotNull(tableCell.GetVerticalAlignment());
                    }
                }
            }
        }
Пример #27
0
        public void TestHeadersFooters()
        {
            XWPFDocument      doc       = XWPFTestDataSamples.OpenSampleDocument("ThreeColHeadFoot.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.AreEqual(
                "First header column!\tMid header\tRight header!\n" +
                "This is a sample word document. It has two pages. It has a three column heading, and a three column footer\n" +
                "\n" +
                "HEADING TEXT\n" +
                "\n" +
                "More on page one\n" +
                "\n\n" +
                "End of page 1\n\n\n" +
                "This is page two. It also has a three column heading, and a three column footer.\n" +
                "Footer Left\tFooter Middle\tFooter Right\n",
                extractor.Text
                );

            // Now another file, expect multiple headers
            //  and multiple footers
            doc       = XWPFTestDataSamples.OpenSampleDocument("DiffFirstPageHeadFoot.docx");
            extractor = new XWPFWordExtractor(doc);
            extractor =
                new XWPFWordExtractor(doc);
            //extractor.Text;

            Assert.AreEqual(
                "I am the header on the first page, and I" + '\u2019' + "m nice and simple\n" +
                "First header column!\tMid header\tRight header!\n" +
                "This is a sample word document. It has two pages. It has a simple header and footer, which is different to all the other pages.\n" +
                "\n" +
                "HEADING TEXT\n" +
                "\n" +
                "More on page one\n" +
                "\n\n" +
                "End of page 1\n\n\n" +
                "This is page two. It also has a three column heading, and a three column footer.\n" +
                "The footer of the first page\n" +
                "Footer Left\tFooter Middle\tFooter Right\n",
                extractor.Text
                );
        }
Пример #28
0
        public void TestAddingRuns()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx");

            XWPFParagraph p = doc.Paragraphs[0];

            Assert.AreEqual(2, p.Runs.Count);

            XWPFRun r = p.CreateRun();

            Assert.AreEqual(3, p.Runs.Count);
            Assert.AreEqual(2, p.Runs.IndexOf(r));

            XWPFRun r2 = p.InsertNewRun(1);

            Assert.AreEqual(4, p.Runs.Count);
            Assert.AreEqual(1, p.Runs.IndexOf(r2));
            Assert.AreEqual(3, p.Runs.IndexOf(r));
        }
Пример #29
0
        public void TestAddFootnotesToDocument()
        {
            XWPFDocument docOut = new XWPFDocument();

            int noteId = 1;

            XWPFFootnotes footnotes = docOut.CreateFootnotes();
            CT_FtnEdn     ctNote    = new CT_FtnEdn();

            ctNote.id   = (noteId.ToString());
            ctNote.type = (ST_FtnEdn.normal);
            footnotes.AddFootnote(ctNote);

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            XWPFFootnote note = docIn.GetFootnoteByID(noteId);

            Assert.AreEqual(note.GetCTFtnEdn().type, ST_FtnEdn.normal);
        }
Пример #30
0
        public void TestSetStyles_Bug57254()
        {
            XWPFDocument docOut = new XWPFDocument();
            XWPFStyles   styles = docOut.CreateStyles();

            CT_Styles ctStyles   = new CT_Styles();
            String    strStyleId = "headline1";
            CT_Style  ctStyle    = ctStyles.AddNewStyle();

            ctStyle.styleId = (/*setter*/ strStyleId);
            styles.SetStyles(ctStyles);

            Assert.IsTrue(styles.StyleExist(strStyleId));

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            styles = docIn.GetStyles();
            Assert.IsTrue(styles.StyleExist(strStyleId));
        }