[Solved] How can I get the thumbnail of a document?

Creating a macro - Writing a Script - Using the API

[Solved] How can I get the thumbnail of a document?

Postby yindali » Mon Jun 07, 2010 5:29 am

I want to display the thumbnail of ODF document in my program. Currently I plan to export pages to image file to get the thumbnail.
Is there an API allowing the developer to get the image memory directly?
Last edited by Hagar Delest on Mon Jun 07, 2010 10:17 am, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.1 on Windows XP
yindali
 
Posts: 14
Joined: Fri Oct 30, 2009 11:12 am

Re: How can I get the thumbnail of a document?

Postby B Marcelly » Mon Jun 07, 2010 8:28 am

Hi,
Old documents may not contain a thumbnail, you have to check its presence.
Interfaces com.sun.star.document.XStorageBasedDocument and com.sun.star.embed.XStorage are used to get a document stored in a document.
Code: Select all   Expand viewCollapse view
Sub Demo
if thumbnailFound(ThisComponent)  then
  MsgBox("Thumbnail copied")
else
  MsgBox("No thumbnail")
end if
End Sub


Function thumbnailFound(doc As Object) As Boolean
' returns False if no thumbnail
' returns True if thumbnail found and copied in same folder
Dim ds As Object, th As Object, streamTh As Object, f1 As Object

thumbnailFound = False
ds = doc.getDocumentStorage()
if ds.hasByName("Thumbnails")  then
  th = ds.getByName("Thumbnails")
  if th.hasByName("thumbnail.png")  then
    streamTh = th.getByName("thumbnail.png")
    f1 = createUnoService("com.sun.star.ucb.SimpleFileAccess")
    f1.writeFile(doc.URL & ".png", streamTh)
    thumbnailFound = True
  end if
end if
End Function
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: How can I get the thumbnail of a document?

Postby yindali » Mon Jun 07, 2010 10:16 am

Thank you Bernard. I solved this question according to your clue.

This is my java code:

Code: Select all   Expand viewCollapse view
XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class,
               xComponent);
XStorageBasedDocument xDocStorage = (XStorageBasedDocument) UnoRuntime.queryInterface(XStorageBasedDocument.class, xModel);
XStorage xStorage = xDocStorage.getDocumentStorage();
if (xStorage.hasByName(THUMBNAIL_DIR)) {
   XStorage xThumb = xStorage.openStorageElement(THUMBNAIL_DIR,
         ElementModes.READ);
   if (xThumb.hasByName(THUMBNAIL_NAME)) {
      XStream xStream = xThumb.openStreamElement(THUMBNAIL_NAME, ElementModes.READ);
         XComponentContext xContext = getContext();
         XMultiComponentFactory xMCF = xContext.getServiceManager();
         Object obj = xMCF.createInstanceWithContext(
                  "com.sun.star.ucb.SimpleFileAccess", xContext);

         XSimpleFileAccess2 f2 = (XSimpleFileAccess2) UnoRuntime.queryInterface(XSimpleFileAccess2.class, obj);
         f2.writeFile(filename + ".png", xStream.getInputStream());
   }
xThumb.dispose();
OpenOffice 3.1 on Windows XP
yindali
 
Posts: 14
Joined: Fri Oct 30, 2009 11:12 am

Re: [Solved] How can I get the thumbnail of a document?

Postby caoticud » Wed Feb 29, 2012 3:41 pm

This piece of code works for OpenOffice documents but not for MS Office ones. Could you please give an example how to extract the preview thumbnail from a MS office doc ?
OpenOffice 3.3 on Windows XP
LibreOffice 3.5 on Windows XP
MS Office 2010 on Windows XP
caoticud
 
Posts: 3
Joined: Tue Feb 21, 2012 12:18 pm

Re: [Solved] How can I get the thumbnail of a document?

Postby RoryOF » Wed Feb 29, 2012 4:27 pm

The macro languages are different between OpenOffice and Excel. Google to see if you can find a ready-to-go Excel macro; our expertise on this Forum is in OpenOffice.
Apache OpenOffice 4.1.7 on Xubuntu 18.04.4 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 31079
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland


Return to Macros and UNO API

Who is online

Users browsing this forum: Zizi64 and 4 guests