locked
Adding tables side by side using itext sharp RRS feed

  • Question

  • User-1323512201 posted

    How do we create 2 table's side by side using itext sharp like the below way.any help would ba appreciated

     <table>
        
        <tr>
        <td>
       test
        </td>
        <td>
     <table>
        <tr>
        <td>
       test abc
        
        </td>
        <td> </table>
         
        </table>
    Tuesday, September 20, 2011 10:44 PM

Answers

  • User-821857111 posted

    You should use the PdfPTable's WriteSelectedRows method:

    using System;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    public partial class TablesSideBySide : System.Web.UI.Page
    {
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		string path = Server.MapPath("PDFs");
    		Document doc = new Document();
    		try
    		{
    			PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + "/TablesSideBySide.pdf", FileMode.Create));
    			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, writer.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 + 200, doc.Top, writer.DirectContent);
    
    		}
    		catch (Exception ex)
    		{
    			//Log(ex.Message);
    		}
    		finally
    		{
    			doc.Close();
    		}
    	}
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2011 3:22 AM

All replies

  • User-126497635 posted

    http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables
    http://www.dotnetspark.com/kb/1365-create-custom-table-pdf-document-using-itextsharp.aspx
    http://www.developerfusion.com/thread/40095/5682-itextsharp-library-to-create-pdf-files-on-fly-in-cnet/

    Wednesday, September 21, 2011 1:31 AM
  • User-1323512201 posted

    I already tried all the things in google.Couldnt figure out a perfect sol..

    Wednesday, September 21, 2011 11:34 AM
  • User-1635004338 posted

    Hi,

    You cannot find any 100% same project from others online. You can just refer their ways to create yours by some similar idea or tricks. What we can do is give you these links about creating dynamic tables using iTextSharp. If still not the case you want, you can post the issue to the official support forum for this product.

    http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

    http://www.codeproject.com/KB/cs/iTextSharpPdfTables.aspx

    http://www.dotnetspark.com/kb/1365-create-custom-table-pdf-document-using-itextsharp.aspx

    Thanks,

    Friday, September 23, 2011 2:39 AM
  • User-821857111 posted

    You should use the PdfPTable's WriteSelectedRows method:

    using System;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    public partial class TablesSideBySide : System.Web.UI.Page
    {
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		string path = Server.MapPath("PDFs");
    		Document doc = new Document();
    		try
    		{
    			PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + "/TablesSideBySide.pdf", FileMode.Create));
    			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, writer.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 + 200, doc.Top, writer.DirectContent);
    
    		}
    		catch (Exception ex)
    		{
    			//Log(ex.Message);
    		}
    		finally
    		{
    			doc.Close();
    		}
    	}
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2011 3:22 AM
  • User-1323512201 posted

    That's Perfect.Thanks Mike.

    Friday, September 23, 2011 11:32 AM