Generating PDF File Using C#

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 top of .cs page,

  1. using iTextSharp.text;  
  2. using iTextSharp.text.pdf;  
Step 3: Create a method for creating the PDF file and write logic.
  1. protected void GeneratePDF(object sender, System.EventArgs e)  
  2. {  
  3.     using(System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())   
  4.     {  
  5.         Document document = new Document(PageSize.A4, 10, 10, 10, 10);  
  6.   
  7.         PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);  
  8.         document.Open();  
  9.   
  10.         Chunk chunk = new Chunk("This is from chunk. ");  
  11.         document.Add(chunk);  
  12.   
  13.         Phrase phrase = new Phrase("This is from Phrase.");  
  14.         document.Add(phrase);  
  15.   
  16.         Paragraph para = new Paragraph("This is from paragraph.");  
  17.         document.Add(para);  
  18.   
  19.         string text = @ "you are successfully created PDF file.";  
  20.         Paragraph paragraph = new Paragraph();  
  21.         paragraph.SpacingBefore = 10;  
  22.         paragraph.SpacingAfter = 10;  
  23.         paragraph.Alignment = Element.ALIGN_LEFT;  
  24.         paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);  
  25.         paragraph.Add(text);  
  26.         document.Add(paragraph);  
  27.   
  28.         document.Close();  
  29.         byte[] bytes = memoryStream.ToArray();  
  30.         memoryStream.Close();  
  31.         Response.Clear();  
  32.         Response.ContentType = "application/pdf";  
  33.   
  34.         string pdfName = "User";  
  35.         Response.AddHeader("Content-Disposition""attachment; filename=" + pdfName + ".pdf");  
  36.         Response.ContentType = "application/pdf";  
  37.         Response.Buffer = true;  
  38.         Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);  
  39.         Response.BinaryWrite(bytes);  
  40.         Response.End();  
  41.         Response.Close();  
  42.     }  
  43. }  
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. 
  1. Document document = newDocument(PageSize.A4, 10, 10, 10, 10);  
We can add text to above document using chunk, phrase and paragraph. 
  • Chunk: It is used for displaying smallest significant piece of text.

  • Phrase: It is used for displaying an array of chunks or displaying little more significant piece of text but it will go into the newline, 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.

    Paragraph has properties like margin, alignment and many more as per our requirement. In Paragraph, we can add the properties as below,
    1. string text = @"you are successfully created PDF file";  
    2. Paragraph paragraph = newParagraph();  
    3. paragraph.SpacingBefore = 10;  
    4. paragraph.SpacingAfter = 10;  
    5. paragraph.Alignment = Element.ALIGN_LEFT;  
    6. paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);  
    7. paragraph.Add(text);  

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

    And for adding paragraph into the document, we are writing the code as below,
    1. document.Add(paragraph);  

    We can give our own name to pdf file as below,
    1. string pdfName = "User";  
    2. Response.AddHeader("Content-Disposition""attachment; filename=" + pdfName + ".pdf");  

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

Up Next
    Ebook Download
    View all
    Learn
    View all