MVC中的Pdf Viewer在View中显示pdf内容

use*_*915 4 .net asp.net-mvc pdf-viewer asp.net-mvc-3 asp.net-mvc-4

我有一个名为ShowDocument.cshtml的视图.

我想在视图中显示pdf文档.首先,我将html页面转换为.pdf,其中包含以下信息:

控制器中的代码是:

Stream stream = HtmlToPdfBuilder.GetHtmlForm(model.Type, 16);
Run Code Online (Sandbox Code Playgroud)

如果我返回File(stream, "application/pdf", "Authorization.pdf"),我将保存,保存为对话框.

我不想要这个对话框,我只想在页面内显示pdf内容.

那么MVC中是否有任何pdf查看器,以便我只能使用某些控件在View中显示内容

ret*_*lig 11

这可能不是您想要的,但可能满足您的需求.您可以在部分视图中嵌入PDF,然后使用表单提交按钮上的PDF通过ajax更新部分视图.

示例代码:部分视图

    @model Test.Models.ViewModel

<style type="text/css">

#pdfbox
{
    width:600px;
    height:400px;
    border: 5px solid #ccc;
}

</style>

<object id='pdfbox' type="application/pdf" data="@Url.Action("GeneratePDF", "Home", Model)">
    Click @Html.ActionLink("here", "GeneratePDF", "Home") to view the file.
</object>    
Run Code Online (Sandbox Code Playgroud)

控制器呼叫:

    public ActionResult GeneratePDF(ViewModel model)
    {

        byte[] bytes = OpenPDFAndGetBytes("Thepdfname");
        return File(bytes, "application/pdf");
    }

    public ActionResult RenderPDF(LabelViewModel model)
    {
        return PartialView(model);
    }
Run Code Online (Sandbox Code Playgroud)

主要观点:

@using (Ajax.BeginForm("RenderPDF", "Home", new AjaxOptions { UpdateTargetId = "pdf" }))
{
    <table>
        <tr>
            <td>
                <fieldset>
                    <legend>Fill the form:</legend>
                        Some form junk can go here
                    <br />
                    <input type="submit" value="Display PDF" />
                </fieldset>
            </td>
            <td>
                <div id='pdf'>
                    @{
                        Html.RenderPartial("RenderPDF", Model);
                    }
                </div>
            </td>
        </tr>
    </table>
}
Run Code Online (Sandbox Code Playgroud)

(编辑:将"主视图"更改为标题ish)