locked
Save Word Document pages as images RRS feed

  • Question

  • User1318172346 posted

    In our application, we are allowing users to upload word documents. I need to create thumbnail image of the first page of those uploaded documents. I couldn't find a way to enable "Save Preview Picure" option of the documents.Even if I use my own template, since I am not creating new documents,the uploaded documents use"normal.dot" only. I could copy the first page's content into Clipboard (using Microsoft.Office.Interoperop.Word). But there is no option to convert rtf datatype to bitmap datatype, so I couldn't save it as image.

    Is there any way to do it? Does Visual Studio Tools for Office has those options?

    Thanks. 

     

    Wednesday, May 28, 2008 2:20 PM

Answers

  • User1318172346 posted

    I figured the solution to this problem. After copying the first page's content into Clipboard (using Microsoft.Office.Interoperop.Word), we can use native Win32 dll (user32.dll) to get access to the clipboard. Check whether the image is in EnhancedMetaFile format. If so, you can save it as an image using the following code.  If you want to save it directly as thumbnail, you can do it .But remember to give declarations for all the functions in user32.dll that you are going to call.

    Public Class ClipboardAPI

    <DllImport("user32.dll", EntryPoint:="OpenClipboard", _

    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean

    End Function

    <DllImport("user32.dll", EntryPoint:="EmptyClipboard", _

    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function EmptyClipboard() As Boolean

    End Function

    <DllImport("user32.dll", EntryPoint:="SetClipboardData", _

    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function SetClipboardData(ByVal uFormat As Integer, ByVal ByValhWnd As IntPtr) As IntPtr

    End Function

    <DllImport("user32.dll", EntryPoint:="CloseClipboard", _

    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function CloseClipboard() As Boolean

    End Function

    <DllImport("user32.dll", EntryPoint:="GetClipboardData", _

    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function GetClipboardData(ByVal uFormat As Integer) As IntPtr

    End Function

    <DllImport("user32.dll", EntryPoint:="IsClipboardFormatAvailable", _

    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As Integer) As Short

    End Function

    End Class

     

    Const C_ENHMETAFILE As Integer = 14

    Try

    Dim ip As IntPtr

    Dim metaFile As System.Drawing.Imaging.Metafile = Nothing

    If ClipboardAPI.OpenClipboard(0) Then

    If ClipboardAPI.IsClipboardFormatAvailable(C_ENHMETAFILE) <> 0 Then

    ip = ClipboardAPI.GetClipboardData(C_ENHMETAFILE)

    If Not IntPtr.Zero.Equals(ip) Then

    metaFile = New System.Drawing.Imaging.Metafile(ip, True)

    End If

    Dim image As System.Drawing.Image = metaFile

    Dim objImageWriter As Image = New Bitmap(image.Width, image.Height)

    Dim objGraphics As Graphics = Graphics.FromImage(objImageWriter)

    objGraphics.DrawImage(image, 0, 0, image.Width, image.Height)

    objImageWriter.Save("[location]\test.png")      // You can either save it directly or you can create thumbnail using

    objImageWriter.GetThumbnailImage([ThumbnailWidth],[ThumbnailHeight], nothing, IntPtr.Zero)

    Return objImageWriter

    End If

    End If

    Catch ex As Exception

    Finally

    ClipboardAPI.EmptyClipboard()

    ClipboardAPI.CloseClipboard()

    End Try

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 30, 2008 11:39 AM