コード例 #1
0
ファイル: TestXWPFRun.cs プロジェクト: zanhaipeng/npoi
        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.GetRuns())
                {
                    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);
        }
コード例 #2
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);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
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);
        }
コード例 #4
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());
        }
コード例 #5
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);
        }
コード例 #6
0
        public void AppendParagraphText(StringBuilder text, XWPFParagraph paragraph)
        {
            try
            {
                CT_SectPr ctSectPr = null;
                if (paragraph.GetCTP().pPr != null)
                {
                    ctSectPr = paragraph.GetCTP().pPr.sectPr;
                }

                XWPFHeaderFooterPolicy headerFooterPolicy = null;

                if (ctSectPr != null)
                {
                    headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
                    extractHeaders(text, headerFooterPolicy);
                }

                foreach (IRunElement run in paragraph.Runs)
                {
                    text.Append(run.ToString());
                    if (run is XWPFHyperlinkRun && fetchHyperlinks)
                    {
                        XWPFHyperlink link = ((XWPFHyperlinkRun)run).GetHyperlink(document);
                        if (link != null)
                        {
                            text.Append(" <" + link.URL + ">");
                        }
                    }
                }

                // Add comments
                XWPFCommentsDecorator decorator = new XWPFCommentsDecorator(paragraph, null);
                String commentText = decorator.GetCommentText();
                if (commentText.Length > 0)
                {
                    text.Append(commentText).Append('\n');
                }

                // Do endnotes and footnotes
                String footnameText = paragraph.FootnoteText;
                if (footnameText != null && footnameText.Length > 0)
                {
                    text.Append(footnameText + '\n');
                }

                if (ctSectPr != null)
                {
                    extractFooters(text, headerFooterPolicy);
                }
            }
            catch (IOException e)
            {
                throw new POIXMLException(e);
            }
            catch (XmlException e)
            {
                throw new POIXMLException(e);
            }
        }
コード例 #7
0
        private void verifyOneHeaderPicture(XWPFDocument sampleDoc)
        {
            XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy();

            XWPFHeader header = policy.GetDefaultHeader();

            IList <XWPFPictureData> pictures = header.AllPictures;

            Assert.AreEqual(1, pictures.Count);
        }
コード例 #8
0
ファイル: TestXWPFHeader.cs プロジェクト: zbl960/npoi
        public void TestImageInHeader()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("headerPic.docx");

            XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy();

            XWPFHeader header = policy.GetDefaultHeader();

            Assert.IsNotNull(header.GetRelations());
            Assert.AreEqual(1, header.GetRelations().Count);
        }
コード例 #9
0
        public void TestPictureInHeader()
        {
            XWPFDocument           sampleDoc = XWPFTestDataSamples.OpenSampleDocument("headerPic.docx");
            XWPFHeaderFooterPolicy policy    = sampleDoc.GetHeaderFooterPolicy();

            XWPFHeader header = policy.GetDefaultHeader();

            IList <XWPFPictureData> pictures = header.AllPictures;

            Assert.AreEqual(1, pictures.Count);
        }
コード例 #10
0
ファイル: TestXWPFHeader.cs プロジェクト: zbl960/npoi
        public void TestSimpleHeader()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("headerFooter.docx");

            XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy();

            XWPFHeader header = policy.GetDefaultHeader();
            XWPFFooter footer = policy.GetDefaultFooter();

            Assert.IsNotNull(header);
            Assert.IsNotNull(footer);
        }
コード例 #11
0
ファイル: XWPFWordExtractor.cs プロジェクト: purehzj/npoi-1
 private void extractHeaders(StringBuilder text, XWPFHeaderFooterPolicy hfPolicy)
 {
     if (hfPolicy.GetFirstPageHeader() != null)
     {
         text.Append(hfPolicy.GetFirstPageHeader().GetText());
     }
     if (hfPolicy.GetEvenPageHeader() != null)
     {
         text.Append(hfPolicy.GetEvenPageHeader().GetText());
     }
     if (hfPolicy.GetDefaultHeader() != null)
     {
         text.Append(hfPolicy.GetDefaultHeader().GetText());
     }
 }
コード例 #12
0
ファイル: TestXWPFHeader.cs プロジェクト: zbl960/npoi
        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());
        }
コード例 #13
0
ファイル: XWPFWordExtractor.cs プロジェクト: hiodava/Romero
 private void extractFooters(StringBuilder text, XWPFHeaderFooterPolicy hfPolicy)
 {
     if (hfPolicy == null)
     {
         return;
     }
     if (hfPolicy.GetFirstPageFooter() != null)
     {
         text.Append(hfPolicy.GetFirstPageFooter().Text);
     }
     if (hfPolicy.GetEvenPageFooter() != null)
     {
         text.Append(hfPolicy.GetEvenPageFooter().Text);
     }
     if (hfPolicy.GetDefaultFooter() != null)
     {
         text.Append(hfPolicy.GetDefaultFooter().Text);
     }
 }
コード例 #14
0
ファイル: TestXWPFHeader.cs プロジェクト: zbl960/npoi
        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();

            t.Value = ("Paragraph in header");

            // 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
            policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
            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);

            // 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 XWPFParagraph[footer.Paragraphs.Count];
            int             i     = 0;

            foreach (XWPFParagraph p in footer.Paragraphs)
            {
                paras[i++] = p;
            }

            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);
        }
コード例 #15
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);
        }
コード例 #16
0
ファイル: XWPFDocument.cs プロジェクト: thachgiasoft/shuijin
 internal override void OnDocumentRead()
 {
     try
     {
         DocumentDocument documentDocument = DocumentDocument.Parse(this.GetPackagePart().GetInputStream());
         this.ctDocument = documentDocument.Document;
         this.InitFootnotes();
         foreach (object obj in this.ctDocument.body.Items)
         {
             if (obj is CT_P)
             {
                 XWPFParagraph xwpfParagraph = new XWPFParagraph((CT_P)obj, (IBody)this);
                 this.bodyElements.Add((IBodyElement)xwpfParagraph);
                 this.paragraphs.Add(xwpfParagraph);
             }
             else if (obj is CT_Tbl)
             {
                 XWPFTable xwpfTable = new XWPFTable((CT_Tbl)obj, (IBody)this);
                 this.bodyElements.Add((IBodyElement)xwpfTable);
                 this.tables.Add(xwpfTable);
             }
         }
         if (documentDocument.Document.body.sectPr != null)
         {
             this.headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
         }
         foreach (POIXMLDocumentPart relation1 in this.GetRelations())
         {
             string relationshipType = relation1.GetPackageRelationship().RelationshipType;
             if (relationshipType.Equals(XWPFRelation.STYLES.Relation))
             {
                 this.styles = (XWPFStyles)relation1;
                 this.styles.OnDocumentRead();
             }
             else if (relationshipType.Equals(XWPFRelation.NUMBERING.Relation))
             {
                 this.numbering = (XWPFNumbering)relation1;
                 this.numbering.OnDocumentRead();
             }
             else if (relationshipType.Equals(XWPFRelation.FOOTER.Relation))
             {
                 XWPFFooter xwpfFooter = (XWPFFooter)relation1;
                 this.footers.Add(xwpfFooter);
                 xwpfFooter.OnDocumentRead();
             }
             else if (relationshipType.Equals(XWPFRelation.HEADER.Relation))
             {
                 XWPFHeader xwpfHeader = (XWPFHeader)relation1;
                 this.headers.Add(xwpfHeader);
                 xwpfHeader.OnDocumentRead();
             }
             else if (relationshipType.Equals(XWPFRelation.COMMENT.Relation))
             {
                 foreach (CT_Comment comment in CommentsDocument.Parse(relation1.GetPackagePart().GetInputStream()).Comments.comment)
                 {
                     this.comments.Add(new XWPFComment(comment, this));
                 }
             }
             else if (relationshipType.Equals(XWPFRelation.SETTINGS.Relation))
             {
                 this.Settings = (XWPFSettings)relation1;
                 this.Settings.OnDocumentRead();
             }
             else if (relationshipType.Equals(XWPFRelation.IMAGES.Relation))
             {
                 XWPFPictureData picData = (XWPFPictureData)relation1;
                 picData.OnDocumentRead();
                 this.RegisterPackagePictureData(picData);
                 this.pictures.Add(picData);
             }
             else if (relationshipType.Equals(XWPFRelation.GLOSSARY_DOCUMENT.Relation))
             {
                 foreach (POIXMLDocumentPart relation2 in relation1.GetRelations())
                 {
                     try
                     {
                         relation2.OnDocumentRead();
                     }
                     catch (Exception ex)
                     {
                         throw new POIXMLException(ex);
                     }
                 }
             }
         }
         this.InitHyperlinks();
     }
     catch (XmlException ex)
     {
         throw new POIXMLException((Exception)ex);
     }
 }