Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

How to generate pdf using itextsharp in asp.net mvc

Date- Aug 06,2023

8771

Free Download Pay & Download
Itextsharp Aspnet 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.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to export table data in pdf using itextsharp in asp.net mvc
Sep 16, 2023
How to Convert Text to Speech in Asp.Net
Nov 06, 2023
Status Code 413 Request Entity Too Large
Jul 02, 2023
Using Ajax Beginform in Asp.Net MVC
Jun 18, 2023

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers