Exemplo n.º 1
0
        public void InPlaceReWrite()
        {
            FileInfo f = TempFile.CreateTempFile("protected_agile", ".docx");
            // File f = new File("protected_agile.docx");
            FileStream fos = f.Create();
            Stream     fis = POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_agile.docx");

            IOUtils.Copy(fis, fos);
            fis.Close();
            fos.Close();

            NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false);

            // decrypt the protected file - in this case it was encrypted with the default password
            EncryptionInfo encInfo = new EncryptionInfo(fs);
            Decryptor      d       = encInfo.Decryptor;
            bool           b       = d.VerifyPassword(Decryptor.DEFAULT_PASSWORD);

            Assert.IsTrue(b);

            // do some strange things with it ;)
            XWPFDocument docx = new XWPFDocument(d.GetDataStream(fs));

            docx.GetParagraphArray(0).InsertNewRun(0).SetText("POI was here! All your base are belong to us!");
            docx.GetParagraphArray(0).InsertNewRun(1).AddBreak();

            // and encrypt it again
            Encryptor e = encInfo.Encryptor;

            e.ConfirmPassword("AYBABTU");
            docx.Write(e.GetDataStream(fs));

            fs.Close();
        }
Exemplo n.º 2
0
        public void TestSmartTags()
        {
            XWPFDocument  doc = XWPFTestDataSamples.OpenSampleDocument("smarttag-snippet.docx");
            XWPFParagraph p   = doc.GetParagraphArray(0);

            //About NPOI: because the serializer bug(the CT_Run contains whitespace will discard the whitespace),
            //Text is "CarnegieMellonUniversitySchool of Computer Science"
            Assert.IsTrue(p.GetText().Contains("Carnegie Mellon University School of Computer Science"));
            p = doc.GetParagraphArray(2);
            Assert.IsTrue(p.GetText().Contains("Alice's Adventures"));
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
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.Text);
            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.Text);
            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.Text);
            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.Text);
            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);
        }