我需要做的是;
到目前为止我所拥有的是什么;
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指向该文件,则上述方法有效。
答案 0 :(得分:2)
使用以下处理程序:
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”,它将无法按预期工作。