Asp.net mvc 从ASHX的MemoryStream中显示PDF

Asp.net mvc 从ASHX的MemoryStream中显示PDF,asp.net-mvc,pdf,Asp.net Mvc,Pdf,我需要做的是 从sharepoint获取pdf 使用PDFSharp获取单个页面 将其返回到视图并显示该页面 到目前为止,我所拥有的是 context.Response.ClearHeaders(); context.Response.ContentType = "application/pdf"; context.Response.AddHeader("content-disposition", "inline; file

我需要做的是

  • 从sharepoint获取pdf
  • 使用PDFSharp获取单个页面
  • 将其返回到视图并显示该页面
  • 到目前为止,我所拥有的是

            context.Response.ClearHeaders();
            context.Response.ContentType = "application/pdf";            
            context.Response.AddHeader("content-disposition", "inline; filename=Something.pdf");
    
            // Get a fresh copy of the sample PDF file from Sharepoint later on
            string filename = @"book.pdf";
    
            // Open the file
            PdfDocument inputDocument = CompatiblePdfReader.Open(filename, PdfDocumentOpenMode.Import);
    
            PdfDocument outputDocument = new PdfDocument();
            outputDocument.Version = inputDocument.Version;
            outputDocument.Info.Title = "Pages 1 to 30";
            outputDocument.Info.Author = "Slappy";
    
            outputDocument.AddPage(inputDocument.Pages[1]);
    
            MemoryStream ms = new MemoryStream();
            outputDocument.Save(ms, false);
    
            ms.WriteTo(context.Response.OutputStream);
    
    我不知道如何在网页中显示它

    我有这个

    <script src="../../Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.media.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.metadata.js" type="text/javascript"></script>
    
    <script>
        $(function () {
            $.ajax({ url: '/GetBookPage.ashx',
                success: function (result) {
                    $("a.media").attr('href', result);
                    $('a.media').media({ width: 800, height: 600 });
                },
                async: false
            });
        });
    </script>
    
    <a class="media">PDF File</a>
    
    
    $(函数(){
    $.ajax({url:'/GetBookPage.ashx',
    成功:功能(结果){
    $(“a.media”).attr('href',result);
    $('a.media').media({宽:800,高:600});
    },
    异步:false
    });
    });
    PDF文件
    

    如果我将pdf保存到文件系统,然后使用以下处理程序将href指向该文件,则上述方法有效。

    public class GetBookPage : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                string filePath = @"c:\somepath\test.pdf";
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", "inline; filename=test.pdf");
                context.Response.WriteFile(filePath);
                context.Response.End(); 
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    
    如果您执行以下操作,我可以使PDF以内联方式显示:

    <script type="text/javascript">
       $(function () {
            $('a.media').media({ width: 800, height: 600 });
        });
    </script>
    
    <a class="media" href="/GetBookPage.ashx?.pdf">PDF File</a>
    
    
    $(函数(){
    $('a.media').media({宽:800,高:600});
    });
    

    插件使用url(或者更准确地说是扩展名)在页面上构建适当的媒体容器。如果您没有“.pdf”,它将不会像预期的那样工作。

    我认为您有点遗漏了这里的要点,或者我不清楚。我在SharePoint中的PDF有100多页,我只想返回每一页的第一页。因此使用PDFSharp并返回MemoryStream。抱歉,事实上,您用href=“/GetBookPage.ashx?.pdf”给出了答案,这是一个银弹,非常感谢!