DrLeary

Hi!

I'm working on a project where the client will be putting all their document templates in a sentral document library and this library will then be used as a source of document templates in Word as described in this article: http://technet2.microsoft.com/Office/en-us/library/1f04f451-8537-4434-8789-ad54dca7652b1033.mspx mfr=true

The templates that come with Office all have descriptions and thumbnails generated that make it easier to identify the right template when choosing from the list of templates, and you can ofcourse generate your own thumbnails for custom templates, but I would like to know if there is a way of generating images from say a Word document programmatically (Ie. not by pressing print screen and then dumping the image in an image editor)

I've found a few programs out there that generate thumbnail images from pdf documents, but nothing from office documents. Does anybody know a way to do this I suppose I could convert the document to pdf and use on of these tools to generate image from the pdf, but this seems a bit inefficient... The best scenario would be if there is somebody out there who have made this functionality so I can just add a .dll to my project in VS and handle everything in code, but so far I haven't seen any such software...




Re: SharePoint - Development and Programming How to generate thumbnail images from office documents?

Curtis Ruppe (MicroStaff IT)

First the good news. If you are using Office 2007, the OpenXML formats allow for this sort of thing. The bad news: you will probably have to purchase something to get a reliable image. Here's how to implement it once you have the DLL / .NET code to perform this action.

Create a new column for the preview image (most likely a custom type to allow the storage and preview of the image: http://msdn2.microsoft.com/en-us/library/bb417414.aspx). Using the SPItemEventReceiver ItemAdded / ItemUpdated run the .NET code (or your custom .NET code against the provided DLL) against the contents of the properties.ListItem.File object. To avoid holding up the user, I purposely chose the asynchronous events, but you can certainly use the synchronous events.

If you want to try to create your own image as part of this environment, feel free to look into the OpenXML schema: http://msdn2.microsoft.com/en-us/office/bb265236.aspx





Re: SharePoint - Development and Programming How to generate thumbnail images from office documents?

DrLeary

It turns out that (atleast) Word creates it's own thumbnail if you check off the 'Save preview picture' box in the document properties. This image is put in the 'docProps' folder inside the zip package and named 'Thumbnail.wmf'. Now I only need to extract this image from the package...

I decided to make an eventhandler that extracts the image and puts it in a folder in the document library. I can get a reference to the document and use a ZipPackage to work with it using this code:

Code Snippet

public class DokumentmalHandler : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPFile newFile = properties.ListItem.File;
Stream fileStream = webFile.OpenBinaryStream();
ZipPackage zipPack = (ZipPackage)ZipPackage.Open(fileStream, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
}

}

But I can't figure out how to copy the file from inside the package to the folder... Any ideas of what methods could be used






Re: SharePoint - Development and Programming How to generate thumbnail images from office documents?

Curtis Ruppe (MicroStaff IT)

Unfortunately, SharePoint doesn't allot for multiple files as part of the same list item, and there is no "image" or "binary" column you can define as a field within the context of SharePoint. With that being said, you will probably have to create a custom type for this purpose. The custom type would only be rendered on the AllItems.aspx page (not the View or Edit pages). This custom type would include the code you have placed within the ItemAdded method.





Re: SharePoint - Development and Programming How to generate thumbnail images from office documents?

DrLeary

I don't really need to do anything fancy with the image, just put it in a folder and then I can use a single line of text field in the list to reference where the image is located. I then use this string to build the XML that tells Word where the template thumbnail is etc. But I can't find a SIMPLE way to extract just this one file from a zip archive in C# without using silly J# or 3rd party assemblies. Surely there must be a way of extracting files from a zip using native .Net I even have .Net 3.0 on this project.

I foresee another problem though when I find this method that will allow me to extract files, and that is the destination path... Maybe it could work by extracting to the .Net temp folder and then adding it to the document library from there Or some way of just keeping it in memory as a binary stream and then using the SP object model to add it to the document library.






Re: SharePoint - Development and Programming How to generate thumbnail images from office documents?

Curtis Ruppe (MicroStaff IT)

Oh, look into those links I sent you earlier. Basically you will use the Packaging API (from WindowsBase.dll provided by .NET 3.0).