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.
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.
Step #2
Select “ASP.NET Web Application” - Set the name for “Project” - Click on “OK”.
Step 3
Click on the new page in the project.
Right click on solution explorer - Goto “Add” - Click on “New Item”.
Step 4
Select “Web Form” from the list - Set the name for the file - Click on “Add”.
Step 5
Include the below code into the aspx file.
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button id="btnSubmit" runat="server" Text="Generate PDF" OnClick="btnSubmit_Click" /> </div>
- </form>
- </body>
-
- </html>
Step 6
Include this code into button click event,
- protected void btnSubmit_Click(object sender, EventArgs e) {
- try {
- Document doc = new Document(PageSize.A4, 36, 36, 36, 72);
- PdfPTable tableLayout = new PdfPTable(5);
- tableLayout.HeaderRows = 1;
- PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("SK.pdf"), FileMode.Create));
- doc.Open();
- doc.Add(GenerateContent(tableLayout));
- Process.Start(Server.MapPath("Sample-PDF-File.pdf"));
- doc.Close();
- } catch (Exception ex) {}
- }
And add this method inside the .cs page,
-
-
-
-
-
-
- private PdfPTable GenerateContent(PdfPTable tableLayout) {
- Font fontCell = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
- Font fontHeader = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
- PdfPTable tableWithRowspan = tableLayout;
- tableWithRowspan.SpacingBefore = 10;
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header", fontHeader)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 1", fontHeader)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 2", fontHeader)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 3", fontHeader)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("Header 4", fontHeader)));
- for (int iRow = 0; iRow < 7; iRow++) {
- PdfPCell cellWithRowspan = new PdfPCell(new Phrase("Environment Access Data place"));
-
- cellWithRowspan.Rowspan = 5;
- tableWithRowspan.AddCell(cellWithRowspan);
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("iPerform", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("iBehaviour", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("iTexts", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("iMap", fontCell)));
-
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));
-
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("158.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("1680.0", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));
-
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("158.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("1680.0", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("10.5", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("20.5", fontCell)));
-
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));
- tableWithRowspan.AddCell(new PdfPCell(new Phrase("POP UP value", fontCell)));
- }
- return tableWithRowspan;
- }
Repeat Head on each row
Use the below property to repeat your PDF header on each page.
- tableLayout.HeaderRows = 1;
Merge Column
If you want to merge the column, you can use the below one,
- cellWithRowspan.Colspan = 5;
Page Format
You can set the A1, A2, A4, Portrait and Landscape format on “Document” (Generic class)
Output
Hope this helps. Thanks.