Example #1
0
        public void TestPictureInHeader()
        {
            XWPFDocument           sampleDoc = XWPFTestDataSamples.OpenSampleDocument("headerPic.docx");
            XWPFHeaderFooterPolicy policy    = sampleDoc.GetHeaderFooterPolicy();

            XWPFHeader header = policy.GetDefaultHeader();

            int count = 0;

            foreach (XWPFParagraph p in header.Paragraphs)
            {
                foreach (XWPFRun r in p.Runs)
                {
                    List <XWPFPicture> pictures = r.GetEmbeddedPictures();

                    foreach (XWPFPicture pic in pictures)
                    {
                        Assert.IsNotNull(pic.GetPictureData());
                        Assert.AreEqual("DOZOR", pic.GetDescription());
                    }

                    count += pictures.Count;
                }
            }

            Assert.AreEqual(1, count);
        }
Example #2
0
        public void TestSetParagraphStyle()
        {
            //System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            //new clean instance of paragraph
            XWPFDocument  doc = XWPFTestDataSamples.OpenSampleDocument("heading123.docx");
            XWPFParagraph p   = doc.CreateParagraph();
            XWPFRun       run = p.CreateRun();

            run.SetText("Heading 1");

            CT_SdtBlock block = doc.Document.body.AddNewSdt();

            Assert.IsNull(p.Style);
            p.Style = HEADING1;
            Assert.AreEqual(HEADING1, p.GetCTP().pPr.pStyle.val);

            doc.CreateTOC();

            /*
             * // TODO - finish this test
             * if (false) {
             *  CTStyles styles = doc.Style;
             *  CTStyle style = styles.AddNewStyle();
             *  style.Type=(STStyleType.PARAGRAPH);
             *  style.StyleId=("Heading1");
             * }
             *
             * if (false) {
             *  File file = TempFile.CreateTempFile("testHeaders", ".docx");
             *  OutputStream out1 = new FileOutputStream(file);
             *  doc.Write(out1);
             *  out1.Close();
             * }
             */
        }
Example #3
0
        public void TestRegisterPackagePictureData()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("issue_51265_1.docx");

            /* manually assemble a new image package part*/
            OPCPackage      opcPckg      = doc.Package;
            XWPFRelation    jpgRelation  = XWPFRelation.IMAGE_JPEG;
            PackagePartName partName     = PackagingUriHelper.CreatePartName(jpgRelation.DefaultFileName.Replace('#', '2'));
            PackagePart     newImagePart = opcPckg.CreatePart(partName, jpgRelation.ContentType);

            byte[] nature1 = XWPFTestDataSamples.GetImage("abstract4.jpg");
            Stream os      = newImagePart.GetOutputStream();

            os.Write(nature1, 0, nature1.Length);
            os.Close();
            XWPFHeader          xwpfHeader   = doc.HeaderList[(0)];
            PackageRelationship relationship = xwpfHeader.GetPackagePart().AddRelationship(partName, TargetMode.Internal, jpgRelation.Relation);
            XWPFPictureData     newPicData   = new XWPFPictureData(newImagePart, relationship);

            /* new part is now Ready to rumble */

            Assert.IsFalse(xwpfHeader.AllPictures.Contains(newPicData));
            Assert.IsFalse(doc.AllPictures.Contains(newPicData));
            Assert.IsFalse(doc.AllPackagePictures.Contains(newPicData));

            doc.RegisterPackagePictureData(newPicData);

            Assert.IsFalse(xwpfHeader.AllPictures.Contains(newPicData));
            Assert.IsFalse(doc.AllPictures.Contains(newPicData));
            Assert.IsTrue(doc.AllPackagePictures.Contains(newPicData));

            doc.Package.Revert();
        }
Example #4
0
        public void TestGetAllPictures()
        {
            XWPFDocument            doc                = XWPFTestDataSamples.OpenSampleDocument("issue_51265_3.docx");
            IList <XWPFPictureData> allPictures        = doc.AllPictures;
            IList <XWPFPictureData> allPackagePictures = doc.AllPackagePictures;

            Assert.IsNotNull(allPictures);
            Assert.AreEqual(3, allPictures.Count);
            foreach (XWPFPictureData xwpfPictureData in allPictures)
            {
                Assert.IsTrue(allPackagePictures.Contains(xwpfPictureData));
            }

            try
            {
                allPictures.Add(allPictures[0]);
                Assert.Fail("This list must be unmodifiable!");
            }
            catch (NotSupportedException)
            {
                // all ok
            }

            doc.Package.Revert();
        }
Example #5
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());

            // Re-open, and check
            XWPFDocument reopened = XWPFTestDataSamples.WriteOutAndReadBack(sampleDoc);

            policy = reopened.GetHeaderFooterPolicy();

            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNotNull(policy.GetEvenPageHeader());
        }
Example #6
0
        public void TestGetSDTs()
        {
            String[] contents = new String[] {
                "header_rich_text",
                "Rich_text",
                "Rich_text_pre_table\nRich_text_cell1\t\t\t\n\nRich_text_post_table",
                "Plain_text_no_newlines",
                "Plain_text_with_newlines1\nplain_text_with_newlines2",
                "Watermelon",
                "Dirt",
                "4/16/2013",
                "rich_text_in_paragraph_in_cell",
                "Footer_rich_text",
                "Footnote_sdt",
                "Endnote_sdt"
            };
            XWPFDocument   doc  = XWPFTestDataSamples.OpenSampleDocument("Bug54849.docx");
            List <XWPFSDT> sdts = extractAllSDTs(doc);

            Assert.AreEqual(contents.Length, sdts.Count, "number of sdts");

            for (int i = 0; i < sdts.Count; i++)
            {//contents.Length; i++){
                XWPFSDT sdt = sdts[i];

                Assert.AreEqual(contents[i], sdt.Content.ToString(), i + ": " + contents[i]);
            }
        }
Example #7
0
        public void TestEndnotes()
        {
            XWPFDocument      doc       = XWPFTestDataSamples.OpenSampleDocument("endnotes.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.IsTrue(extractor.Text.Contains("XXX"));
        }
Example #8
0
        public void TestGetWithHyperlinks()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            // Now check contents
            extractor.SetFetchHyperlinks(false);
            Assert.AreEqual(
                    "This is a test document.\nThis bit is in bold and italic\n" +
                    "Back to normal\n" +
                    "This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" +
                    "We have a hyperlink here, and another.\n",
                    extractor.Text
            );

            // One hyperlink is a real one, one is just to the top of page
            extractor.SetFetchHyperlinks(true);
            Assert.AreEqual(
                    "This is a test document.\nThis bit is in bold and italic\n" +
                    "Back to normal\n" +
                    "This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" +
                    "We have a hyperlink <http://poi.apache.org/> here, and another.\n",
                    extractor.Text
            );
        }
Example #9
0
 public void TestDrawings()
 {
     XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("drawing.docx");
     XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
     String text = extractor.Text;
     Assert.IsTrue(text.Length > 0);
 }
Example #10
0
        public void TestRead()
        {
            XWPFDocument            sampleDoc = XWPFTestDataSamples.OpenSampleDocument("VariousPictures.docx");
            IList <XWPFPictureData> pictures  = sampleDoc.AllPictures;

            Assert.AreEqual(5, pictures.Count);
            String[] ext = { "wmf", "png", "emf", "emf", "jpeg" };
            for (int i = 0; i < pictures.Count; i++)
            {
                Assert.AreEqual(ext[i], pictures[(i)].SuggestFileExtension());
            }

            int num = pictures.Count;

            byte[] pictureData = XWPFTestDataSamples.GetImage("nature1.jpg");

            String relationId = sampleDoc.AddPictureData(pictureData, (int)PictureType.JPEG);

            // picture list was updated
            Assert.AreEqual(num + 1, pictures.Count);
            XWPFPictureData pict = (XWPFPictureData)sampleDoc.GetRelationById(relationId);

            Assert.AreEqual("jpeg", pict.SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(pictureData, pict.Data));
        }
Example #11
0
        public void TestGetSimpleText()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            String text = extractor.Text;
            Assert.IsTrue(text.Length > 0);

            // Check contents
            Assert.IsTrue(text.StartsWith(
                    "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc at risus vel erat tempus posuere. Aenean non ante. Suspendisse vehicula dolor sit amet odio."
            ));
            Assert.IsTrue(text.EndsWith(
                    "Phasellus ultricies mi nec leo. Sed tempus. In sit amet lorem at velit faucibus vestibulum.\n"
            ));

            // Check number of paragraphs
            int ps = 0;
            char[] t = text.ToCharArray();
            for (int i = 0; i < t.Length; i++)
            {
                if (t[i] == '\n')
                {
                    ps++;
                }
            }
            Assert.AreEqual(3, ps);
        }
Example #12
0
        public void TestGetComplexText()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("IllustrativeCases.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            String text = extractor.Text;
            Assert.IsTrue(text.Length > 0);

            char euro = '\u20ac';
            Debug.WriteLine("'" + text.Substring(text.Length - 40) + "'");

            //Check contents
            Assert.IsTrue(text.StartsWith(
                    "  \n(V) ILLUSTRATIVE CASES\n\n"
            ));
            Assert.IsTrue(text.Contains(
                    "As well as gaining " + euro + "90 from child benefit increases, he will also receive the early childhood supplement of " + euro + "250 per quarter for Vincent for the full four quarters of the year.\n\n\n\n"// \n\n\n"
            ));
            Assert.IsTrue(text.EndsWith(
                    "11.4%\t\t90\t\t\t\t\t250\t\t1,310\t\n\n \n\n\n"
            ));

            // Check number of paragraphs
            int ps = 0;
            char[] t = text.ToCharArray();
            for (int i = 0; i < t.Length; i++)
            {
                if (t[i] == '\n')
                {
                    ps++;
                }
            }
            Assert.AreEqual(134, ps);
        }
Example #13
0
        public void TestCreateTable()
        {
            // open an empty document
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx");

            // create a table with 5 rows and 7 coloumns
            int       noRows = 5;
            int       noCols = 7;
            XWPFTable table  = doc.CreateTable(noRows, noCols);

            // assert the table is empty
            List <XWPFTableRow> rows = table.Rows;

            Assert.AreEqual(noRows, rows.Count, "Table has less rows than requested.");
            foreach (XWPFTableRow xwpfRow in rows)
            {
                Assert.IsNotNull(xwpfRow);
                for (int i = 0; i < 7; i++)
                {
                    XWPFTableCell xwpfCell = xwpfRow.GetCell(i);
                    Assert.IsNotNull(xwpfCell);
                    Assert.AreEqual(1, xwpfCell.Paragraphs.Count, "Empty cells should not have one paragraph.");
                    xwpfCell = xwpfRow.GetCell(i);
                    Assert.AreEqual(1, xwpfCell.Paragraphs.Count, "Calling 'getCell' must not modify cells content.");
                }
            }
            doc.Package.Revert();
        }
Example #14
0
        public void TestTableFootnotes()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("table_footnotes.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.IsTrue(extractor.Text.Contains("snoska"));
        }
Example #15
0
        public void Bug57312_NullPointException()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("57312.docx");

            Assert.IsNotNull(doc);

            foreach (IBodyElement bodyElement in doc.BodyElements)
            {
                BodyElementType elementType = bodyElement.ElementType;

                if (elementType == BodyElementType.PARAGRAPH)
                {
                    XWPFParagraph paragraph = (XWPFParagraph)bodyElement;

                    foreach (IRunElement iRunElem in paragraph.IRuns)
                    {
                        if (iRunElem is XWPFRun)
                        {
                            XWPFRun RunElement = (XWPFRun)iRunElem;

                            UnderlinePatterns underline = RunElement.Underline;
                            Assert.IsNotNull(underline);

                            //System.out.Println("Found: " + underline + ": " + RunElement.GetText(0));
                        }
                    }
                }
            }
        }
Example #16
0
        public void TestBug51770()
        {
            XWPFDocument           doc    = XWPFTestDataSamples.OpenSampleDocument("Bug51170.docx");
            XWPFHeaderFooterPolicy policy = doc.GetHeaderFooterPolicy();
            XWPFHeader             header = policy.GetDefaultHeader();

            foreach (XWPFParagraph paragraph in header.Paragraphs)
            {
                foreach (XWPFRun run in paragraph.Runs)
                {
                    foreach (XWPFPicture picture in run.GetEmbeddedPictures())
                    {
                        if (paragraph.Document != null)
                        {
                            System.Console.WriteLine(picture.GetCTPicture());
                            XWPFPictureData data = picture.GetPictureData();
                            if (data != null)
                            {
                                System.Console.WriteLine(data.FileName);
                            }
                        }
                    }
                }
            }
        }
Example #17
0
        public void TestEasyAccessToStyles()
        {
            XWPFDocument doc    = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
            XWPFStyles   styles = doc.GetStyles();

            Assert.IsNotNull(styles);

            // Has 3 paragraphs on page one, a break, and 3 on page 2
            Assert.AreEqual(7, doc.Paragraphs.Count);

            // Check the first three have no run styles, just default paragraph style
            for (int i = 0; i < 3; i++)
            {
                XWPFParagraph p = doc.Paragraphs[(i)];
                Assert.AreEqual(null, p.Style);
                Assert.AreEqual(null, p.StyleID);
                Assert.AreEqual(1, p.Runs.Count);

                XWPFRun r = p.Runs[(0)];
                Assert.AreEqual(null, r.GetColor());
                Assert.AreEqual(null, r.FontFamily);
                Assert.AreEqual(null, r.FontName);
                Assert.AreEqual(-1, r.FontSize);
            }

            // On page two, has explicit styles, but on Runs not on
            //  the paragraph itself
            for (int i = 4; i < 7; i++)
            {
                XWPFParagraph p = doc.Paragraphs[(i)];
                Assert.AreEqual(null, p.Style);
                Assert.AreEqual(null, p.StyleID);
                Assert.AreEqual(1, p.Runs.Count);

                XWPFRun r = p.Runs[(0)];
                Assert.AreEqual("Arial Black", r.FontFamily);
                Assert.AreEqual("Arial Black", r.FontName);
                Assert.AreEqual(16, r.FontSize);
                Assert.AreEqual("548DD4", r.GetColor());
            }

            // Check the document styles
            // Should have a style defined for each type
            Assert.AreEqual(4, styles.NumberOfStyles);
            Assert.IsNotNull(styles.GetStyle("Normal"));
            Assert.IsNotNull(styles.GetStyle("DefaultParagraphFont"));
            Assert.IsNotNull(styles.GetStyle("TableNormal"));
            Assert.IsNotNull(styles.GetStyle("NoList"));

            // We can't do much yet with latent styles
            Assert.AreEqual(137, styles.LatentStyles.NumberOfStyles);

            // Check the default styles
            Assert.IsNotNull(styles.DefaultRunStyle);
            Assert.IsNotNull(styles.DefaultParagraphStyle);

            Assert.AreEqual(11, styles.DefaultRunStyle.FontSize);
            Assert.AreEqual(200, styles.DefaultParagraphStyle.SpacingAfter);
        }
Example #18
0
 public void TestFldSimpleContent()
 {
     XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("FldSimple.docx");
     XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
     String text = extractor.Text;
     Assert.IsTrue(text.Length > 0);
     Assert.IsTrue(text.Contains("FldSimple.docx"));
 }
Example #19
0
 public void TestEndnotes()
 {
     XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("endnotes.docx");
     XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
     string text = extractor.Text;
     Assert.IsTrue(text.Contains("XXX"));
     Assert.IsTrue(text.Contains("tilaka [endnoteRef:2]or 'tika'"));
 }
Example #20
0
 public void TestFootnotes()
 {
     XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("footnotes.docx");
     XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
     String text = extractor.Text;
     Assert.IsTrue(extractor.Text.Contains("snoska"));
     Assert.IsTrue(text.Contains("Eto ochen prostoy[footnoteRef:1] text so snoskoy"));
 }
Example #21
0
        public void SetUp()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("documentProperties.docx");

            _props          = sampleDoc.GetProperties();
            _coreProperties = _props.CoreProperties;
            Assert.IsNotNull(_props);
        }
Example #22
0
        public void TestInsertedDeletedText()
        {
            XWPFDocument      doc       = XWPFTestDataSamples.OpenSampleDocument("delins.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.IsTrue(extractor.Text.Contains("pendant worn"));
            Assert.IsTrue(extractor.Text.Contains("extremely well"));
        }
Example #23
0
        public void TestTika792()
        {
            //This test forces the loading of CTMoveBookmark and
            //CTMoveBookmarkImpl into ooxml-lite.
            XWPFDocument  doc       = XWPFTestDataSamples.OpenSampleDocument("Tika-792.docx");
            XWPFParagraph paragraph = doc.Paragraphs[(0)];

            Assert.AreEqual("s", paragraph.Text);
        }
Example #24
0
        public void TestGetNumIlvl()
        {
            XWPFDocument doc     = XWPFTestDataSamples.OpenSampleDocument("Numbering.docx");
            string       numIlvl = "0";

            Assert.AreEqual(numIlvl, doc.Paragraphs[0].GetNumIlvl());
            numIlvl = "1";
            Assert.AreEqual(numIlvl, doc.Paragraphs[5].GetNumIlvl());
        }
Example #25
0
        public void TestPictureHandlingHeaderDocumentImages()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("issue_51265_2.docx");

            Assert.AreEqual(1, doc.AllPictures.Count);
            Assert.AreEqual(1, doc.AllPackagePictures.Count);
            Assert.AreEqual(1, doc.HeaderList[(0)].AllPictures.Count);
            doc.Package.Revert();
        }
Example #26
0
 public void SetUp()
 {
     noHeader     = XWPFTestDataSamples.OpenSampleDocument("NoHeadFoot.docx");
     header       = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx");
     headerFooter = XWPFTestDataSamples.OpenSampleDocument("SimpleHeadThreeColFoot.docx");
     footer       = XWPFTestDataSamples.OpenSampleDocument("FancyFoot.docx");
     oddEven      = XWPFTestDataSamples.OpenSampleDocument("PageSpecificHeadFoot.docx");
     diffFirst    = XWPFTestDataSamples.OpenSampleDocument("DiffFirstPageHeadFoot.docx");
 }
Example #27
0
        public void TestDOCMFiles()
        {
            XWPFDocument      doc       = XWPFTestDataSamples.OpenSampleDocument("45690.docm");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.IsTrue(extractor.Text.Contains("2004"));
            Assert.IsTrue(extractor.Text.Contains("2008"));
            Assert.IsTrue(extractor.Text.Contains("(120 "));
        }
Example #28
0
        public void TestParagraphHeader()
        {
            XWPFDocument      doc       = XWPFTestDataSamples.OpenSampleDocument("Headers.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            Assert.IsTrue(extractor.Text.Contains("Section 1"));
            Assert.IsTrue(extractor.Text.Contains("Section 2"));
            Assert.IsTrue(extractor.Text.Contains("Section 3"));
        }
Example #29
0
 public void TestNoFieldCodes()
 {
     XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("FieldCodes.docx");
     XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
     String text = extractor.Text;
     Assert.IsTrue(text.Length > 0);
     Assert.IsFalse(text.Contains("AUTHOR"));
     Assert.IsFalse(text.Contains("CREATEDATE"));
 }
Example #30
0
        public void TestBug55733()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("55733.docx");
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);

            // Check it gives text without error
            string text = extractor.Text;
            extractor.Close();
        }