• vista and office dev: creating preview handlers


    (long, informative post with images -- apologies aggregates...click on the link :-))

    in outlook 2007, when you have an attachement in a mail message and click on the attachment file, you are presented with an option to preview this attachment directly in outlook...just like the preview pane for a normal message:

    previewmode

    likewise in vista's file explorers, you can enable a preview pane and get the same functionality for highlighting a file in explorer as seen here:

    previewvista

    for productivity, namely in outlook 2007 for me, this is a great advancement to the platform.  i now don't have to double-click a file, etc and launch another program to see what usually is just a glance that is required.  this preview mode in the file explorer has existing in windows xp, but was little used by developers.  this is likely due to the warnings and caution from microsoft to not use managed code for this feature.  in winxp, this functionality is executed in process, thus making it a dangerous path when multiple versions of the CLR might be invovled.

    in vista, this is much more simplified and the model has changed to out of process...finally making it easier to do these things in managed code.  has an excellent article in the january edition of msdn magazine that covers this entire story and the in's and out's of preview handler development.  the great thing about his article aside from the depth of understanding you will gain is that stephen has created a managed interface to make it easier to implement your own preview handler.  you can download his code from the msdn preview handler code link here.  stephen encourages us to write our own preview handlers, but he's gone and done most of the ones i think we all use -- zip, xml, xaml, resx, csv, msi, internet explorer, etc.  they are all awesome.  the zip preview handler is especially cool since it also implements a treeview representation of the contents of the zip file...look at this sample showing the vista preview, but would be the same in outlook 2007 (note: this zip one also handles .gadget files):

    zippvwhandler

    one handler that stephen also provides is a pdf preview handler (probably the second one i will use the most next to the zip one).  in his sample, he uses the adobe activex control for the acrobat reader implementation.  now this would require that you have adobe reader installed.  if you are a frequent reader, you may know that i'm not a fan of adobe reader.  i use foxit reader from .  it's lean and mean.  i wanted to ensure i could still use the pdf preview handler, but had to modify a few things.

    first i had to get the foxit activex sdk from their site.  there is a license cost to use this, which i'm currently investigating.  after getting the activex control, i modified stephen's code at about line 41 of of the PdfPreviewHandler.cs file in the download.  i changed it to:

    // Foxit Reader ActiveX
    public PdfAxHost()
        : base("d46a7492-4b6c-446f-8100-4812edf406c9") { }
     

    then, i also had to change the pinvoke method.  the adobe control uses a LoadFile() signature, but foxit uses an OpenFile() signature.  foxit also uses a second parameter, the starting page of the document.  so i changed it to this code:

    _ocx.GetType().InvokeMember(
      "OpenFile", BindingFlags.InvokeMethod, null, // changed to OpenFile
       _ocx, new object[] { fileName, 0 }, CultureInfo.InvariantCulture); // added 0 parameter to array
     

    compiled and deployed...now i have pdf previewing in outlook 2007:

    foxitpreview

    it's a beautiful thing.  please check out stephen's article in msdn magazine -- it was a great read and well done -- i totally understood the concepts presented and it allowed me to create a few handlers of my own.  as a developer i frequently get code files sent as attachments.  i generally have to open them up and look at them in visual studio or notepad2 for code highlighting, etc.  well, with my new found knowledge i created a code preview handler.  it will see .cs, .vb, .js and .sql files and enable a previewer to show them in code-highlighted formatting in the preview window of outlook 2007 or windows vista explorer.  here's a sample:

    codeprievewer

    how is this accomplished?  well, aside from the great library stephen provided, i pulled together the HtmlApp code that nikhilk wrote, as well as the csharpformat from manoli and bundled them together to produce this.  i had some issues with manoli's library when trying to set the embed stylesheet property, but i just embedded it as a resource and that was fine by me.  it was easy to implement and here is the magical function code:

    public override void Load(FileInfo file)
    {
        StreamReader rdr = file.OpenText();
        string previewCode = rdr.ReadToEnd();
        string formattedCode = FormatCode(previewCode, file.Extension);
    
        HtmlApp.Html.HtmlControl html = new HtmlApp.Html.HtmlControl();
        html.LoadHtml(string.Format("<html><head><style type=\"text/css\">{0}</style><body>{1}</body></html>", SmilingGoat.PreviewHandlers.Properties.Resources.CssString, formattedCode));
        html.Dock = DockStyle.Fill;
    
        Controls.Add(html);
    }


    after this (the FormatCode funciton basically just does a switch on the file type and applies the appropriate formatting from manoli's object), the formatted code is rendered to view like the sample above.  it's really easy to implement.

    one thing important to note about preview handlers is the fact that only one preview handler can be associated with one file extension and class.  what does that mean? well, simple...for example, you can't have two pdf preview handlers.  well what happens if i install two of them? last one will likely win -- depending on how it is registering itself.  you see, there will only be one registry entry for that file type, so the second one you install will overwrite the settings from the first one.  stephen planned on posting a little utility to see what preview handlers are registered for your machine...check it out to see how things are registering and to actually add additional ones.  for example, if i didn't want code highlighting, i could have easily set the TXT preview handler to also preview the code files mentioned above.  here are the links to my code preview handler:

    File iconCode Preview Handler [source]
    File iconCode Preview Handler Setup [MSI]

    Wednesday, December 13, 2006 4:06 PM

    PostTypeIcon

Comments.

  • Gravatar
    # re: vista and office dev: creating preview handlers


    Nice handlers, Tim! I've put up the tool you mentioned at http://blogs.msdn.com/toub/archive/2006/12/14/preview-handler-association-editor.aspx

    12/14/2006 12:37 PM
  • Gravatar
    # re: vista and office dev: creating preview handlers


    Hi:
    I would like to create previewer for PDF files but am not a programmer. I installed Visual J# v2.0 but in attempting to install your MSI installer version I get the following error: Unable to get installer types in the C:...\codepreviewhandler.dll assembly. Unable to load one or more of the requested types. Can you give me any advice please.

    Also do I need to download anything from foxit?

    1/29/2007 4:59 PM
  • timheuer said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    fred: ensure you have the .NET framework version 2 installed as well (should be there). send me the full error offline and i'll see -- i've not seen this reported before.

    regarding the pdf one...i am awaiting an updated control from foxit.

    1/29/2007 10:05 PM
  • Gravatar
    # re: vista and office dev: creating preview handlers


    Tim:
    The only thing I didn't include from the message was: Retrieve the LoaderExcceptions property for more information. I do have .NET v2.0 & Visual J# 2.0 installed. With regard to pdf one, do you expect Foxit will comply? Thanks, Fred

    1/30/2007 12:25 PM
  • timheuer said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    fred: yes, i'm actually partnering with foxit to create it.

    1/31/2007 8:43 AM
  • Gravatar
    # re: vista and office dev: creating preview handlers


    great post!! i wanted to make the same reader; i'd love to know when you/foxit release the extension!

    2/12/2007 3:09 PM
  • JackO said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    Hi.
    I've tried to modify the
    PdfPreviewHandler.cs file but I get an error message below.
    Can someone tell me what I'm doing wrong?

    =========================================

    System.Runtime.InteropServices.COMException (0x80040154): Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
    at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
    at System.Windows.Forms.AxHost.CreateWithoutLicense(Guid clsid)
    at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)
    at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
    at System.Windows.Forms.AxHost.CreateInstance()
    at System.Windows.Forms.AxHost.GetOcxCreate()
    at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
    at System.Windows.Forms.AxHost.CreateHandle()
    at System.Windows.Forms.Control.get_Handle()
    at MsdnMag.PdfPreviewHandler.PdfAxHost.LoadFile(String fileName)
    at MsdnMag.PdfPreviewHandler.PdfPreviewHandlerControl.Load(FileInfo file)
    at MsdnMag.FileBasedPreviewHandler.Load(PreviewHandlerControl c)
    at MsdnMag.PreviewHandler.<MsdnMag.IPreviewHandler.DoPreview>b__2()

    2/13/2007 10:45 AM
  • timheuer said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    JackO: the MSDN PDF preview handler requires you to have the Adobe Acrobat Reader installed -- i am DAYS away from releasing one that does not --

    2/17/2007 9:04 PM
  • Gravatar
    # re: vista and office dev: creating preview handlers


    Not able to install on Vista RTM. I'm getting error code 2869. I've tried this to no avail:
    http://blogs.conchango.com/pauloreichert/archive/2006/11/21/Windows-Installer-MSI-packages-error-code-2869-on-Windows-Vista.aspx

    3/1/2007 6:44 PM
  • Gravatar
    # re: vista and office dev: creating preview handlers


    Hi Tim,

    I'm trying to make a previewer to run in Outlook 2007, but without Vista. Do you have any ideas how to do that?

    TIA

    3/2/2007 4:50 PM
  • cad said:
    Gravatar
    # preview handlers in XP?


    hi,may i know if the File Preview Handler Setup [MSI] can be used in XP? Apparently, I have went ahead with the installation but no preview in outlook 2007 for pdf and html. thanks in advance!

    3/5/2007 6:56 AM
  • Tim Misiak said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    I get the same error as Jon Galloway, error code 2869. Any idea what is causing this or how to fix it?

    3/26/2007 4:39 PM
  • franck said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    nope.. same error here.

    4/4/2007 10:23 AM
  • timheuer said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    if you are installing on Vista with user account control enabled, oyu have to elevate to a command prompt and then run msiexe /i <pathtoMSI>

    i did change the MSI installer so it should prompt automatically, so you may want to re-download and try both methods.

    4/4/2007 10:32 AM
  •  said:
    Gravatar
    # fgqufvmd - Google Search


    fgqufvmd - Google Search

    4/13/2007 2:37 PM
  •  said:
    Gravatar
    # jemucfed - Google Search


    jemucfed - Google Search

    4/20/2007 9:21 AM
  • Gil Azar said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    Hi,
    I've created a bunch of preview handlers, including those displayed here (I used Acrobat instead of Foxit), and put them (and their source code) in here:
    http://www.azarfamily.org/previewhandlersforwindowsxp

    Enjoy :)

    5/19/2007 3:22 PM
  • indra said:
    Gravatar
    # re: vista and office dev: creating preview handlers


    Is it possible to view the preview of files in a custom application in Vista.
    Not in the expolorer

    5/29/2007 4:07 AM
  • Gravatar
    # re: vista and office dev: creating preview handlers


    ok

    6/7/2007 8:04 PM
  •  said:
    Gravatar
    # MSDN Blog Postings &amp;raquo; Preview Handlers in Vista and Outlook


    MSDN Blog Postings &amp;raquo; Preview Handlers in Vista and Outlook

    2/2/2008 10:04 PM
Comments have been closed on this topic.