Example #1
0
        /**
         * Adds a picture to the document.
         *
         * @param pictureData       The picture data
         * @param format            The format of the picture.
         *
         * @return the index to this picture (0 based), the Added picture can be obtained from {@link #getAllPictures()} .
         * @throws InvalidFormatException
         */
        public String AddPictureData(byte[] pictureData, int format)
        {
            XWPFPictureData xwpfPicData = document.FindPackagePictureData(pictureData, format);
            POIXMLRelation  relDesc     = XWPFPictureData.RELATIONS[format];

            if (xwpfPicData == null)
            {
                /* Part doesn't exist, create a new one */
                int idx = document.GetNextPicNameNumber(format);
                xwpfPicData = (XWPFPictureData)CreateRelationship(relDesc, XWPFFactory.GetInstance(), idx);
                /* write bytes to new part */
                PackagePart picDataPart = xwpfPicData.GetPackagePart();
                Stream      out1        = null;
                try
                {
                    out1 = picDataPart.GetOutputStream();
                    out1.Write(pictureData, 0, pictureData.Length);
                }
                catch (IOException e)
                {
                    throw new POIXMLException(e);
                }
                finally
                {
                    try
                    {
                        if (out1 != null)
                        {
                            out1.Close();
                        }
                    }
                    catch (IOException)
                    {
                        // ignore
                    }
                }

                document.RegisterPackagePictureData(xwpfPicData);
                pictures.Add(xwpfPicData);
                return(GetRelationId(xwpfPicData));
            }
            else if (!GetRelations().Contains(xwpfPicData))
            {
                /*
                 * Part already existed, but was not related so far. Create
                 * relationship to the already existing part and update
                 * POIXMLDocumentPart data.
                 */
                PackagePart picDataPart = xwpfPicData.GetPackagePart();
                // TODO add support for TargetMode.EXTERNAL relations.
                RelationPart rp = AddRelation(null, XWPFRelation.IMAGES, xwpfPicData);
                pictures.Add(xwpfPicData);
                return(rp.Relationship.Id);
            }
            else
            {
                /* Part already existed, Get relation id and return it */
                return(GetRelationId(xwpfPicData));
            }
        }
Example #2
0
        public string AddPictureData(byte[] pictureData, int format)
        {
            XWPFPictureData packagePictureData = this.document.FindPackagePictureData(pictureData, format);
            POIXMLRelation  descriptor         = XWPFPictureData.RELATIONS[format];

            if (packagePictureData == null)
            {
                int             nextPicNameNumber = this.document.GetNextPicNameNumber(format);
                XWPFPictureData relationship      = (XWPFPictureData)this.CreateRelationship(descriptor, (POIXMLFactory)XWPFFactory.GetInstance(), nextPicNameNumber);
                PackagePart     packagePart       = relationship.GetPackagePart();
                Stream          stream            = (Stream)null;
                try
                {
                    stream = packagePart.GetOutputStream();
                    stream.Write(pictureData, 0, pictureData.Length);
                }
                catch (IOException ex)
                {
                    throw new POIXMLException((Exception)ex);
                }
                finally
                {
                    try
                    {
                        stream.Close();
                    }
                    catch (IOException ex)
                    {
                    }
                }
                this.document.RegisterPackagePictureData(relationship);
                this.pictures.Add(relationship);
                return(this.GetRelationId((POIXMLDocumentPart)relationship));
            }
            if (this.GetRelations().Contains((POIXMLDocumentPart)packagePictureData))
            {
                return(this.GetRelationId((POIXMLDocumentPart)packagePictureData));
            }
            PackagePart     packagePart1 = packagePictureData.GetPackagePart();
            TargetMode      targetMode   = TargetMode.Internal;
            PackagePartName partName     = packagePart1.PartName;
            string          relation     = descriptor.Relation;
            string          id           = this.GetPackagePart().AddRelationship(partName, targetMode, relation).Id;

            this.AddRelation(id, (POIXMLDocumentPart)packagePictureData);
            this.pictures.Add(packagePictureData);
            return(id);
        }
Example #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);
 }
Example #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);
 }
Example #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);
 }
Example #6
0
 public XWPFDocument(Stream is1)
     : base(PackageHelper.Open(is1))
 {
     this.Load((POIXMLFactory)XWPFFactory.GetInstance());
 }
Example #7
0
 public XWPFDocument(OPCPackage pkg)
     : base(pkg)
 {
     this.Load((POIXMLFactory)XWPFFactory.GetInstance());
 }
Example #8
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;
 }