7

I have a website where users upload Word documents and I want to display thumbnails of these word documents. If anyone of you knows how to display the first page of a Word file as an image using C# please tell me.

Also if you know a trusted .NET library to convert word files to images that requires no office interop that would be great.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
mona
  • 71
  • 1
  • 1
  • 3

3 Answers3

2

http://blogs.msdn.com/windowssdk/archive/2009/06/12/windows-api-code-pack-for-microsoft-net-framework.aspx

ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;

It's Microsoft's API Code Pack

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
1

I found this question (7 yrs later) while searching for a similar solution. I'm evaluating 2JPEG and it appears to support 275 formats including Word, Excel, Publisher & Powerpoint files. fCoder recommends running 2JPEG as a scheduled background task. The command line syntax is pretty comprehensive.

Here's a sample snippet to generate a thumbnail for a specific file:

2jpeg.exe -src "c:\files\myfile.docx" -dst "c:\files" -oper Resize size:"100 200" fmode:fit_width -options pages:"1" scansf:no overwrite:yes template:"{Title}_thumb.jpg" silent:yes
James Moberg
  • 4,360
  • 1
  • 22
  • 21
0

A preview image of the 1st page of a .doc or .docx document can easily be created with a tool called Free Spire.Doc for .NET (a totally free word API for commercial and personal use). I found it to be fast and accurate.

Note from the developer's page:

"The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013, Word 2016 and Word 2019) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc. Friendly Reminder: Free version is limited to 500 paragraphs and 25 tables... "

This C# code creates a System.Drawing.Image object of the 1st page of a .docx file:

using Spire.Doc

byte[] docContent = File.ReadAllBytes(@"C:\Temp\word_document.docx");

using (MemoryStream ms = new MemoryStream(docContent))
{
    // Creates a Spire.Doc object to work with
    Spire.Doc.Document doc = new Spire.Doc.Document(ms, Spire.Doc.FileFormat.Auto);
    // SaveToImages creates an array of System.Drawing.Image, we take only the 1st element
    System.Drawing.Image img = doc.SaveToImages(0, 1, Spire.Doc.Documents.ImageType.Bitmap)[0];
}

To create the thumbnail image, the following C# example includes a second using block to do it, and then converts to a base64 string:

using Spire.Doc

byte[] docContent = File.ReadAllBytes(@"C:\Temp\word_document.docx");

using (MemoryStream ms = new MemoryStream(docContent))
{
    // Creates a Spire.Doc object to work with
    Spire.Doc.Document doc = new Spire.Doc.Document(ms, Spire.Doc.FileFormat.Auto);
    // SaveToImages creates an array of System.Drawing.Image, we take only the 1st element
    System.Drawing.Image img = doc.SaveToImages(0, 1, Spire.Doc.Documents.ImageType.Bitmap)[0];

    using (var ms2 = new MemoryStream())
    {
        // Auxiliary object needed for GetThumbnailImage
        System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        // We create a thumbnail (0.5 width and height = 50%)
        img.GetThumbnailImage((int)(img.Width * 0.5), (int)(img.Height * 0.5), myCallback, IntPtr.Zero).Save(ms2, System.Drawing.Imaging.ImageFormat.Png);
        // Convert to Base64 string representation of the image
        return Convert.ToBase64String(ms2.ToArray());
    }
}

In addition, the library can also convert in other ways, for instance this function returns .SVG files with each page:

doc.SaveToFile("resulting_file_name.svg", Spire.Doc.FileFormat.SVG);
desertnaut
  • 57,590
  • 26
  • 140
  • 166
renzol
  • 183
  • 2
  • 8