简体   繁体   中英

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.

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.

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 :

在此输入图像描述

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:

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM