Esempio n. 1
0
        public override POIXMLDocumentPart CreateDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part)
        {
            POIXMLRelation descriptor = XWPFRelation.GetInstance(rel.RelationshipType);

            if (descriptor == null || descriptor.RelationClass == null)
            {
                logger.Log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.RelationshipType);
                return(new POIXMLDocumentPart(part, rel));
            }

            try
            {
                Type cls = descriptor.RelationClass;
                try
                {
                    ConstructorInfo constructor = cls.GetConstructor(new Type[] { typeof(POIXMLDocumentPart), typeof(PackagePart), typeof(PackageRelationship) });
                    return(constructor.Invoke(new object[] { parent, part, rel }) as POIXMLDocumentPart);
                }
                catch (Exception)
                {
                    ConstructorInfo constructor = cls.GetConstructor(new Type[] { typeof(PackagePart), typeof(PackageRelationship) });
                    return(constructor.Invoke(new object[] { part, rel }) as POIXMLDocumentPart);
                }
            }
            catch (Exception e)
            {
                throw new POIXMLException(e);
            }
        }
Esempio n. 2
0
        public void TestRegisterPackagePictureData()
        {
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("issue_51265_1.docx");

            /* manually assemble a new image package part*/
            OPCPackage      opcPckg      = doc.Package;
            XWPFRelation    jpgRelation  = XWPFRelation.IMAGE_JPEG;
            PackagePartName partName     = PackagingUriHelper.CreatePartName(jpgRelation.DefaultFileName.Replace('#', '2'));
            PackagePart     newImagePart = opcPckg.CreatePart(partName, jpgRelation.ContentType);

            byte[] nature1 = XWPFTestDataSamples.GetImage("abstract4.jpg");
            Stream os      = newImagePart.GetOutputStream();

            os.Write(nature1, 0, nature1.Length);
            os.Close();
            XWPFHeader          xwpfHeader   = doc.HeaderList[(0)];
            PackageRelationship relationship = xwpfHeader.GetPackagePart().AddRelationship(partName, TargetMode.Internal, jpgRelation.Relation);
            XWPFPictureData     newPicData   = new XWPFPictureData(newImagePart, relationship);

            /* new part is now Ready to rumble */

            Assert.IsFalse(xwpfHeader.AllPictures.Contains(newPicData));
            Assert.IsFalse(doc.AllPictures.Contains(newPicData));
            Assert.IsFalse(doc.AllPackagePictures.Contains(newPicData));

            doc.RegisterPackagePictureData(newPicData);

            Assert.IsFalse(xwpfHeader.AllPictures.Contains(newPicData));
            Assert.IsFalse(doc.AllPictures.Contains(newPicData));
            Assert.IsTrue(doc.AllPackagePictures.Contains(newPicData));

            doc.Package.Revert();
        }
Esempio n. 3
0
 public XWPFFootnotes CreateFootnotes()
 {
     if (this.footnotes == null)
     {
         FootnotesDocument footnotesDocument = new FootnotesDocument();
         XWPFRelation      footnote          = XWPFRelation.FOOTNOTE;
         int           relationIndex         = this.GetRelationIndex(footnote);
         XWPFFootnotes relationship          = (XWPFFootnotes)this.CreateRelationship((POIXMLRelation)footnote, (POIXMLFactory)XWPFFactory.GetInstance(), relationIndex);
         relationship.SetFootnotes(footnotesDocument.Footnotes);
         this.footnotes = relationship;
     }
     return(this.footnotes);
 }
Esempio n. 4
0
 public XWPFStyles CreateStyles()
 {
     if (this.styles == null)
     {
         StylesDocument stylesDocument = new StylesDocument();
         XWPFRelation   styles         = XWPFRelation.STYLES;
         int            relationIndex  = this.GetRelationIndex(styles);
         XWPFStyles     relationship   = (XWPFStyles)this.CreateRelationship((POIXMLRelation)styles, (POIXMLFactory)XWPFFactory.GetInstance(), relationIndex);
         relationship.SetStyles(stylesDocument.Styles);
         this.styles = relationship;
     }
     return(this.styles);
 }
Esempio n. 5
0
 public XWPFNumbering CreateNumbering()
 {
     if (this.numbering == null)
     {
         NumberingDocument numberingDocument = new NumberingDocument();
         XWPFRelation      numbering         = XWPFRelation.NUMBERING;
         int           relationIndex         = this.GetRelationIndex(numbering);
         XWPFNumbering relationship          = (XWPFNumbering)this.CreateRelationship((POIXMLRelation)numbering, (POIXMLFactory)XWPFFactory.GetInstance(), relationIndex);
         relationship.SetNumbering(numberingDocument.Numbering);
         this.numbering = relationship;
     }
     return(this.numbering);
 }
Esempio n. 6
0
        private int GetRelationIndex(XWPFRelation relation)
        {
            List <POIXMLDocumentPart> relations = this.GetRelations();
            int num = 1;
            IEnumerator <POIXMLDocumentPart> enumerator = (IEnumerator <POIXMLDocumentPart>)relations.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (enumerator.Current.GetPackageRelationship().RelationshipType.Equals(relation.Relation))
                {
                    ++num;
                }
            }
            return(num);
        }
Esempio n. 7
0
        public override POIXMLDocumentPart CreateDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part)
        {
            POIXMLRelation instance = (POIXMLRelation)XWPFRelation.GetInstance(rel.RelationshipType);

            if (instance != null)
            {
                if (instance.RelationClass != null)
                {
                    try
                    {
                        Type relationClass = instance.RelationClass;
                        try
                        {
                            return(relationClass.GetConstructor(new Type[3] {
                                typeof(POIXMLDocumentPart), typeof(PackagePart), typeof(PackageRelationship)
                            }).Invoke(new object[3] {
                                (object)parent, (object)part, (object)rel
                            }) as POIXMLDocumentPart);
                        }
                        catch (Exception ex)
                        {
                            return(relationClass.GetConstructor(new Type[2] {
                                typeof(PackagePart), typeof(PackageRelationship)
                            }).Invoke(new object[2] {
                                (object)part, (object)rel
                            }) as POIXMLDocumentPart);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new POIXMLException(ex);
                    }
                }
            }
            XWPFFactory.logger.Log(1, (object)("using default POIXMLDocumentPart for " + rel.RelationshipType));
            return(new POIXMLDocumentPart(part, rel));
        }
Esempio n. 8
0
 private int GetRelationIndex(XWPFRelation relation)
 {
     List<POIXMLDocumentPart> relations = doc.GetRelations();
     int i = 1;
     for (IEnumerator<POIXMLDocumentPart> it = relations.GetEnumerator(); it.MoveNext(); )
     {
         POIXMLDocumentPart item = it.Current;
         if (item.GetPackageRelationship().RelationshipType.Equals(relation.Relation))
         {
             i++;
         }
     }
     return i;
 }
Esempio n. 9
0
 /**
  * @since POI 3.14-Beta1
  */
 protected override POIXMLRelation GetDescriptor(String relationshipType)
 {
     return(XWPFRelation.GetInstance(relationshipType));
 }