Hi smile,
Create a main PdfPTable with 2 cells. Then create separate table for the left and right content.
Next add the two tables in the main table.
Refer below sample and correct your code accordingly.
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
string folderPath = @"C:\Users\anand\Desktop\Test\\";
FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create);
Document doc = new Document(PageSize.A4.Rotate(), 5f, 5f, 5f, 5f);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, stream);
doc.Open();
PdfContentByte content = pdfWriter.DirectContent;
PdfPTable mtable = new PdfPTable(2);
mtable.WidthPercentage = 100;
mtable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100;
PdfPCell cell = new PdfPCell(new Phrase("Assesssment 1 Term 2"));
cell.Colspan = 6;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Strength");
table.AddCell("Lowest %");
table.AddCell("Heighest %");
table.AddCell("Average");
table.AddCell("Grade");
table.AddCell("A1-T2");
table.AddCell("89.00");
table.AddCell("99.00");
table.AddCell("275");
table.AddCell("A+");
mtable.AddCell(table);
table = new PdfPTable(5);
table.WidthPercentage = 100;
cell = new PdfPCell(new Phrase("Assesssment 2 Term 2"));
cell.Colspan = 5;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Strength");
table.AddCell("Lowest %");
table.AddCell("Heighest %");
table.AddCell("Average");
table.AddCell("Grade");
table.AddCell("A2-T2");
table.AddCell("14.00");
table.AddCell("99.00");
table.AddCell("398");
table.AddCell("A+");
mtable.AddCell(table);
doc.Add(mtable);
doc.Close();
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim folderPath As String = "C:\Users\anand\Desktop\Test\\"
Dim stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim doc As Document = New Document(PageSize.A4.Rotate(), 5.0F, 5.0F, 5.0F, 5.0F)
Dim pdfWriter As pdf.PdfWriter = pdfWriter.GetInstance(doc, stream)
doc.Open()
Dim content As PdfContentByte = pdfWriter.DirectContent
Dim mtable As PdfPTable = New PdfPTable(2)
mtable.WidthPercentage = 100
mtable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER
Dim table As PdfPTable = New PdfPTable(5)
table.WidthPercentage = 100
Dim cell As PdfPCell = New PdfPCell(New Phrase("Assesssment 1 Term 2"))
cell.Colspan = 6
cell.HorizontalAlignment = 1
table.AddCell(cell)
table.AddCell("Strength")
table.AddCell("Lowest %")
table.AddCell("Heighest %")
table.AddCell("Average")
table.AddCell("Grade")
table.AddCell("A1-T2")
table.AddCell("89.00")
table.AddCell("99.00")
table.AddCell("275")
table.AddCell("A+")
mtable.AddCell(table)
table = New PdfPTable(5)
table.WidthPercentage = 100
cell = New PdfPCell(New Phrase("Assesssment 2 Term 2"))
cell.Colspan = 5
cell.HorizontalAlignment = 1
table.AddCell(cell)
table.AddCell("Strength")
table.AddCell("Lowest %")
table.AddCell("Heighest %")
table.AddCell("Average")
table.AddCell("Grade")
table.AddCell("A2-T2")
table.AddCell("14.00")
table.AddCell("99.00")
table.AddCell("398")
table.AddCell("A+")
mtable.AddCell(table)
doc.Add(mtable)
doc.Close()
End Sub
Screenshot