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

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.

Build the PDF Preview solution

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:

1
2
3
4
5
6
7
8
[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 theembeddedWACPreview. 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.

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
<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

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.

One comment

  1. mihalych says:

    If i understand correct it’s a feature is available for SharePoint 2013, right? The same capability (pdf preview, also ms office docs preview and other) may be realized with thumbnailer solution for sharepoint libraries:
    http://www.harepoint.com/Products/HarePointThumbnails/Default.aspx
    May be it helps somebody who need documetns preview feature in SP 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *