Esempio n. 1
0
        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();
        }
Esempio n. 2
0
 internal override void OnDocumentCreate()
 {
     this.ctDocument = new CT_Document();
     this.ctDocument.AddNewBody();
     this.Settings = (XWPFSettings)this.CreateRelationship((POIXMLRelation)XWPFRelation.SETTINGS, (POIXMLFactory)XWPFFactory.GetInstance());
     this.CreateStyles();
     this.GetProperties().GetExtendedProperties().GetUnderlyingProperties().Application = POIXMLDocument.DOCUMENT_CREATOR;
 }
Esempio n. 3
0
 public DocumentDocument(CT_Document document)
 {
     this.document = document;
 }
Esempio n. 4
0
        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);
            }
            
        }
Esempio n. 5
0
        /**
         * Create a new CT_Document with all values Set to default
         */

        internal override void OnDocumentCreate()
        {
            ctDocument = new CT_Document();
            ctDocument.AddNewBody();
            

            Settings = (XWPFSettings) CreateRelationship(XWPFRelation.SETTINGS,XWPFFactory.GetInstance());
            CreateStyles();

            ExtendedProperties expProps = GetProperties().ExtendedProperties;
            expProps.GetUnderlyingProperties().Application = (DOCUMENT_CREATOR);
        }
Esempio n. 6
0
        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);
            }
            
        }
Esempio n. 7
0
 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);
     }
 }
Esempio n. 8
0
        private static void FillLoop(XWPFDocument doc, LoopDefine loop, DataTable table)
        {
            foreach (DataRow row in table.Rows)
            {
                foreach (XWPFParagraph templateph in loop.Templates)
                {
                    XWPFParagraph ph = doc.CreateParagraph();

                    int paragraphPos = doc.Paragraphs.IndexOf(loop.Templates[0]);

                    //公开的API中信息不全
                    //修改位置
                    FieldInfo propertyCtDocument = typeof(XWPFDocument).GetField("ctDocument", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
                    if (propertyCtDocument != null)
                    {
                        CT_Document ctDocument = propertyCtDocument.GetValue(doc) as CT_Document;
                        if (ctDocument != null)
                        {
                            ArrayList items = ctDocument.body.Items;
                            List <DocumentBodyItemChoiceType> names = ctDocument.body.ItemsElementName;

                            int pos       = doc.Paragraphs.IndexOf(loop.Templates[0]);
                            int globalPos = GetObjectIndex(names, DocumentBodyItemChoiceType.p, paragraphPos);

                            items[globalPos] = items[items.Count - 1];
                            names[globalPos] = names[items.Count - 1];
                            for (int i = items.Count - 2; i >= globalPos; i--)
                            {
                                items[i + 1] = items[i];
                                names[i + 1] = names[i];
                            }
                        }
                    }

                    //修改列表中的位置
                    FieldInfo propertyParagraphs   = typeof(XWPFDocument).GetField("paragraphs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
                    FieldInfo propertyBodyElements = typeof(XWPFDocument).GetField("bodyElements", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
                    if (propertyParagraphs != null && propertyBodyElements != null)
                    {
                        List <XWPFParagraph> paragraphs   = propertyParagraphs.GetValue(doc) as List <XWPFParagraph>;
                        List <IBodyElement>  bodyElements = propertyBodyElements.GetValue(doc) as List <IBodyElement>;

                        paragraphs.Remove(ph);
                        bodyElements.Remove(ph);
                        paragraphs.Insert(paragraphs.IndexOf(loop.Templates[0]), ph);
                        bodyElements.Insert(bodyElements.IndexOf(loop.Templates[0]), ph);
                    }

                    WordGenerator.CopyParagraph(templateph, ph);

                    foreach (XWPFRun run in ph.Runs)
                    {
                        WordGenerator.MergeField(run, row);
                    }
                }
            }

            //删除模板
            doc.RemoveBodyElement(doc.BodyElements.IndexOf(loop.StartParagraph));
            doc.RemoveBodyElement(doc.BodyElements.IndexOf(loop.EndParagraph));
            foreach (XWPFParagraph template in loop.Templates)
            {
                doc.RemoveBodyElement(doc.BodyElements.IndexOf(template));
            }
        }