public void TestRead() { XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("VariousPictures.docx"); IList <XWPFPictureData> pictures = sampleDoc.AllPictures; Assert.AreEqual(5, pictures.Count); String[] ext = { "wmf", "png", "emf", "emf", "jpeg" }; for (int i = 0; i < pictures.Count; i++) { Assert.AreEqual(ext[i], pictures[(i)].suggestFileExtension()); } int num = pictures.Count; byte[] pictureData = XWPFTestDataSamples.GetImage("nature1.jpg"); String relationId = sampleDoc.AddPictureData(pictureData, (int)PictureType.JPEG); // picture list was updated Assert.AreEqual(num + 1, pictures.Count); XWPFPictureData pict = (XWPFPictureData)sampleDoc.GetRelationById(relationId); Assert.AreEqual("jpeg", pict.suggestFileExtension()); Assert.IsTrue(Arrays.Equals(pictureData, pict.GetData())); }
public override bool Equals(object obj) { if (obj == this) { return(true); } if (obj == null || !(obj is XWPFPictureData)) { return(false); } XWPFPictureData xwpfPictureData = (XWPFPictureData)obj; PackagePart packagePart1 = xwpfPictureData.GetPackagePart(); PackagePart packagePart2 = this.GetPackagePart(); if (packagePart1 != null && packagePart2 == null || packagePart1 == null && packagePart2 != null) { return(false); } if (packagePart2 != null) { OPCPackage package1 = packagePart1.Package; OPCPackage package2 = packagePart2.Package; if (package1 != null && package2 == null || package1 == null && package2 != null || package2 != null && !package2.Equals((object)package1)) { return(false); } } if (!this.Checksum.Equals(xwpfPictureData.Checksum)) { return(false); } return(Arrays.Equals((object)this.GetData(), (object)xwpfPictureData.GetData())); }
public XWPFPictureData FindPackagePictureData(byte[] pictureData, int format) { long checksum = IOUtils.CalculateChecksum(pictureData); XWPFPictureData xwpfPictureData = (XWPFPictureData)null; List <XWPFPictureData> xwpfPictureDataList = (List <XWPFPictureData>)null; if (this.packagePictures.ContainsKey(checksum)) { xwpfPictureDataList = this.packagePictures[checksum]; } if (xwpfPictureDataList != null) { IEnumerator <XWPFPictureData> enumerator = (IEnumerator <XWPFPictureData>)xwpfPictureDataList.GetEnumerator(); while (enumerator.MoveNext() && xwpfPictureData == null) { XWPFPictureData current = enumerator.Current; if (Arrays.Equals((object)pictureData, (object)current.GetData())) { xwpfPictureData = current; } } } return(xwpfPictureData); }
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.GetData(), picData.GetData())); }
public void TestNew() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("EmptyDocumentWithHeaderFooter.docx"); byte[] jpegData = XWPFTestDataSamples.GetImage("nature1.jpg"); byte[] gifData = XWPFTestDataSamples.GetImage("nature1.gif"); byte[] pngData = XWPFTestDataSamples.GetImage("nature1.png"); IList <XWPFPictureData> pictures = doc.AllPictures; Assert.AreEqual(0, pictures.Count); // Document shouldn't have any image relationships Assert.AreEqual(13, doc.GetPackagePart().Relationships.Size); foreach (PackageRelationship rel in doc.GetPackagePart().Relationships) { if (rel.RelationshipType.Equals(XSSFRelation.IMAGE_JPEG.Relation)) { Assert.Fail("Shouldn't have JPEG yet"); } } // Add the image String relationId = doc.AddPictureData(jpegData, (int)PictureType.JPEG); Assert.AreEqual(1, pictures.Count); XWPFPictureData jpgPicData = (XWPFPictureData)doc.GetRelationById(relationId); Assert.AreEqual("jpeg", jpgPicData.suggestFileExtension()); Assert.IsTrue(Arrays.Equals(jpegData, jpgPicData.GetData())); // Ensure it now has one Assert.AreEqual(14, doc.GetPackagePart().Relationships.Size); PackageRelationship jpegRel = null; foreach (PackageRelationship rel in doc.GetPackagePart().Relationships) { if (rel.RelationshipType.Equals(XWPFRelation.IMAGE_JPEG.Relation)) { if (jpegRel != null) { Assert.Fail("Found 2 jpegs!"); } jpegRel = rel; } } Assert.IsNotNull(jpegRel, "JPEG Relationship not found"); // Check the details Assert.AreEqual(XWPFRelation.IMAGE_JPEG.Relation, jpegRel.RelationshipType); Assert.AreEqual("/word/document.xml", jpegRel.Source.PartName.ToString()); Assert.AreEqual("/word/media/image1.jpeg", jpegRel.TargetUri.OriginalString); XWPFPictureData pictureDataByID = doc.GetPictureDataByID(jpegRel.Id); byte[] newJPEGData = pictureDataByID.GetData(); Assert.AreEqual(newJPEGData.Length, jpegData.Length); for (int i = 0; i < newJPEGData.Length; i++) { Assert.AreEqual(newJPEGData[i], jpegData[i]); } // Save an re-load, check it appears doc = XWPFTestDataSamples.WriteOutAndReadBack(doc); Assert.AreEqual(1, doc.AllPictures.Count); Assert.AreEqual(1, doc.AllPackagePictures.Count); }