Beispiel #1
0
        public void TestSetProperties()
        {
            String inputPath = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageThumbnail.docx");

            String imagePath = OpenXml4NetTestDataSamples.GetSampleFileName("thumbnail.jpg");

            FileInfo outputFile = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageThumbnailOUTPUT.docx");

            // Open namespace
            OPCPackage p  = OPCPackage.Open(inputPath, PackageAccess.READ_WRITE);
            FileStream fs = outputFile.OpenWrite();

            p.AddThumbnail(imagePath);
            // Save the namespace in the output directory
            p.Save(fs);

            // Open the newly Created file to check core properties saved values.
            OPCPackage p2 = OPCPackage.Open(outputFile.Name, PackageAccess.READ);

            if (p2.GetRelationshipsByType(PackageRelationshipTypes.THUMBNAIL)
                .Size == 0)
            {
                Assert.Fail("Thumbnail not Added to the namespace !");
            }
            p2.Revert();
            File.Delete(outputFile.Name);
        }
Beispiel #2
0
        /**
         * Sets the Thumbnail for the document, replacing any existing
         *  one.
         *
         * @param name The filename for the thumbnail image, eg <code>thumbnail.jpg</code>
         * @param imageData The inputstream to read the thumbnail image from
         */
        public void SetThumbnail(String filename, Stream imageData)
        {
            PackagePart tPart = ThumbnailPart;

            if (tPart == null)
            {
                // New thumbnail
                pkg.AddThumbnail(filename, imageData);
            }
            else
            {
                // Change existing
                String newType = ContentTypes.GetContentTypeFromFileExtension(filename);
                if (!newType.Equals(tPart.ContentType))
                {
                    throw new ArgumentException("Can't set a Thumbnail of type " +
                                                newType + " when existing one is of a different type " +
                                                tPart.ContentType);
                }
                StreamHelper.CopyStream(imageData, tPart.GetOutputStream());
            }
        }