'How to create pdf document in asp.net using C#


This code snippet shows how to create a pdf document and write to that pdf document in ASP.NET using C#. we are going to use itextsharp dll in order to create the pdf file in c#. we can add our own custom images and paragraphs to the pdf document. we can also write text in the pdf document as shown in this code snippet.

Create a pdf document and write to that pdf document in ASP.NET using C#.
This code snippet shows how to create a pdf document and write to that pdf document in ASP.NET using C#. Here we are going to use the iTextSharp dll in order to create the pdf file and to write to the pdf file.

Please download the itextsharp dll and add it as reference to your project and import the following namespaces.


using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;

protected void Page_Load(object sender, EventArgs e)
{
//Creates a Document object specifying the document's dimensions and left, right, top, and bottom margins, respectively
var document = new Document(PageSize.A4, 50, 50, 25, 25);

//Create a new PDFWriter object, specifying the output stream i.e. Stream where the Document object's output should be serialized when it is closed
//FileStream, which will cause the PDF document's contents to be serialized to a file on disk named HelloPdf.pdf.

var output = new FileStream(Server.MapPath("HelloPdf.pdf"), FileMode.Create);
var writer = PdfWriter.GetInstance(document, output);

//open the pdf document for writing.
document.Open();

//Add elements to the document.
//add a new Paragraph object to the document with the text, "Hello, World!"
var helloPara = new Paragraph("Hello ,This is my First PDF Demo");
//Add this paragraph object to the document.
document.Add(helloPara);


//Close the document. This saves the document contents to the output stream.
document.Close();

}



Reference: http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp


Attachments

  • itextsharp dll (42956-1290-itextsharp.dll)
  • Article by Vaishali Jain
    Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

    Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: