How to generate pdf using itextsharp in asp.net mvc
Creating PDF Documents with iTextSharp in ASP.NET MVC
Are you looking to enhance your ASP.NET MVC application by generating dynamic PDF documents? Look no further than iTextSharp, a robust library that empowers developers to seamlessly integrate PDF creation capabilities into their web applications.
In this article, we'll walk you through the process of using iTextSharp to create PDF documents within an ASP.NET MVC project. We'll dissect the code you've provided and explain each step to ensure you have a comprehensive understanding of the implementation.
Setting Up the Project
The first step involves adding iTextSharp as a dependency to your ASP.NET MVC project. By referencing the appropriate NuGet package, you gain access to a plethora of tools for working with PDFs programmatically.
Install-Package iTextSharp
Once you have iTextSharp integrated, you're ready to dive into the world of PDF generation.
Generating a Basic PDF
The heart of your code lies in the Index
action of your controller. Here, you create a new instance of Document
and establish a PdfWriter
to manage the output. This serves as the foundation for constructing your PDF document.
// Creating a new document
Document doc = new Document();
FileStream fs = new FileStream("F:\\example.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
// Adding content to the document
doc.Add(new Paragraph("Hello, Code2night!"));
// ...
doc.Close();
fs.Close();
By following this structure, you ensure that your PDF generation process is structured and efficient.
Adding Links and Images
To make your PDF interactive and engaging, you can add links and images. The Anchor
class facilitates the creation of hyperlinks within the PDF. As demonstrated in your code, you can create an anchor that directs readers to a specific URL.
// Adding an anchor (link)
Anchor anchor = new Anchor("Visit Code2night", new Font(Font.FontFamily.HELVETICA, 12, Font.UNDERLINE));
anchor.Reference = "https://www.code2night.com";
doc.Add(anchor);
In addition to links, incorporating images can visually enhance your PDF. By utilizing the Image
class, you can seamlessly insert images into the document.
// Adding an image
Image image = Image.GetInstance(Server.MapPath("/Content/Image.jpg"));
image.ScaleToFit(200, 200);
doc.Add(image);
Creating Lists
Information organization is essential in any document. With iTextSharp, you can effortlessly create bulleted lists that present content in a structured manner.
// Adding a bulleted list
List list = new List(List.UNORDERED);
list.Add(new ListItem("Item 1"));
list.Add(new ListItem("Item 2"));
doc.Add(list);
Document Generation and File Management
One crucial aspect is proper document generation and file management. Your code illustrates the process of deleting any existing PDF file before generating a new one. This ensures that there are no conflicts or overwrites during the generation process.
You can copy the complete code from here and user that in your action
using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using System.Xml.Linq; namespace ITextSharpWeb.Controllers { public class HomeController : Controller { public ActionResult Index() { Document doc = new Document(); if (System.IO.File.Exists("F:\\example.pdf")) { System.IO.File.Delete("F:\\example.pdf"); } // Set the output file stream FileStream fs = new FileStream("F:\\example.pdf", FileMode.Create); PdfWriter writer = PdfWriter.GetInstance(doc, fs); // Open the document for writing doc.Open(); // Add content to the document doc.Add(new Paragraph("Hello, Code2night!")); Paragraph paragraph = new Paragraph("This is a paragraph."); doc.Add(paragraph); // Adding a header Paragraph header = new Paragraph("Header Text", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD)); doc.Add(header); // Adding an anchor (link) Anchor anchor = new Anchor("Visit Code2night", new Font(Font.FontFamily.HELVETICA, 12, Font.UNDERLINE)); anchor.Reference = "https://www.code2night.com"; doc.Add(anchor); Image image = Image.GetInstance(Server.MapPath("/Content/Image.jpg")); image.ScaleToFit(200, 200); doc.Add(image); // Adding a bulleted list List list = new List(List.UNORDERED); list.Add(new ListItem("Item 1")); list.Add(new ListItem("Item 2")); doc.Add(list); // Close the document doc.Close(); // Close the file stream fs.Close(); return View(); } } }
So, now you can run the application and you will see the pdf generated. you can check the file generated in the following format.
So this is How to generate pdf using itextsharp in asp.net mvc. Let us know the feedback.