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); }
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()); } }
internal override void OnDocumentRead() { try { XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream()); DocumentDocument doc = DocumentDocument.Parse(xmldoc, NamespaceManager); ctDocument = doc.Document; InitFootnotes(); // parse the document with cursor and add // // the XmlObject to its lists foreach (object o in ctDocument.body.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); bodyElements.Add(p); paragraphs.Add(p); } else if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); bodyElements.Add(t); tables.Add(t); } else if (o is CT_SdtBlock) { XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this); bodyElements.Add(c); contentControls.Add(c); } } // Sort out headers and footers if (doc.Document.body.sectPr != null) headerFooterPolicy = new XWPFHeaderFooterPolicy(this); // Create for each XML-part in the Package a PartClass foreach (POIXMLDocumentPart p in GetRelations()) { String relation = p.GetPackageRelationship().RelationshipType; if (relation.Equals(XWPFRelation.STYLES.Relation)) { this.styles = (XWPFStyles)p; this.styles.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.NUMBERING.Relation)) { this.numbering = (XWPFNumbering)p; this.numbering.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.FOOTER.Relation)) { XWPFFooter footer = (XWPFFooter)p; footers.Add(footer); footer.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.HEADER.Relation)) { XWPFHeader header = (XWPFHeader)p; headers.Add(header); header.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.COMMENT.Relation)) { XmlDocument xml = ConvertStreamToXml(p.GetPackagePart().GetInputStream()); CommentsDocument cmntdoc = CommentsDocument.Parse(xml ,NamespaceManager); foreach (CT_Comment ctcomment in cmntdoc.Comments.comment) { comments.Add(new XWPFComment(ctcomment, this)); } } else if (relation.Equals(XWPFRelation.SETTINGS.Relation)) { Settings = (XWPFSettings)p; Settings.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.IMAGES.Relation)) { XWPFPictureData picData = (XWPFPictureData)p; picData.OnDocumentRead(); RegisterPackagePictureData(picData); pictures.Add(picData); } else if (relation.Equals(XWPFRelation.GLOSSARY_DOCUMENT.Relation)) { // We don't currently process the glossary itself // Until we do, we do need to load the glossary child parts of it foreach (POIXMLDocumentPart gp in p.GetRelations()) { // Trigger the onDocumentRead for all the child parts // Otherwise we'll hit issues on Styles, Settings etc on save try { gp.OnDocumentRead(); //Method onDocumentRead = gp.getClass().getDeclaredMethod("onDocumentRead"); //onDocumentRead.setAccessible(true); //onDocumentRead.invoke(gp); } catch (Exception e) { throw new POIXMLException(e); } } } } InitHyperlinks(); } catch (XmlException e) { throw new POIXMLException(e); } }
internal override void OnDocumentRead() { try { DocumentDocument doc = DocumentDocument.Parse(GetPackagePart().GetInputStream()); ctDocument = doc.Document; InitFootnotes(); foreach (object o in ctDocument.body.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); bodyElements.Add(p); paragraphs.Add(p); } else if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); bodyElements.Add(t); tables.Add(t); } } // parse the document with cursor and add // // the XmlObject to its lists // XmlCursor cursor = ctDocument.Body.NewCursor(); // cursor.SelectPath("./*"); // while (cursor.ToNextSelection()) { // XmlObject o = cursor.Object; // if (o is CTP) { // XWPFParagraph p = new XWPFParagraph((CTP) o, this); // bodyElements.Add(p); // paragraphs.Add(p); // } else if (o is CTTbl) { // XWPFTable t = new XWPFTable((CTTbl) o, this); // bodyElements.Add(t); // tables.Add(t); // } // } // cursor.Dispose(); // Sort out headers and footers if (doc.Document.body.sectPr != null) headerFooterPolicy = new XWPFHeaderFooterPolicy(this); // Create for each XML-part in the Package a PartClass foreach (POIXMLDocumentPart p in GetRelations()) { String relation = p.GetPackageRelationship().RelationshipType; if (relation.Equals(XWPFRelation.STYLES.Relation)) { this.styles = (XWPFStyles)p; this.styles.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.NUMBERING.Relation)) { this.numbering = (XWPFNumbering)p; this.numbering.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.FOOTER.Relation)) { XWPFFooter footer = (XWPFFooter)p; footers.Add(footer); footer.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.HEADER.Relation)) { XWPFHeader header = (XWPFHeader)p; headers.Add(header); header.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.COMMENT.Relation)) { // TODO Create according XWPFComment class, extending POIXMLDocumentPart CommentsDocument cmntdoc = CommentsDocument.Parse(p.GetPackagePart().GetInputStream()); foreach (CT_Comment ctcomment in cmntdoc.Comments.comment) { comments.Add(new XWPFComment(ctcomment, this)); } } else if (relation.Equals(XWPFRelation.SETTINGS.Relation)) { Settings = (XWPFSettings)p; Settings.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.IMAGES.Relation)) { XWPFPictureData picData = (XWPFPictureData)p; picData.OnDocumentRead(); RegisterPackagePictureData(picData); pictures.Add(picData); } } InitHyperlinks(); } catch (XmlException e) { throw new POIXMLException(e); } }
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); } }
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); } }