Merge Rows Using iTextSharp

Introduction

Let’s discuss iTextSharp and how to use it in C# programming.

iTextSharp is a PDF library that allows you to CREATE, INSPECT and MAINTAIN the document file. You can generate the PDF file based on your requirement.

Features

These are some features in iTextSharp,

  • PDF generations
  • PDF form filling
  • Design signature
  • Adding Page Number
  • Adding Water Marks
  • Adding Footer parts
  • Adding text on the right top side of the header

Now, we are going to discuss the step by step process to implement PDF file using iTextsharp.

Merge Rows in the table

This is the sample output that we are going to implement using the C# programming language.

iTextSharp

Follow the below steps to get the result.

Step 1

Open Start - Visual Studio 2015 - Click “File” menu - New - Project.

The below screenshot may help you.

iTextSharp

Step #2

Select “ASP.NET Web Application” - Set the name for “Project” - Click on “OK”.

iTextSharp

Step 3

Click on the new page in the project.

Right click on solution explorer - Goto “Add” - Click on “New Item”.

iTextSharp

Step 4

Select “Web Form” from the list - Set the name for the file - Click on “Add”.

iTextSharp

Step 5

Include the below code into the aspx file.

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title></title>  
  5. </head>  
  6.   
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <asp:Button id="btnSubmit" runat="server" Text="Generate PDF" OnClick="btnSubmit_Click" /> </div>  
  11.     </form>  
  12. </body>  
  13.   
  14. </html>  

Step 6

Include this code into button click event,

  1. protected void btnSubmit_Click(object sender, EventArgs e) {  
  2.     try {  
  3.         Document doc = new Document(PageSize.A4, 36, 36, 36, 72); //Create PDF Table  
  4.         PdfPTable tableLayout = new PdfPTable(5);  
  5.         tableLayout.HeaderRows = 1;  
  6.         PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("SK.pdf"), FileMode.Create)); //Create a PDF file in specific path  
  7.         doc.Open(); //Open the PDF document  
  8.         doc.Add(GenerateContent(tableLayout)); //Add Content to PDF  
  9.         Process.Start(Server.MapPath("Sample-PDF-File.pdf"));  
  10.         doc.Close(); // Closing the document  
  11.     } catch (Exception ex) {}  
  12. }  

And add this method inside the .cs page,

  1. /// <summary>  
  2. /// Add content to PDF  
  3. /// SANKARA KRISHANAN  
  4. /// </summary>  
  5. /// <param name="tableLayout"></param>  
  6. /// <returns></returns>  
  7. private PdfPTable GenerateContent(PdfPTable tableLayout) {  
  8.     Font fontCell = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);  
  9.     Font fontHeader = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);  
  10.     PdfPTable tableWithRowspan = tableLayout;  
  11.     tableWithRowspan.SpacingBefore = 10;  
  12.     tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header", fontHeader)));  
  13.     tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 1", fontHeader)));  
  14.     tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 2", fontHeader)));  
  15.     tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 3", fontHeader)));  
  16.     tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 4", fontHeader)));  
  17.     for (int iRow = 0; iRow < 7; iRow++) {  
  18.         PdfPCell cellWithRowspan = new PdfPCell(new Phrase("Environment Access Data place"));  
  19.         // The first cell spans 5 rows  
  20.         cellWithRowspan.Rowspan = 5;  
  21.         tableWithRowspan.AddCell(cellWithRowspan);  
  22.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("iPerform", fontCell)));  
  23.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("iBehaviour", fontCell)));  
  24.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("iTexts", fontCell)));  
  25.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("iMap", fontCell)));  
  26.         // Cell 2,1 does not exist  
  27.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));  
  28.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));  
  29.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));  
  30.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));  
  31.         // Cell 3,1 does not exist  
  32.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("158.5", fontCell)));  
  33.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("1680.0", fontCell)));  
  34.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));  
  35.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));  
  36.         // Cell 4,1 does not exist  
  37.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("158.5", fontCell)));  
  38.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("1680.0", fontCell)));  
  39.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));  
  40.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));  
  41.         // Cell 5,1 does not exist  
  42.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));  
  43.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));  
  44.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));  
  45.         tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));  
  46.     }  
  47.     return tableWithRowspan;  
  48. }  

Repeat Head on each row

Use the below property to repeat your PDF header on each page.

  1. tableLayout.HeaderRows = 1;  

Merge Column

If you want to merge the column, you can use the below one,

  1. cellWithRowspan.Colspan = 5;  

Page Format

You can set the A1, A2, A4, Portrait and Landscape format on “Document” (Generic class)

iTextSharp

Output

iTextSharp

Hope this helps. Thanks.


Similar Articles