Exemple #1
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);
            }
            
        }
Exemple #2
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);
            }
            
        }