iTextSharp MVC Razor C#
Create PDF Using iTextSharp MVC Razor C#
RazorPDF for MVC - Generate PDFs with Razor Views and iTextSharp
RazorPDF is a simple package that allow you to use a Razor View to generate a PDF.RazorPDF uses the Razor View Engine to create iTextXML which in turn is used to produce the PDF files. RazorPDF is basically a port of the PDF feature of Spark View Engine.
Overview
RazorPDF is a package that adds a new ViewResult type called PdfResult. You can use a PdfResult to take your Razor view filled with iText XML or HTML and have it return a PDF file as the output.
RazorPDF is basically a port of the PDF feature of Spark View Engine.
Now very easy to create pdf using RazorPDF step by step.
Step 1
Add in your project RazorPDF
run the following command in the Package Manager Console
PM> Install-Package RazorPDF
Step 2
Create Pdf layout in Shared folder _PdfLayout.cshtml
<itext creationdate="@DateTime.Now.ToString()" producer="RazorPDF">
@RenderBody()
</itext>
Step 3
Create Pdf Page with OrdersInPdf.cshtml, Now design pdf with xml tag.
Like
<paragraph style="font-family: Helvetica; font-size: 18; font-style: italic;">
<chunk red="255" green="0" blue="0">@ViewBag.Title</chunk>
</paragraph>
<paragraph>
<chunk style="font-family:Times;font-weight:bold;">Report Date: </chunk>
<chunk style="font-family:Times;">@DateTime.Now.ToString("MM/dd/yyyy")</chunk>
</paragraph>
<paragraph>
<chunk style="font-family:Times;font-weight:bold;">Report Time: </chunk>
<chunk style="font-family:Times;">@DateTime.Now.ToString("hh:mm tt")</chunk>
</paragraph>
Step 4
Now ;Generate PDF file.
In Home Controller Create This Function.
using RazorPDF; public ActionResult OrdersInPdf() { var pdf = new PdfResult(null, "OrdersInPdf"); pdf.ViewBag.Title = "Report Title"; return pdf; }
Download Full Example
Links
- Official Nuget Package
- Project on Github
- Sample Project on Github
- Short Demo Video (partof Intro blog post)
- Syntax Samples blog post
iText Markup Samples
- Frequently used iText XML Gist
- Sample RazorPDF View
- Official iText Tutorial (This sample from the tutorial is great.)
iTextSharp MVC Razor C#
Reviewed by Bhaumik Patel
on
9:52 PM
Rating: