Skip to main content

Create PDF File In C#

Creating PDF is very simple in C#. We are going to use iTextSharp Library for creating a PDF file.

We have to follow some simple steps for generating the pdf file.

Step 1: We have to install iTextSharp through manage NuGet packages as below,

 packages

Or we can install using package manager console with the following line:

Install-Package iTextSharp

Step 2: Now add the following two namespaces in the top of .cs page,

using iTextSharp.text;  
using iTextSharp.text.pdf;

 

Step 3: Create a method for creating the PDF file and write logic.

protected void GeneratePDF(object sender, System.EventArgs e)  
{  
    using(System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())   
    {  
        Document document = new Document(PageSize.A4, 10, 10, 10, 10);  
  
        PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);  
        document.Open();  
  
        Chunk chunk = new Chunk("This is from chunk. ");  
        document.Add(chunk);  
  
        Phrase phrase = new Phrase("This is from Phrase.");  
        document.Add(phrase);  
  
        Paragraph para = new Paragraph("This is from paragraph.");  
        document.Add(para);  
  
        string text = @ "you are successfully created PDF file.";  
        Paragraph paragraph = new Paragraph();  
        paragraph.SpacingBefore = 10;  
        paragraph.SpacingAfter = 10;  
        paragraph.Alignment = Element.ALIGN_LEFT;  
        paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);  
        paragraph.Add(text);  
        document.Add(paragraph);  
  
        document.Close();  
        byte[] bytes = memoryStream.ToArray();  
        memoryStream.Close();  
        Response.Clear();  
        Response.ContentType = "application/pdf";  
  
        string pdfName = "User";  
        Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");  
        Response.ContentType = "application/pdf";  
        Response.Buffer = true;  
        Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);  
        Response.BinaryWrite(bytes);  
        Response.End();  
        Response.Close();  
    }  
}

 

In the above code, we are using Document parameterized constructor, with the following parameters.

  • pageSize: the pageSize,
  • marginLeft: the margin on the left,
  • marginRight: the margin on the right,
  • marginTop: the margin on the top,
  • marginBottom: the margin on the bottom.

 

Document document = newDocument(PageSize.A4, 10, 10, 10, 10);

 

We can add text to the above document using chunk, phrase, and paragraph.

  • Chunk: It is used for displaying the smallest significant piece of text.
  • Phrase: It is used for displaying an array of chunks or displaying the little more significant piece of text but it will go into the new line when the length of its contents exceed.
  • Paragraph: It is used for displaying the largest significant piece of text. It will go into the new line. The paragraph has properties like margin, alignment and many more as per our requirement. In Paragraph, we can add the properties as below,

 

string text = @"you are successfully created PDF file";  
Paragraph paragraph = newParagraph();  
paragraph.SpacingBefore = 10;  
paragraph.SpacingAfter = 10;  
paragraph.Alignment = Element.ALIGN_LEFT;  
paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);  
paragraph.Add(text);

 

In the above code, we are adding explicitly spacing before, spacing after, alignment and also the font of the paragraph.

And for adding the paragraph into the document, we are writing the code as below,

document.Add(paragraph);

 

We can give our own name to the pdf file as below,

string pdfName = "User";  
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");

 

In this easy way, we can generate PDF file using C#.

 

Leave a Reply