Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add PDF viewer panel to Sharepoint WebPart

Never worked with Sharepoint, but need to add a WebPart to display a PDF document. How does this work?

There is currently some code to add a ReportViewer (SSRS) WebPart, but I need to replace it to display a PDF file (from disk).

The PDF file is from a local/network path that is not served publicly. I need to get the PDF content streamed in a web part, setting the content-type along the way.

If it helps any, my thoughts are to respond on a different URL (page in the same site), that simply takes some token (get params?) and streams a byte[] and sets the content-type as application/pdf - I could well be completely off the mark. You will need to be quite specific with steps and code/sample/links in the answer.

like image 210
RichardTheKiwi Avatar asked Jan 26 '26 20:01

RichardTheKiwi


2 Answers

Add a Page Viewer Web Part to the page and set the URL to the location of the pdf. A side effect of this is the Acrobat tool bar is displayed in the web part. If you want that to go away you have to add #toolbar=0&navpanes=0 to the url. For example:

http://server:port/path/filename.pdf#toolbar=0&navpanes=0

NOTE: The user would have to have Adobe reader plugin installed to view the pdf in the web part.

like image 174
Matt Avatar answered Jan 29 '26 10:01

Matt


You should combine Matt's solution and a HttpHandler : The HttpHandler will get the PDF file from your local/network path and get it to your client. The Page Viewer Web Part (or an iframe inserted with a content webpart) will integrate the PDF on your page :

enter image description here

Here is a simple code for the httphandler :

public class TestPdfHandler : IHttpHandler
{
    #region IHttpHandler Membres

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        string fileName = context.Request["pdfname"];

        byte[] buffer = null;
        buffer = File.ReadAllBytes("d:\\" + fileName +".pdf");
        context.Response.Clear();
        context.Response.ContentType = "application/pdf";

        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
        context.Response.End();
    }

    #endregion
}

In the web.config, you add the httphandler :

 <add verb="*" path="pdf.axd" type="test.TestPdfHandler, pdfhandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=98df27cf3a770eaa"/>

In the WebPart, you set the src property:

enter image description here

The difficulty would be if you want to load different PDF depending on a parameter passed on the URL.

If so you would need to create your own webpart that simply take the QueryString parameter on the current request and write an iframe elt with the src attribute containing the parameter.

like image 33
Sylvain Reverdy Avatar answered Jan 29 '26 10:01

Sylvain Reverdy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!