usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingSystem.IO;usingHiQPdf;namespaceHiQPdfMvcRazorApplication.Controllers
{public classHomeController : Controller
{publicActionResult Index()
{
ViewBag.Message= "Welcome to ASP.NET MVC!";
Session["MySessionVariable"] = "My Session Variable Value assigned in Index";returnView();
}publicActionResult About()
{returnView();
}public string RenderViewAsString(string viewName, objectmodel)
{//create a string writer to receive the HTML code
StringWriter stringWriter = newStringWriter();//get the view to render
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null);//create a context to render a view based on a model
ViewContext viewContext = newViewContext(
ControllerContext,
viewResult.View,newViewDataDictionary(model),newTempDataDictionary(),
stringWriter
);//render the view to a HTML code
viewResult.View.Render(viewContext, stringWriter);//return the HTML code
returnstringWriter.ToString();
}
[HttpPost]publicActionResult ConvertThisPageToPdf()
{//get the HTML code of this view
string htmlToConvert = RenderViewAsString("Index", null);//the base URL to resolve relative images and css
String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl= thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertThisPageToPdf".Length);//instantiate the HiQPdf HTML to PDF converter
HtmlToPdf htmlToPdfConverter = newHtmlToPdf();//hide the button in the created PDF
htmlToPdfConverter.HiddenHtmlElements = new string[] { "#convertThisPageButtonDiv"};//render the HTML code as PDF in memory
byte[] pdfBuffer =htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);//send the PDF file to browser
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName= "ThisMvcViewToPdf.pdf";returnfileResult;
}
[HttpPost]publicActionResult ConvertAboutPageToPdf()
{//get the About view HTML code
string htmlToConvert = RenderViewAsString("About", null);//the base URL to resolve relative images and css
String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl= thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertAboutPageToPdf".Length);//instantiate the HiQPdf HTML to PDF converter
HtmlToPdf htmlToPdfConverter = newHtmlToPdf();//render the HTML code as PDF in memory
byte[] pdfBuffer =htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);//send the PDF file to browser
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName= "AboutMvcViewToPdf.pdf";returnfileResult;
}
}
}2. Source Code Example -Index View Razor Code
@{
ViewBag.Title= "Home Page";
}
@ViewBag.Message
To learn more about ASP.NET MVC visit ASP.NET MVC Website
@using (Html.BeginForm("ConvertThisPageToPdf", "Home", FormMethod.Post, new { id = "convertForm"}))
{
/>
@using (Html.BeginForm("ConvertAboutPageToPdf", "Home", FormMethod.Post, new { id = "convertForm"}))
{
/>
@Session["MySessionVariable"].ToString()
Source Code Example-Index View MVC Code
.Master"Inherits="System.Web.Mvc.ViewPage"%>
Home Page
To learn more about ASP.NET MVC visit ASP.NET MVC Website
/>
/>