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