SharePoint 2013: Enabling PDF Previews in Document Libraries with Office Web Apps 2013

Tags: SharePoint 2013, Office Web Apps

Introduction

A couple of weeks back I blogged about the March Update for Office Web Apps 2013 and also how you could use that update to show PDF previews in a SharePoint 2013 Search Center. Since then I’ve received a lot of requests on how to enable PDF Previews in a Document Library, which isn’t there by default. Of course it is not a WAC thing, it’s a SharePoint 2013 thing – but the SharePoint 2013 updates (up until now at least) does not provide this capability either.

In this post I will show you that it can be done. It’s a JavaScript thing and can be done using a Content Editor Web Part added on all pages where you want the PDF previews or as Farm solution which uses a delegate control and a custom JavaScript file.

[Update 2014-01-01] Some customers may see errors when using this solution and previewing PDFs. If so, make sure that you have the http://support.microsoft.com/kb/2825665 hotfix installed. Thanks to Dan from MSFT Support.

Build the PDF Preview solutionhe

I assume that you are familiar with SharePoint 2013 development and knows what a delegate control is. What you need to do is create a new empty Farm solution project. In this project we’ll create a new web control that will add a JavaScript (which we will create in just a minute) to the page. The implementation should look like below:

[MdsCompliant(true)]
public class PdfPreviewControl: WebControl
{
    protected override void OnPreRender(EventArgs e)
    {
        ScriptLink.RegisterScriptAfterUI(this.Page, "wictor.pdfpreviews/previews.js", false);
    }
}

The JavaScript file, added as a Layouts file, is what makes the magic happen. We’re using the Script-On-Demand features in this script to make sure that the scripts aren’t executed before the SharePoint filepreview.js file is loaded. Once that file is loaded two new JavaScript objects are created; the filePreviewManager and the embeddedWACPreview. To enable the PDF previews we only need to add the PDF extension to these objects and specify the previewer objects and the dimensions. In this case I use the same settings as the other Word previewers.

function WictorsPdfPreviews() {
    SP.SOD.executeOrDelayUntilScriptLoaded(function () {
        filePreviewManager.previewers.extensionToPreviewerMap.pdf = 
            [embeddedWACPreview, WACImagePreview];
        embeddedWACPreview.dimensions.pdf= { width: 379, height: 252}
    }, "filepreview.js");
    notifyScriptsLoadedAndExecuteWaitingJobs("wictor.pdfpreviews/previews.js");
}
WictorsPdfPreviews();

Now we need to make sure that this control loads the JavaScript on all pages. This is done by adding a new Empty Element SPI and creating a Control element pointing to the web control, like this:

<Control 
  ControlAssembly="$SharePoint.Project.AssemblyFullName$" 
  ControlClass="Wictor.PdfPreviews.PdfPreviewControl" 
  Id="AdditionalPageHead" 
  Sequence="100"/>

As you can see we’re adding this control to the AdditionalPageHead, which means that we will have it on every page. Do not forget to add the web control as a Safe Control in the project!

The final thing we need to do is to modify the Feature that was automatically created when the Empty Element SPI was added to the project. You can scope it to whatever you like, but I want it for all Document Libraries in my farm so I set the scope to Farm. The image below shows all the files in the project.

The Visual Studio 2012 solution

Deploy and Test

Now all we have to do is to deploy the solution. Once the solution is deployed and the Farm feature activated we can navigate to any document library and upload a PDF file. Note that you have to be on at least the March 2013 update of Office Web Apps Server and that you have enabled the WordPDF application (see previous blog post). Once you have the PDF file in the library you can click on the ellipsis button and see the PDF Preview:

PDF Previews in SharePoint 2013

Disclaimer

As always when it comes to stuff that is not documented. I do not give any guarantee that this will work on your machine(s) or after any upcoming SharePoint patches.

Summary

Enabling PDF Previews are not (yet) a default feature of SharePoint 2013 but can easily be added to your SharePoint farm – if you’re allowed to use Full Trust solutions.  If you don’t feel like you want to do some hacking yourself you can download the WSP here and deploy it yourself to try it out.

21 Comments

  • Christian said

    Cool ! Good post.

    Another way may be to create a CustomAction with Location="ScriptLink" and using a ScriptSrc attribute to declare the javascript on your pages.

    Thanks though, good tip !

    Christian

  • Xuerong Chen said

    Hi,
    I download the WSP file,deployed the solution,but can not find it in site collections when I want to activate the feature !

  • Xuerong Chen said

    Hi,
    Sorry,I download the WSP file,deployed the solution,activated the feature in the farm,but can not preview the PDF file, it says can not display the page.

  • matthew moore said

    sorry i am new to sharepoint but is there any documentation on how to install this to the farm ? I downloaded the wsp file but finding it hard to install.

  • Volker said

    Hi Wictor,

    thanks for this great post. But I have a little problem with your solution. The solution itselfs works great, but if i open the pdf preview i will see an link "open" to open the document. If i have no pdf software installed the link whats to open the pdf with the web apps. This should work, but the web app url contains an "action=edit" (or somethink like this) and the web app does not start and we get an error (in uls we see somthing with url convertion failed). If i take the generated url and remove the action parameter the document will open read only perfectly. If i then switch to modify the word web app will ask if we wants to save the pdf as doc to modify.
    I think the problem is that a direct "edit" link will not work with pdf. Have you an idea to remove the action parameter from the open link?

  • Oxy said

    this is a solution - pdf preview will be enable in doc library. but what's about other types of documents? is it possible to realize thumbnails for example for office documents, something like this: http://www.harepoint.com/Products/HarePointThumbnails/Default.aspx ?

  • Tim O'Pry said

    Do you know if this can be done for O365? I tried using your .WSP, but activation fails due to some invalid markups.

  • David Crabbe said

    Great article.

    However you should be aware that someone is passing off your articles as their own: http://moss-net4u.blogspot.com.au/2013/04/sharepoint-2013-enabling-pdf-previews.html

    They failed to change anything about the post so it was obvious from the namespace that it originated on your site.

  • Bruce said

    Tried to deploy the WSP but got back a "This solution contains invalid markup or elements that cannot be deployed as part of a sandboxed solution.". You other article was extremely helpful for getting the PDF preview in search results, libraries would be the icing on the cake!

  • Syed Shoaib Adil said

    hi,

    nice post, but tell me can it work without Office Web Apps? I want to do a demo from my development machine and we cannot install OFFICE WEB APPS on the server where we have SP2013 installed. Any suggestion for me for showing the document preview either PDF, Doc, Xls or jpg?

  • Paolo Sardi said

    Hi,
    I tried to load solution on my Office365 site collection and it resulted in the following error:

    Sorry, something went wrong
    This solution contains invalid markup or elements that cannot be deployed as part of a sandboxed solution. Solution manifest for solution '253fe421-73e0-41b5-89c4-ed65c5d7c0a4' failed validation, file manifest.xml, line 10, character 4: The element 'Solution' in namespace 'http://schemas.microsoft.com/sharepoint/' has invalid child element 'TemplateFiles' in namespace 'http://schemas.microsoft.com/sharepoint/'. List of possible elements expected: 'FeatureManifests, ActivationDependencies' in namespace 'http://schemas.microsoft.com/sharepoint/'.

    Can you help me to solve the problem?

    Thank you in advance.

  • Tobias said

    Works like a charm.
    Thank you so much for this elegant solution!
    Yout Blog is really Top5 for SharePoint.

    BTW: including the solution for download would maybe help some other guys to get it done quicker. ;-)

  • Dan - MSFT Support said

    I work with Microsoft Support and recently worked an issue with Wictor's WSP on SharePoint 2013. When attempting to click "open" on PDF previews generated from Office Web Apps 2013, the following error is shown:

    Sorry, something went wrong. An error has occurred on the server.

    This issue is resolved by installing the SharePoint 2013 October 2013 Cumulative Update:
    http://support.microsoft.com/kb/2825665

  • AL said

    Deployed to Farm, but does not seem to work. When I go to a document library the preview does not work, and there is no reference to the JS file in the source. Deployed as Web Feature, and still does not work.

  • Jason Robertson said

    To those using Office 365 it will not deploy because the dll would require full trust that is inside the .wsp. You will need to find a Sand boxed approach which I believe one of the earlier posters 2nd or 3rd post alluded to.

    This is server side code what ever this is doing would need to be done in the Sand Box or on the client using JavaScript and Html it is merely putting a link on a page but it is using server side code to do it. Research where the ScriptLink.RegisterScriptAfterUI places the script tag in the master page and duplicate in the master page using an ordinary javascript tag <script src="" />

    [MdsCompliant(true)]
    public class PdfPreviewControl: WebControl
    {
    protected override void OnPreRender(EventArgs e)
    {
    ScriptLink.RegisterScriptAfterUI(this.Page, "wictor.pdfpreviews/previews.js", false);
    }
    }

  • Sean Oldroyd said

    First let me start by thanking you, your work is amazing and I have implemented your pdf preview solution in my production Sharepoint farms. The pdf preview is a huge hit and your guide to implementing it along with your canned solution is great. I am having one issue with your solution implemented and the latest cumulative updates applied. I don’t have the December cumulative update applied.

    The issues is this since I applied the updates I cannot open pdf files directly in Adobe regardless of the configuration I try. I have edited the DocIcon.xml, modified all permissive/strict settings both at the webapp and library levels. The odd thing is that if I find the same pdf file in the search results it will open directly with Adobe which is what I want. I am not the only one experiencing this issue, below are a couple of links from others experiencing tis problem.

    http://social.technet.microsoft.com/Forums/lync/en-US/c184a8f8-9c91-4d56-a27f-cd076fb8f60e/pdf-files-which-stored-in-document-library-cannot-be-opened-directly-via-adobe-reader?forum=sharepointgeneral

    http://forums.adobe.com/message/6106427

    Any help would be greatly appreciated!

  • Manish Singh said

    Wictor, I am very new for document preview. actully i don't know how to enable office web app in my server. Is there another way to implement pdf preview for the document library.
    please tell me some times I get error for word document only. Please let me know what is the error in my machine.

Comments have been disabled for this content.

About Wictor...

Wictor Wilén is a Director and SharePoint Architect working at Connecta AB. Wictor has achieved the Microsoft Certified Architect (MCA) - SharePoint 2010, Microsoft Certified Solutions Master (MCSM) - SharePoint  and Microsoft Certified Master (MCM) - SharePoint 2010 certifications. He has also been awarded Microsoft Most Valuable Professional (MVP) for four consecutive years.

And a word from our sponsors...

SharePoint 2010 Web Parts in Action