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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13721529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:46:31  来源:igfitidea点击:

Pdf Viewer in MVC to show the pdf contents in View

.netasp.net-mvcasp.net-mvc-3asp.net-mvc-4pdf-viewer

提问by user1400915

I have a View called ShowDocument.cshtml.

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

I want to show the pdf document in a view. First I am converting the html page to .pdf which contains the information as :

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

The code in the controller is:

控制器中的代码是:

Stream stream = HtmlToPdfBuilder.GetHtmlForm(model.Type, 16);

If I give return File(stream, "application/pdf", "Authorization.pdf"), I will be getting as save, save as dialog box.

如果我给 return File(stream, "application/pdf", "Authorization.pdf"),我将得到保存,另存为对话框。

I do not want this dialog box ,I want to show the pdf content inside the page only .

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

So Is there any pdf viewer in MVC so that I can show the content in a View only using some control

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

回答by retslig

This may not be exactly what you want but might meet your need. You can embed the PDF in a partial view then update the partial view via ajax with the PDF on the form submit button.

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

Example code: Partial view

示例代码:局部视图

    @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>    

Controller call:

控制器调用:

    public ActionResult GeneratePDF(ViewModel model)
    {

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

    public ActionResult RenderPDF(LabelViewModel model)
    {
        return PartialView(model);
    }

main view:

主要观点:

@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>
}

(Edit: Changed "main view" to a title ish)

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

回答by fero

How a PDF file is displayed depends on the user's browser. I don't think that there is a real "solution" for your problem.

PDF 文件的显示方式取决于用户的浏览器。我认为您的问题没有真正的“解决方案”。

When I had this problem I rendered the PDF file to multiple PNG images on the server using the GhostScript library and then referenced the PNG files in my view.

当我遇到这个问题时,我使用 GhostScript 库在服务器上将 PDF 文件渲染为多个 PNG 图像,然后在我的视图中引用这些 PNG 文件。