Contact Us
Please fill out the form below and we will get in touch within four business hours.

*
*
*
*
*
*
*Interested In:
(Check all that apply)



 
 
Synergy Corporate Technologies
Synergyonline > Blog > Posts > Open PDF Files in a New Window
August 27

Open PDF Files in a New Window

A request that I get occasionally is how to force SharePoint to open up PDF files up in a new window.  Assuming you have Adobe Reader installed, the default behavior is to prompt the user to open the PDF file.  For some customers, they want it to automatically open up in the same browser window.  This is easy to do by following the answer shown here. (Note: this does assume that the browser plug-in is installed and enabled).

For me, I like this solution and simply use the back button when I’m done and want to return to SharePoint.  If I want it in a new window, I either hold down Ctrl and click the link or just right-click the link and select Open in New Window.  Basic browser stuff, right?  In most cases, I convince the customer that this is a training issue on how to use the browser. However, that doesn’t always fly with every customer, does it?

If you to force the PDF file to load into a new browser window, keep reading...

There are a number of posts on workarounds to the problem.  One says that you should write a httphandler.  Not a bad solution, very thorough, but a complex one unless your a seasoned SharePoint and ASP.NET developer.  Another talks about editing the global onet.xml file (gasp!) which is not supported by Microsoft and this targets much more than just PDF files.  Others suggest a somewhat elegant JavaScript solution.  This searches through the <A> tags on the page and inserts a target attribute and sets it to _blank which opens it in a new window.  The problem with this is that the target attribute is deprecated when using the strict XHTML doctype which will present problem with SharePoint 2010 sites.

I think I have a pretty-good workaround that addresses the problem.  This is a page-specific fix, so it is not global to all libraries or links in your farm. I like it this way as you have granular control where you want to use it, meaning you may only want to use it on certain pages within team sites.

The solution is to add this code to a Content Editor Web Part (CEWP) on the page:

<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");

function OpenPDFInNewWindow()
{
// Get the collection of <a> tags
var aAllLinks = document.getElementsByTagName('a');

// For each <a> tag
for(var i=0;i<aAllLinks.length;i++)
{
var oA = aAllLinks[i];
var sHREF = oA.attributes["href"].value.toLowerCase();
// Is this a pdf link?
if(sHREF.indexOf(".pdf") > 0)
oA.href="javascript:window.open('" + sHREF + "').focus();";
}
}
</script>

Credit goes to Daniel whose code I modified to have this functionality.  To simplify this for all of you, I am also making this available using a .DWP web part.  You can simply upload this into the web part gallery of your site collection which allows you to more easily add it to any page. To download, click here.

Comments

getting error invalid url

hi
the above code is working fine, but when i click a pdf, it first give an error "invalid page url" and when i close that alert pdf opens in new window. can someone guide me wuts wrong?
 on 1/19/2012 6:36 AM

Re: Open PDF Files in a New Window

try this:

<script language="javascript" type="text/javascript">
    _spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");

    function OpenPDFInNewWindow()
    {     
      var aAllLinks = document.links;    
      for(var i=0;i<aAllLinks.length;i++)
      {
         var oA = aAllLinks[i];
         var sHREF = oA.href.toLowerCase();
     
         if(sHREF.indexOf(".jpg") > 0 || sHREF.indexOf(".pdf") > 0)
         {
            oA.onclick=function()
            {
            var w=window.open(this.href,'test','width=300');
            if(w) {
              w.focus();
              return false
            };
            return true;
          }
         }
      }
    }
    </script>
 on 8/8/2012 3:13 AM

Re: Open PDF Files in New Window

Thank you for this!  Spent all morning looking for the answer and this was it
 on 10/26/2012 2:51 PM

Awesome!

Thanks this works like a charm!
 on 10/29/2012 3:59 PM

Same issue!

Board member asked for this function within doc library and this solved the issue!
 on 10/29/2012 4:00 PM

Worked, but can I change window format?

This worked very well for me, as we were facing the same problem. However, it appears each PDF opens in a new window with a fixed width. I cannot seem to adjust the new window that has opened. Am I missing something? Thanks!
 on 12/12/2012 4:38 PM

Worked, but can I change window format?

This worked very well for me, as we were facing the same problem. However, it appears each PDF opens in a new window with a fixed width. I cannot seem to adjust the new window that has opened. Am I missing something? Thanks!
 on 12/14/2012 4:15 PM

Query - new tab instead of new window?

Thanks for this! I was wondering though, is it possible to not only force PDFs to open in a new browser tab (rather than a new window), but also apply the functionality across a site collection (to effect multiple document libraries on team sites, my sites etc)?
Any guidance much appreciated :)
 on 1/16/2013 5:03 AM

Extension to above comment

I spent quite some time searching for an answer to this one so I'm posting my findings here in case someone else searches for the same thing - managed to get it working using the jQuery solution suggested at http://stumblingthroughsharepoint.blogspot.co.uk/2012/08/pdf-files-and-sharepoint-2010_14.html.
 on 1/24/2013 9:39 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Attachments


United States
518 Riverside Ave
Westport, CT 06880
 
1050 Bishop St.
Suite 176
Honolulu, HI 96813
United Kingdom
Unit 13 Elder Way Waterside Drive Langley Berkshire SL3 6EP
United Kingdom
Singapore
Level 37
Ocean Financial Centre
10 Collyer Quay
Singapore 049315
Asia Pacific
Level 6
115 Pitt Street
Sydney NSW 2000
Australia 
+1800-930-4771
+44 (0)1753 541 000
+(65) 6232 2329
+61 2 9113 7243
United States
United Kingdom
Singapore
Australia
This web page conforms to W3C's "Web Content Accessibility Guidelines 1.0" Level "A" © 2014 Synergy Corporate Technologies
This site is best viewed in IE8 or above. Some features may not render properly if you are using an older browser.