【问题标题】:Display a PDF from a MemoryStream from ASHX从 ASHX 的 MemoryStream 显示 PDF
【发布时间】:2012-08-09 01:55:55
【问题描述】:

我需要做的是;

  1. 从 sharepoint 获取 pdf
  2. 使用 PDFSharp 获取单页
  3. 将其返回到视图并显示该页面

到目前为止,我所拥有的是;

        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>

如果我将 pdf 保存到文件系统,然后将 href 指向该文件,则上述方法有效。

【问题讨论】:

    标签: asp.net-mvc pdf


    【解决方案1】:

    使用以下处理程序:

    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>
    

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

    【讨论】:

    • 我认为您在这里有点忽略了这一点,或者我可能不清楚。我在 SharePoint 中拥有的 PDF 有 100 多页,我只想返回每个页面的第一页。因此使用 PDFSharp 并返回 MemoryStream。
    • 对不起,实际上你已经用 href="/GetBookPage.ashx?.pdf" 给出了答案,非常感谢你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多