Wednesday 14 June 2017

Display (Show) PDF file embedded in View in MVC Razor


Display (Show) PDF file embedded in View in ASP.Net MVC Razor
Description:
in this example we explain that how to Display PDF File embedded in Razor View in MVC or how to Show PDF file in View in Asp.Net MVC Razor.

Here we explain that how to show (Display) the PDF contents in View in MVC or pdf Viewer in MVC to Show the pdf contents in View.or Display PDF within web browser using MVC.How to open a pdf file in the view page of mvc.

So here we demonstrate how to display (view) PDF files within browser without downloading them in MVC Razor View.
Controller:

public class EmployeeController : Controller
{
    // GET: Employee
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult DisplayorViewPDF()
    {
        string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
        embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
        embed += "</object>";
        TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/Test.pdf"));

        return RedirectToAction("Index");
    }
}

View:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body {
            font-familyArial;
            font-size10pt;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("DisplayorViewPDF""Employee", FormMethod.Post))
    {
        <a href="javascript:;" onclick="document.forms[0].submit();">View PDF</a>
        <hr/>
        @Html.Raw(TempData["Embed"])
    }
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment