Insert tables side by side to pdf using iTextSharp in ASP.Net

Last Reply 5 days ago By supredenki

Posted 6 days ago

Hello ASPDFormus,

Please I need your urgent help on how to create a table using iTextSharp in asp.net. I want the table to be on the same horizontal line, side-by-side; with spaces in-between. I mean one should be on the left and the other on the right on the same line.

Please direct your help to [email protected]

 

Your help will thankfully go a long way for my development.

Thank you in anticipation

 

Best Regards,

Ayodele

Posted 6 days ago

Hi supredenki,

First you need to download the iTextSharp file from below link and then after downloading take iTextSharp.dll file and add reference of it to your project.

Download iTextSharp

Once you are done with adding reference then refer below sample code which full fill your requirement.

C#

using System;
using System.Web;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (PdfWriter pdfWriter = PdfWriter.GetInstance(doc, memoryStream))
            {
                doc.Open();
                PdfPTable table = new PdfPTable(3);
                table.TotalWidth = 144f;
                table.LockedWidth = true;
                PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
                cell.Colspan = 3;
                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 1 Row 2");
                table.AddCell("Col 2 Row 2");
                table.AddCell("Col 3 Row 2");
                table.WriteSelectedRows(0, -1, doc.Left, doc.Top, pdfWriter.DirectContent);

                table = new PdfPTable(3);
                table.TotalWidth = 144f;
                table.LockedWidth = true;
                cell = new PdfPCell(new Phrase("This is table 2"));
                cell.Colspan = 3;
                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 1 Row 2");
                table.AddCell("Col 2 Row 2");
                table.AddCell("Col 3 Row 2");
                table.WriteSelectedRows(0, -1, doc.Left + 300, doc.Top, pdfWriter.DirectContent); //for adding more space you can change the doc.Left + 300 value to your requiremet.
                doc.Close();
                byte[] bytes = memoryStream.ToArray();
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=TwoTables.pdf");
                Response.Buffer = true;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(bytes);
                Response.End();
            }
        }
    }
}

VB.Net

Imports System.Web
Imports iTextSharp.text
Imports System.IO
Imports iTextSharp.text.pdf

Partial Public Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim doc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
        Using memoryStream As New MemoryStream()
            Using pdfWriter__1 As PdfWriter = PdfWriter.GetInstance(doc, memoryStream)
                doc.Open()
                Dim table As New PdfPTable(3)
                table.TotalWidth = 144.0F
                table.LockedWidth = True
                Dim cell As New PdfPCell(New Phrase("This is table 1"))
                cell.Colspan = 3
                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 1 Row 2")
                table.AddCell("Col 2 Row 2")
                table.AddCell("Col 3 Row 2")
                table.WriteSelectedRows(0, -1, doc.Left, doc.Top, pdfWriter__1.DirectContent)

                table = New PdfPTable(3)
                table.TotalWidth = 144.0F
                table.LockedWidth = True
                cell = New PdfPCell(New Phrase("This is table 2"))
                cell.Colspan = 3
                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 1 Row 2")
                table.AddCell("Col 2 Row 2")
                table.AddCell("Col 3 Row 2")
                table.WriteSelectedRows(0, -1, doc.Left + 300, doc.Top, pdfWriter__1.DirectContent)
                doc.Close()
                Dim bytes As Byte() = memoryStream.ToArray()
                Response.Clear()
                Response.ContentType = "application/pdf"
                Response.AddHeader("content-disposition", "attachment;filename=TwoTables.pdf")
                Response.Buffer = True
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.BinaryWrite(bytes)
                Response.[End]()
            End Using
        End Using
    End Sub
End Class

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108