public OpenXmlDocumentModel(Stream output, WordOpenXmlWriter.CreateXmlStream createXmlStream, ScalabilityCache scalabilityCache) { _createXmlStream = createXmlStream; _zipPackage = Package.Open(output, FileMode.Create); _documentPart = (_currentPart = new PartInfo()); _currentPart.Part = new DocumentPart(); _manager = new PartManager(ZipPackage); Relationship relationship = _manager.AddStreamingRootPartToTree("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "word/document.xml"); WriteStylesheet(); WriteSettings(); _currentPart.PartName = relationship.RelatedPart; _currentPart.Stream = _createXmlStream("document"); _currentPart.Writer = new InterleavingWriter(_currentPart.Stream, scalabilityCache); _currentHeaderFooterReferences = default(TemporaryHeaderFooterReferences); _tags = new Stack <OoxmlComplexType>(); _currentPart.Writer.TextWriter.Write(OoxmlPart.XmlDeclaration); CT_Document cT_Document = new CT_Document(); cT_Document.WriteOpenTag(_currentPart.Writer.TextWriter, _currentPart.Part.Tag, _currentPart.Part.Namespaces); _tags.Push(cT_Document); CT_Body ctObject = new CT_Body(); WriteStartTag(ctObject, CT_Document.BodyElementName); _currentPart.TableContext = new TableContext(_currentPart.Writer, inHeaderFooter: false); _listManager = new OpenXmlListNumberingManager(); _sectionProperties = new OpenXmlSectionPropertiesModel(); }
/** * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated * that the XWPFFooter or XWPFHeader object returned by calls to the CreateHeader(int, XWPFParagraph[]) * and CreateFooter(int, XWPFParagraph[]) methods or the GetXXXXXHeader/Footer methods where * headers or footers had been Added to a document since it had been Created/opened, returned * an object that Contained no XWPFParagraph objects even if the header/footer itself did contain * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.NewInstance(); * Created a brand new instance of the CTHDRFtr class which was then populated with data when * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object * that had previoulsy been instantiated in the CreateHeader(int, XWPFParagraph[]) or * CreateFooter(int, XWPFParagraph[]) methods. */ private CT_HdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper) { CT_HdrFtr ftr = wrapper._getHdrFtr(); if (paragraphs != null) { for (int i = 0; i < paragraphs.Length; i++) { CT_P p = ftr.AddNewP(); ftr.SetPArray(i, paragraphs[i].GetCTP()); } } else { CT_P p = ftr.AddNewP(); CT_Body body = doc.Document.body; if (body.SizeOfPArray() > 0) { CT_P p0 = body.GetPArray(0); if (p0.IsSetRsidR()) { byte[] rsidr = p0.rsidR; byte[] rsidrdefault = p0.rsidRDefault; p.rsidP = rsidr; p.rsidRDefault = rsidrdefault; } } CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = (pStyle); } return(ftr); }