Home > SharePoint > SharePoint 2010 Dynamic PDF viewer

SharePoint 2010 Dynamic PDF viewer

Normally, it’s not that hard to insert a pdf file onto a SharePoint page (pageviewer webpart) but if you had more than one pdf to view, it would me just as many pages to display them.  To counter this, I used a query string and a little javascript to dynamically embed the pdf in the page.

First you have to set up your url.  It basically consists of the url of the page plus a query string with the pdf’s name.  Example:

https://www.domain.com/sitecollection/site/pages/viewer.aspx?pdfurl=my pdf file name

Note, you don’t need to have the full url location of the document here.  You’ll add that later into the javascript.  This keeps your urls down to a decent length.  The next code snippet will go in the source of a Content Editor Webpart.

<div id="pdfHolder">No review document selected</div>
<script language="javascript">
	
		var query = window.location.search;
		
		if(query.substring(0,1) =="?")
		{
			//remove the question mark
			query = query.substring(1);
			//split the string into an array based on ampersand locations
			var urlArray = query.split("&");
			//find the string that matches pdfurl=
			for(var i=0; i<urlArray.length; i++)
			{
			
				if(urlArray[i].search("pdfurl=")== 0)
				{
					//grab the url after the pdfURL=
					query = urlArray[i].substring(7);
					//embed the pdf on the page
					document.getElementById('pdfHolder').innerHTML='<embed src="/site/pdfLibrary/'+query+'.pdf" type="application/pdf" width="500" height="650" >';
				}
			}
		}
			
	</script>

The code first declares a div tag called “pdfHolder”.  There’s some default text in place if there isn’t a pdf to load, but it will be replaced once we find a file.  The javascript below that looks at the url to see if it has a query string attached.  If it does, the code then splits up all the query strings attached to the url and puts them in an array.  We cycle through the array until we find string beginning with “pdfurl=” and then we put that string into a variable.

We then have Javascript write an embed tag into the pdfHolder tag. In the embed code, you’ll put the address of your pdf library. The name of the pdf file, which we got from the query string, gets placed here followed by “.pdf”. You can adjust the width and height as need be.

  1. Anthony
    October 5, 2011 at 8:25 pm

    Hey goodnite, I need your help please….how do I get this working??? How do I pass the id of the file??

  2. Frank
    December 2, 2011 at 5:45 am

    We started down this path, then we tried to build our own. My colleague found this tool called Vizit that works in SharePoint. We got our own code to work but it had some issues. So we went with Vizit. We just upgraded to their new version. Very cool stuff. First page previews, it works with search, and it is really fast. We are still testing it in dev, but so far so good. Skip their site and go to the demo they have online. http://demo.vizit.com

  3. Gman
    December 21, 2011 at 5:10 pm

    How could this be modified to accept URLs from a List View web part?

    TIA

    • December 23, 2011 at 11:07 am

      If you want it to refresh the pdf without refreshing the page, this script wont’ work. Javascript won’t update without refreshing the whole page.

      You can, however, set up your links in your list to append the “?pdfurl=my pdf file name” to the end of the page’s url. The page will refresh, but it would load the new pdf file.

  4. Floyd Hilman
    March 8, 2012 at 11:36 am

    Hi, I am fairly new to develping solutions for sharepoint. Do you consult? I need to have PDF files viewed in sharepoint with their properties. Can you help?

  5. eyelogic
    April 18, 2012 at 9:29 am

    Hi,
    I’ve tried your solution, but I’m unable to get it working.
    Do you have screendumps or a step by step guide, because i’m unable to find the problem.

    The Content part keeps displaying:”No review document selected”

    • April 18, 2012 at 10:40 am

      Double check your URL so that it’s formatted correctly. My guess is one of the “if” statements isn’t getting fired. Your URL needs to be …/pages/viewer.aspx?pdfurl= and then the PDF file name.

  6. August 2, 2012 at 2:40 pm

    Thanks Arcemise – works like a charm

  7. August 22, 2012 at 8:59 am

    This site was… how do you say it? Relevant!! Finally I’ve found something that helped me. Thanks!

  8. May 15, 2013 at 10:41 pm

    Hi, everything is going sound here and ofcourse every one is sharing data, that’s really excellent, keep up writing.

  9. Jewell
    November 25, 2013 at 2:05 pm

    Can you please assist? My content editor isn’t displaying a pdf I am only seeing “No review document selected”. I have placed the content editor web part on my editform.aspx page, updated the javascript as follows:

    if(urlArray[i].search(“pdfurl=/site/Document Library/Document.pdf”)== 0)

    and

    document.getElementById(‘pdfHolder’).innerHTML='<embed src="site/Document Library/'+query+'.pdf" type="application/pdf"

    I am just learning how to code so I need step by step instructions on what I need to do.

    Thanks for all of your help.

    • December 1, 2013 at 12:50 am

      I’d first recommend checking the PDF filename and url match an existing file.

      Second, I might not have been clear on the use of “pdfurl=”. You should be able to simply use “pdfurl=” as described in the code for the if statement on line 16. That bit is only checking to see if the url has a pdf url to consider. Your url can also just be “…?pdfurl=MyPDFdoc”

      The only part you need to change is on line 21 where it asks for the source (“src”) in the embed tag. That’s where you should put the full path to your PDF doc library.

      Hope that helps

  10. John
    April 22, 2014 at 10:08 am

    It would be nice if you could explain it better. According to your last post, we don’t change “pdfurl=”, we only change it line 21. I did but I still got nothing except. Thanks anyway, I hope it worked for someone, but it did not work for me, Thanks

  1. No trackbacks yet.

Leave a comment