Esempio n. 1
0
        public void TestCreateHeaderPicture()
        { // TODO Fix
            XWPFDocument doc = new XWPFDocument();

            // Starts with no header
            XWPFHeaderFooterPolicy policy = doc.GetHeaderFooterPolicy();

            Assert.IsNull(policy);

            // Add a default header
            policy = doc.CreateHeaderFooterPolicy();

            XWPFHeader header = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT);

            header.Paragraphs[0].CreateRun().SetText("Hello, Header World!");
            header.CreateParagraph().CreateRun().SetText("Paragraph 2");
            Assert.AreEqual(0, header.AllPictures.Count);
            Assert.AreEqual(2, header.Paragraphs.Count);

            // Add a picture to  the first paragraph
            header.Paragraphs[0].Runs[0].AddPicture(
                new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }),
                (int)PictureType.JPEG, "test.jpg", 2, 2);

            // Check
            verifyOneHeaderPicture(doc);

            // Save, re-load, re-check
            XWPFDocument readBack = XWPFTestDataSamples.WriteOutAndReadBack(doc);

            verifyOneHeaderPicture(readBack);
        }
Esempio n. 2
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());
        }
Esempio n. 3
0
        public void TestCreate()
        {
            XWPFDocument doc = new XWPFDocument();

            Assert.AreEqual(null, doc.GetHeaderFooterPolicy());
            Assert.AreEqual(0, doc.HeaderList.Count);
            Assert.AreEqual(0, doc.FooterList.Count);

            XWPFHeaderFooterPolicy policy = doc.CreateHeaderFooterPolicy();

            Assert.IsNotNull(doc.GetHeaderFooterPolicy());
            Assert.AreEqual(0, doc.HeaderList.Count);
            Assert.AreEqual(0, doc.FooterList.Count);

            // Create a header and a footer
            XWPFHeader header = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT);
            XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT);

            header.CreateParagraph().CreateRun().SetText("Header Hello");
            footer.CreateParagraph().CreateRun().SetText("Footer Bye");


            // Save, re-load, and check
            doc = XWPFTestDataSamples.WriteOutAndReadBack(doc);
            Assert.IsNotNull(doc.GetHeaderFooterPolicy());
            Assert.AreEqual(1, doc.HeaderList.Count);
            Assert.AreEqual(1, doc.FooterList.Count);

            Assert.AreEqual("Header Hello\n", doc.HeaderList[(0)].Text);
            Assert.AreEqual("Footer Bye\n", doc.FooterList[(0)].Text);
        }
Esempio n. 4
0
        public void TestPictureInHeader()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("headerPic.docx");

            verifyOneHeaderPicture(sampleDoc);

            XWPFDocument readBack = XWPFTestDataSamples.WriteOutAndReadBack(sampleDoc);

            verifyOneHeaderPicture(readBack);
        }
Esempio n. 5
0
        public void Test59378()
        {
            XWPFDocument          doc  = XWPFTestDataSamples.OpenSampleDocument("59378.docx");
            ByteArrayOutputStream out1 = new ByteArrayOutputStream();

            doc.Write(out1);
            out1.Close();
            XWPFDocument doc2 = new XWPFDocument(new ByteArrayInputStream(out1.ToByteArray()));

            doc2.Close();
            XWPFDocument docBack = XWPFTestDataSamples.WriteOutAndReadBack(doc);

            docBack.Close();
        }
Esempio n. 6
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);
        }
Esempio n. 7
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());
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
        public void TestAllPictureFormats()
        {
            XWPFDocument doc = new XWPFDocument();

            doc.AddPictureData(new byte[10], (int)PictureType.EMF);
            doc.AddPictureData(new byte[11], (int)PictureType.WMF);
            doc.AddPictureData(new byte[12], (int)PictureType.PICT);
            doc.AddPictureData(new byte[13], (int)PictureType.JPEG);
            doc.AddPictureData(new byte[14], (int)PictureType.PNG);
            doc.AddPictureData(new byte[15], (int)PictureType.DIB);
            doc.AddPictureData(new byte[16], (int)PictureType.GIF);
            doc.AddPictureData(new byte[17], (int)PictureType.TIFF);
            doc.AddPictureData(new byte[18], (int)PictureType.EPS);
            doc.AddPictureData(new byte[19], (int)PictureType.BMP);
            doc.AddPictureData(new byte[20], (int)PictureType.WPG);

            Assert.AreEqual(11, doc.AllPictures.Count);

            doc = XWPFTestDataSamples.WriteOutAndReadBack(doc);
            Assert.AreEqual(11, doc.AllPictures.Count);
        }
Esempio n. 11
0
        public void TestBug55476()
        {
            byte[]       image    = XWPFTestDataSamples.GetImage("abstract1.jpg");
            XWPFDocument document = new XWPFDocument();

            document.CreateParagraph().CreateRun().AddPicture(
                new MemoryStream(image), (int)PictureType.JPEG, "test.jpg", Units.ToEMU(300), Units.ToEMU(100));
            XWPFDocument       docBack  = XWPFTestDataSamples.WriteOutAndReadBack(document);
            List <XWPFPicture> pictures = docBack.GetParagraphArray(0).Runs[0].GetEmbeddedPictures();

            Assert.AreEqual(1, pictures.Count);
            docBack.Close();

            /*OutputStream stream = new FileOutputStream("c:\\temp\\55476.docx");
             * try {
             *  document.write(stream);
             * } finally {
             *  stream.close();
             * }*/
            document.Close();
        }
Esempio n. 12
0
        public void TestNew()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("EmptyDocumentWithHeaderFooter.docx");

            byte[] jpegData = XWPFTestDataSamples.GetImage("nature1.jpg");
            Assert.IsNotNull(jpegData);
            byte[] gifData = XWPFTestDataSamples.GetImage("nature1.gif");
            Assert.IsNotNull(gifData);
            byte[] pngData = XWPFTestDataSamples.GetImage("nature1.png");
            Assert.IsNotNull(pngData);

            IList <XWPFPictureData> pictures = doc.AllPictures;

            Assert.AreEqual(0, pictures.Count);

            // Document shouldn't have any image relationships
            Assert.AreEqual(13, doc.GetPackagePart().Relationships.Size);
            foreach (PackageRelationship rel in doc.GetPackagePart().Relationships)
            {
                if (rel.RelationshipType.Equals(XSSFRelation.IMAGE_JPEG.Relation))
                {
                    Assert.Fail("Shouldn't have JPEG yet");
                }
            }

            // Add the image
            String relationId = doc.AddPictureData(jpegData, (int)PictureType.JPEG);

            Assert.AreEqual(1, pictures.Count);
            XWPFPictureData jpgPicData = (XWPFPictureData)doc.GetRelationById(relationId);

            Assert.AreEqual("jpeg", jpgPicData.SuggestFileExtension());
            Assert.IsTrue(Arrays.Equals(jpegData, jpgPicData.Data));

            // Ensure it now has one
            Assert.AreEqual(14, doc.GetPackagePart().Relationships.Size);
            PackageRelationship jpegRel = null;

            foreach (PackageRelationship rel in doc.GetPackagePart().Relationships)
            {
                if (rel.RelationshipType.Equals(XWPFRelation.IMAGE_JPEG.Relation))
                {
                    if (jpegRel != null)
                    {
                        Assert.Fail("Found 2 jpegs!");
                    }
                    jpegRel = rel;
                }
            }
            Assert.IsNotNull(jpegRel, "JPEG Relationship not found");

            // Check the details
            Assert.AreEqual(XWPFRelation.IMAGE_JPEG.Relation, jpegRel.RelationshipType);
            Assert.AreEqual("/word/document.xml", jpegRel.Source.PartName.ToString());
            Assert.AreEqual("/word/media/image1.jpeg", jpegRel.TargetUri.OriginalString);

            XWPFPictureData pictureDataByID = doc.GetPictureDataByID(jpegRel.Id);

            Assert.IsTrue(Arrays.Equals(jpegData, pictureDataByID.Data));

            // Save an re-load, check it appears
            doc = XWPFTestDataSamples.WriteOutAndReadBack(doc);
            Assert.AreEqual(1, doc.AllPictures.Count);
            Assert.AreEqual(1, doc.AllPackagePictures.Count);

            // verify the picture that we read back in
            pictureDataByID = doc.GetPictureDataByID(jpegRel.Id);
            Assert.IsTrue(Arrays.Equals(jpegData, pictureDataByID.Data));
        }
Esempio n. 13
0
        public void TestSetHeader()
        {
            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());

            CT_P    ctP1  = new CT_P();
            CT_R    ctR1  = ctP1.AddNewR();
            CT_Text t     = ctR1.AddNewT();
            String  tText = "Paragraph in header";

            t.Value = tText;

            // Commented MB 23 May 2010
            //CTP ctP2 = CTP.Factory.NewInstance();
            //CTR ctR2 = ctP2.AddNewR();
            //CTText t2 = ctR2.AddNewT();
            //t2.StringValue=("Second paragraph.. for footer");

            // Create two paragraphs for insertion into the footer.
            // Previously only one was inserted MB 23 May 2010
            CT_P    ctP2 = new CT_P();
            CT_R    ctR2 = ctP2.AddNewR();
            CT_Text t2   = ctR2.AddNewT();

            t2.Value = ("First paragraph for the footer");

            CT_P    ctP3 = new CT_P();
            CT_R    ctR3 = ctP3.AddNewR();
            CT_Text t3   = ctR3.AddNewT();

            t3.Value = ("Second paragraph for the footer");

            XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);

            XWPFParagraph[] pars = new XWPFParagraph[1];
            pars[0] = p1;

            XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
            XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);

            XWPFParagraph[] pars2 = new XWPFParagraph[2];
            pars2[0] = p2;
            pars2[1] = p3;

            // Set headers
            XWPFHeader headerD = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
            XWPFHeader headerF = policy.CreateHeader(XWPFHeaderFooterPolicy.FIRST);
            // Set a default footer and capture the returned XWPFFooter object.
            XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);

            // Ensure the headers and footer were Set correctly....
            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNotNull(policy.GetDefaultFooter());
            // ....and that the footer object captured above Contains two
            // paragraphs of text.
            Assert.AreEqual(2, footer.Paragraphs.Count);

            // Check the header created with the paragraph got them, and the one
            // created without got an empty one
            Assert.AreEqual(1, headerD.Paragraphs.Count);
            Assert.AreEqual(1, headerF.Paragraphs.Count);

            Assert.AreEqual(tText, headerD.Paragraphs[0].Text);
            Assert.AreEqual("", headerF.Paragraphs[0].Text);

            // As an Additional Check, recover the defauls footer and
            // make sure that it Contains two paragraphs of text and that
            // both do hold what is expected.
            footer = policy.GetDefaultFooter();

            XWPFParagraph[] paras = new List <XWPFParagraph>(footer.Paragraphs).ToArray();

            Assert.AreEqual(2, paras.Length);
            Assert.AreEqual("First paragraph for the footer", paras[0].Text);
            Assert.AreEqual("Second paragraph for the footer", paras[1].Text);

            // Add some text to the empty header
            String fText1 = "New Text!";

            headerF.Paragraphs[0].InsertNewRun(0).SetText(fText1);
            // TODO Add another paragraph and check

            // Check it
            Assert.AreEqual(tText, headerD.Paragraphs[0].Text);
            Assert.AreEqual(fText1, headerF.Paragraphs[0].Text);

            // Save, re-open, ensure it's all still there
            XWPFDocument reopened = XWPFTestDataSamples.WriteOutAndReadBack(sampleDoc);

            policy = reopened.GetHeaderFooterPolicy();
            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNull(policy.GetEvenPageHeader());
            Assert.IsNotNull(policy.GetDefaultFooter());
            Assert.IsNull(policy.GetFirstPageFooter());
            Assert.IsNull(policy.GetEvenPageFooter());

            // Check the new headers still have their text
            headerD = policy.GetDefaultHeader();
            headerF = policy.GetFirstPageHeader();
            Assert.AreEqual(tText, headerD.Paragraphs[0].Text);
            Assert.AreEqual(fText1, headerF.Paragraphs[0].Text);

            // Check the new footers have their new text too
            footer = policy.GetDefaultFooter();
            paras  = new List <XWPFParagraph>(footer.Paragraphs).ToArray();

            Assert.AreEqual(2, paras.Length);
            Assert.AreEqual("First paragraph for the footer", paras[0].Text);
            Assert.AreEqual("Second paragraph for the footer", paras[1].Text);
        }