/** * 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.Dispose(); } } 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. TargetMode targetMode = TargetMode.Internal; PackagePartName partName = picDataPart.PartName; String relation = relDesc.Relation; PackageRelationship relShip = GetPackagePart().AddRelationship(partName, targetMode, relation); String id = relShip.Id; AddRelation(id, xwpfPicData); pictures.Add(xwpfPicData); return(id); } else { /* Part already existed, Get relation id and return it */ return(GetRelationId(xwpfPicData)); } }
public override bool Equals(Object obj) { /** * In case two objects ARE Equal, but its not the same instance, this * implementation will always run through the whole * byte-array-comparison before returning true. If this will turn into a * performance issue, two possible approaches are available:<br> * a) Use the Checksum only and take the risk that two images might have * the same CRC32 sum, although they are not the same.<br> * b) Use a second (or third) Checksum algorithm to minimise the chance * that two images have the same Checksums but are not equal (e.g. * CRC32, MD5 and SHA-1 Checksums, Additionally compare the * data-byte-array lengths). */ if (obj == this) { return(true); } if (obj == null) { return(false); } if (!(obj is XWPFPictureData)) { return(false); } XWPFPictureData picData = (XWPFPictureData)obj; PackagePart foreignPackagePart = picData.GetPackagePart(); PackagePart ownPackagePart = this.GetPackagePart(); if ((foreignPackagePart != null && ownPackagePart == null) || (foreignPackagePart == null && ownPackagePart != null)) { return(false); } if (ownPackagePart != null) { OPCPackage foreignPackage = foreignPackagePart.Package; OPCPackage ownPackage = ownPackagePart.Package; if ((foreignPackage != null && ownPackage == null) || (foreignPackage == null && ownPackage != null)) { return(false); } if (ownPackage != null) { if (!ownPackage.Equals(foreignPackage)) { return(false); } } } long foreignChecksum = picData.Checksum; long localChecksum = Checksum; if (!(localChecksum.Equals(foreignChecksum))) { return(false); } return(Arrays.Equals(this.Data, picData.Data)); }