Browse Source

POI:#59717 POIXMLProperties helper methods for reading and changing OOXML document thumbnails

pull/290/head
antony liu 6 years ago
parent
commit
4945daaa47
  1. 84
      ooxml/POIXMLProperties.cs
  2. 45
      openxml4Net/OPC/OPCPackage.cs
  3. 40
      testcases/ooxml/TestPOIXMLProperties.cs

84
ooxml/POIXMLProperties.cs

@ -514,9 +514,11 @@ namespace NPOI
return null;
}
}
/**
* Wrapper around the two different kinds of OOXML properties
* a document can have
* Wrapper around the three different kinds of OOXML properties
* and metadata a document can have (Core, Extended and Custom),
* as well Thumbnails.
*/
public class POIXMLProperties
{
@ -615,6 +617,84 @@ namespace NPOI
}
}
/**
* Returns the {@link PackagePart} for the Document
* Thumbnail, or <code>null</code> if there isn't one
*
* @return The Document Thumbnail part or null
*/
protected internal PackagePart ThumbnailPart
{
get
{
PackageRelationshipCollection rels =
pkg.GetRelationshipsByType(PackageRelationshipTypes.THUMBNAIL);
if (rels.Size == 1)
{
return pkg.GetPart(rels.GetRelationship(0));
}
return null;
}
}
/**
* Returns the name of the Document thumbnail, eg
* <code>thumbnail.jpeg</code>, or <code>null</code> if there
* isn't one.
*
* @return The thumbnail filename, or null
*/
public String ThumbnailFilename
{
get
{
PackagePart tPart = ThumbnailPart;
if (tPart == null) return null;
String name = tPart.PartName.Name;
return name.Substring(name.LastIndexOf('/'));
}
}
/**
* Returns the Document thumbnail image data, or
* <code>null</code> if there isn't one.
*
* @return The thumbnail data, or null
*/
public Stream ThumbnailImage
{
get
{
PackagePart tPart = ThumbnailPart;
if (tPart == null) return null;
return tPart.GetInputStream();
}
}
/**
* 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());
}
}
/**
* Commit Changes to the underlying OPC namespace
*/

45
openxml4Net/OPC/OPCPackage.cs

@ -445,23 +445,35 @@ namespace NPOI.OpenXml4Net.OPC
RevertImpl();
}
/**
* Add a thumbnail to the package. This method is provided to make easier
* the addition of a thumbnail in a package. You can do the same work by
* using the traditionnal relationship and part mechanism.
*
* @param path
* The full path to the image file.
*/
/// <summary>
/// Add a thumbnail to the package. This method is provided to make easier
/// the addition of a thumbnail in a package. You can do the same work by
/// using the traditionnal relationship and part mechanism.
/// </summary>
/// <param name="path">path The full path to the image file.</param>
public void AddThumbnail(String path)
{
// Check parameter
if ("".Equals(path))
if (string.IsNullOrEmpty(path))
throw new ArgumentException("path");
String name = path.Substring(path.LastIndexOf(Path.DirectorySeparatorChar) + 1);
// Get the filename from the path
String filename = path
.Substring(path.LastIndexOf('\\') + 1);
FileStream is1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
AddThumbnail(name, is1);
is1.Close();
}
/// <summary>
/// Add a thumbnail to the package. This method is provided to make easier
/// the addition of a thumbnail in a package. You can do the same work by
/// using the traditionnal relationship and part mechanism.
/// </summary>
/// <param name="filename"></param>
/// <param name="data"></param>
public void AddThumbnail(String filename, Stream data)
{
// Check parameter
if (string.IsNullOrEmpty(filename))
throw new ArgumentException("filename");
// Create the thumbnail part name
String contentType = ContentTypes
@ -474,11 +486,11 @@ namespace NPOI.OpenXml4Net.OPC
}
catch (InvalidFormatException)
{
String partName = "/docProps/thumbnail" +
filename.Substring(filename.LastIndexOf(".") + 1);
try
{
thumbnailPartName = PackagingUriHelper
.CreatePartName("/docProps/thumbnail"
+ path.Substring(path.LastIndexOf(".") + 1));
thumbnailPartName = PackagingUriHelper.CreatePartName(partName);
}
catch (InvalidFormatException)
{
@ -501,8 +513,7 @@ namespace NPOI.OpenXml4Net.OPC
PackageRelationshipTypes.THUMBNAIL);
// Copy file data to the newly Created part
StreamHelper.CopyStream(new FileStream(path, FileMode.Open), thumbnailPart
.GetOutputStream());
StreamHelper.CopyStream(data, thumbnailPart.GetOutputStream());
}
/**

40
testcases/ooxml/TestPOIXMLProperties.cs

@ -25,6 +25,7 @@ namespace NPOI
using NPOI.XSSF;
using NPOI.XWPF.UserModel;
using NPOI.XWPF;
using NPOI.Util;
/**
* Test Setting extended and custom OOXML properties
@ -33,6 +34,7 @@ namespace NPOI
public class TestPOIXMLProperties
{
private XWPFDocument sampleDoc;
private XWPFDocument sampleNoThumb;
private POIXMLProperties _props;
private CoreProperties _coreProperties;
@ -40,6 +42,9 @@ namespace NPOI
public void SetUp()
{
sampleDoc = XWPFTestDataSamples.OpenSampleDocument("documentProperties.docx");
sampleNoThumb = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
Assert.IsNotNull(sampleDoc);
Assert.IsNotNull(sampleNoThumb);
_props = sampleDoc.GetProperties();
_coreProperties = _props.CoreProperties;
Assert.IsNotNull(_props);
@ -50,6 +55,7 @@ namespace NPOI
public void closeResources()
{
sampleDoc.Close();
sampleNoThumb.Close();
}
[Test]
@ -219,6 +225,40 @@ namespace NPOI
return utcString.Equals(dateTimeUtcString);
}
[Test]
public void testThumbnails()
{
POIXMLProperties noThumbProps = sampleNoThumb.GetProperties();
Assert.IsNotNull(_props.ThumbnailPart);
Assert.IsNull(noThumbProps.ThumbnailPart);
Assert.IsNotNull(_props.ThumbnailFilename);
Assert.IsNull(noThumbProps.ThumbnailFilename);
Assert.IsNotNull(_props.ThumbnailImage);
Assert.IsNull(noThumbProps.ThumbnailImage);
Assert.AreEqual("thumbnail.jpeg", _props.ThumbnailFilename);
// Adding / changing
noThumbProps.SetThumbnail("Testing.png", new ByteArrayInputStream(new byte[1]));
Assert.IsNotNull(noThumbProps.ThumbnailPart);
Assert.AreEqual("Testing.png", noThumbProps.ThumbnailFilename);
Assert.IsNotNull(noThumbProps.ThumbnailImage);
//Assert.AreEqual(1, noThumbProps.ThumbnailImage.Available());
Assert.AreEqual(1, noThumbProps.ThumbnailImage.Length - noThumbProps.ThumbnailImage.Position);
noThumbProps.SetThumbnail("Testing2.png", new ByteArrayInputStream(new byte[2]));
Assert.IsNotNull(noThumbProps.ThumbnailPart);
Assert.AreEqual("Testing.png", noThumbProps.ThumbnailFilename);
Assert.IsNotNull(noThumbProps.ThumbnailImage);
//Assert.AreEqual(2, noThumbProps.ThumbnailImage.Available());
Assert.AreEqual(2, noThumbProps.ThumbnailImage.Length - noThumbProps.ThumbnailImage.Position);
}
private static String ZeroPad(long i)
{
if (i >= 0 && i <= 9)

Loading…
Cancel
Save