Saturday, July 20, 2019

Create Simple PDF File Using iTextSharp Library

iTextSharp


iTextSharp is a library that allows you to create and manipulate PDF documents. 

It enables developers looking to enhance web applications and other applications with dynamic PDF document generation and/or manipulation.

Before proceeding you should have the iTextSharp library file.

You can download the iTextSharp.dll from here.
  1. Open VisualStudio and then create a new ASP.NET web application.
  2. Add a reference to the iTextSharp.dll file to your project.
  3. Add the following namespaces before proceeding:
    1. using System;  
    2. using iTextSharp.text.pdf;  
    3. using System.IO;  
    4. using iTextSharp.text;  
    5. using System.Diagnostics;  
  4. In the designer, drag and drop two buttons as follows,

    Create Simple PDF File Using iTextSharp Library
  5. For the Generate PDF File button, write the following code to generate the PDF file:
    1. protected void btnGeneratePDFFile_Click(object sender, EventArgs e) {  
    2.     //Create document  
    3.     Document doc = new Document();  
    4.     //Create PDF Table  
    5.     PdfPTable tableLayout = new PdfPTable(4);  
    6.     //Create a PDF file in specific path  
    7.     PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("Sample-PDF-File.pdf"), FileMode.Create));  
    8.     //Open the PDF document  
    9.     doc.Open();  
    10.     //Add Content to PDF  
    11.     doc.Add(Add_Content_To_PDF(tableLayout));  
    12.     // Closing the document  
    13.     doc.Close();  
    14.     btnOpenPDFFile.Enabled = true;  
    15.     btnGeneratePDFFile.Enabled = false;  
    16. }  
    17. private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout) {  
    18.     float[] headers = {  
    19.         20,  
    20.         20,  
    21.         30,  
    22.         30  
    23.     }; //Header Widths  
    24.     tableLayout.SetWidths(headers); //Set the pdf headers  
    25.     tableLayout.WidthPercentage = 80; //Set the PDF File witdh percentage  
    26.     //Add Title to the PDF file at the top  
    27.     tableLayout.AddCell(new PdfPCell(new Phrase("Creating PDF file using iTextsharp"new Font(Font.HELVETICA, 13, 1, new iTextSharp.text.Color(153, 51, 0)))) {  
    28.         Colspan = 4, Border = 0, PaddingBottom = 20, HorizontalAlignment = Element.ALIGN_CENTER  
    29.     });  
    30.     //Add header  
    31.     AddCellToHeader(tableLayout, "Cricketer Name");  
    32.     AddCellToHeader(tableLayout, "Height");  
    33.     AddCellToHeader(tableLayout, "Born On");  
    34.     AddCellToHeader(tableLayout, "Parents");  
    35.     //Add body  
    36.     AddCellToBody(tableLayout, "Sachin Tendulkar");  
    37.     AddCellToBody(tableLayout, "1.65 m");  
    38.     AddCellToBody(tableLayout, "April 24, 1973");  
    39.     AddCellToBody(tableLayout, "Ramesh Tendulkar, Rajni Tendulkar");  
    40.     AddCellToBody(tableLayout, "Mahendra Singh Dhoni");  
    41.     AddCellToBody(tableLayout, "1.75 m");  
    42.     AddCellToBody(tableLayout, "July 7, 1981");  
    43.     AddCellToBody(tableLayout, "Devki Devi, Pan Singh");  
    44.     AddCellToBody(tableLayout, "Virender Sehwag");  
    45.     AddCellToBody(tableLayout, "1.70 m");  
    46.     AddCellToBody(tableLayout, "October 20, 1978");  
    47.     AddCellToBody(tableLayout, "Aryavir Sehwag, Vedant Sehwag");  
    48.     AddCellToBody(tableLayout, "Virat Kohli");  
    49.     AddCellToBody(tableLayout, "1.75 m");  
    50.     AddCellToBody(tableLayout, "November 5, 1988");  
    51.     AddCellToBody(tableLayout, "Saroj Kohli, Prem Kohli");  
    52.     return tableLayout;  
    53. }  
    54. // Method to add single cell to the header  
    55. private static void AddCellToHeader(PdfPTable tableLayout, string cellText) {  
    56.     tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.WHITE))) {  
    57.         HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new iTextSharp.text.Color(0, 51, 102)  
    58.     });  
    59. }  
    60. // Method to add single cell to the body  
    61. private static void AddCellToBody(PdfPTable tableLayout, string cellText) {  
    62.     tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.BLACK))) {  
    63.         HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = iTextSharp.text.Color.WHITE  
    64.     });  
    65. }  
    Don't be confused by the code above, I am extracting the methods for reusability purposes.

    I will explain whats happening in the preceding methods.

    btnGeneratePDFFile_Click
    In this method I am creating the PDF file in a specific location. Then I am calling the Add_Content_To_PDF method.

    Add_Content_To_PDF
    In this method I am giving the sizes of the cells as well as PDF file width and then I am adding content to the PDF file usigng the two methods AddCellToHeader and AddCellToBody.

    AddCellToHeader
    In this mehtod I am adding a single cell to the PDF header with some style.

    AddCellToBody
    In this mehtod I am adding a single cell to the PDF body with some style. 
  6. Now in the Open PDF button click write the following code to open the PDF that has been created:
    1. protected void btnOpenPDFFile_Click(object sender, EventArgs e)  
    2. {  
    3.     //Open the PDF file  
    4.     Process.Start(Server.MapPath("Sample-PDF-File.pdf"));  
    5.     btnGeneratePDFFile.Enabled = true;  
    6.     btnOpenPDFFile.Enabled = false;  
    7. }  
  7. Save the files and run the project. Click on Generate PDF; your PDF file will be generated. Then click on the Open PDF file, you will get the created PDF.
  8. If your code is not working then I have added a project to this article you can download and test. 
So in this way we can create a PDF simply by using the iTextSharp library.

No comments:

Post a Comment

Lab 09: Publish and subscribe to Event Grid events

  Microsoft Azure user interface Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after t...