Place (Align) two tables side by side using iTextSharp in C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

it is working fine. snapshoot is attached.

https://imgur.com/WuWu92E

i want to set the table on 0 margin from left right top bottom like this

https://imgur.com/TyGv5Gs

how to increase the width to set on 0 margin of the page.

i wrote the following code to make side by side two tables. 

            string pdfpath = @"C:\Reports\";
            if (!Directory.Exists(pdfpath))
            {
                Directory.CreateDirectory(pdfpath);
            }
            Document doc = new Document(PageSize.A4);
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(pdfpath + "SidebySideTable.pdf", FileMode.Create));
            doc.Open();
            
            PdfContentByte content = pdfWriter.DirectContent;
            
            PdfPTable table = new PdfPTable(5);
            table.HorizontalAlignment = 1;
            table.TotalWidth = 175f;
            table.LockedWidth = true;
            PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
            cell.Colspan = 5;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 2 Row 1");
            table.AddCell("Col 3 Row 1");
            table.AddCell("Col 4 Row 1");
            table.AddCell("Col 5 Row 1");
            table.AddCell("Col 1 Row 2");
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");
            table.AddCell("Col 4 Row 2");
            table.AddCell("Col 5 Row 2");
            //table.AddCell("Col 3 Row 2");
            table.WriteSelectedRows(0, -1, doc.Left, doc.Top, pdfWriter.DirectContent);
            //doc.Add(table);
            table = new PdfPTable(5);
            table.TotalWidth = 175f;
            table.LockedWidth = true;                    
            cell = new PdfPCell(new Phrase("This is table 2"));
            cell.Colspan = 5;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 2 Row 1");
            table.AddCell("Col 3 Row 1");
            table.AddCell("Col 4 Row 1");
            table.AddCell("Col 5 Row 1");
            table.AddCell("Col 1 Row 2");
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");
            table.AddCell("Col 4 Row 2");
            table.AddCell("Col 5 Row 2");
            table.WriteSelectedRows(10, -1, doc.Left + 600, doc.Top, pdfWriter.DirectContent);
            doc.Add(table);
            doc.Close();

            MessageBox.Show("Side by Side Generated Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;

 

Posted 5 months ago

Hi smile,

Refer below sample.

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