locked
pdf table creation problem RRS feed

  • Question

  • User-14001990 posted

    Dear All,

    I am trying to create multiple tables in a pdf using itextsharp but the second table doesn't show up.

    Then I created both the tables in two separate paragraphs but it still doesn't show up.The code is below:

     

    Document

    document = new Document(PageSize

    .A4, 20, 20, 30, 65);

    //create a table

    //PdfPTable table = new PdfPTable(2);

    try

    {

    PdfWriter.GetInstance(document, new FileStream("c:\\my.pdf", FileMode

    .Create));

    document.Open();

    //WebClient wc = new WebClient();

    //string htmlText = wc.DownloadString("C:\\Documents and Settings\\ic013572\\My Documents\\test.htm");

    //Response.Write(htmlText);

    //List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);

    //for (int k = 0; k < htmlarraylist.Count; k++)

    //{

    // document.Add((IElement)htmlarraylist[k]);

    //}

    Paragraph prg = new Paragraph(new Phrase("This is a paragraph1"

    ));

    //prg.SpacingAfter = 15f;

    PdfPTable tbl = new PdfPTable

    (3);

    tbl.CalculateHeights(

    true

    );

    tbl.TotalWidth = 200;

    //tbl.LockedWidth = true;

    PdfPCell cell = new PdfPCell(new Phrase("Idea 320:sdfsd fddsfsdf"

    ));

    cell.Colspan = 2;

    cell.HorizontalAlignment = 0;

    tbl.AddCell(cell);

    //iTextSharp.text.Image img = new );

    string imagepath = Server.MapPath("Images/" + "siemens_logo.jpg"

    );

    iTextSharp.text.

    Image jpg = iTextSharp.text.Image

    .GetInstance(imagepath);

    jpg.Alignment =

    Element

    .ALIGN_CENTER;

    //document.Add(Paragraph);

    //document.Add(jpg);

    //tbl.AddCell(iTextSharp.text.Image("C:\\Documents and Settings\\ic013572\\My Documents\\MyPictures\\siemens_logo.jpg"));

    PdfPCell cell1 = new PdfPCell(jpg,true

    );

    cell1.Colspan = 1;

    cell1.HorizontalAlignment = 2;

    tbl.AddCell(cell1);

    prg.Add(tbl);

    //document.Close();

    //document.Open();

    Paragraph prg1 = new Paragraph(new Phrase("This is a paragraph2"

    ));

    prg1.SpacingAfter = 15f;

    PdfPTable tbl1 = new PdfPTable

    (2);

    //tbl1.CalculateHeights(true);

    tbl1.TotalWidth = 200;

    string imgpath = Server.MapPath("Images/" + "meter.jpg"

    );

    iTextSharp.text.

    Image jpg1 = iTextSharp.text.Image

    .GetInstance(imgpath);

    jpg1.Alignment =

    Element

    .ALIGN_CENTER;

    PdfPCell cell2 = new PdfPCell(jpg1, true

    );

    cell2.Colspan = 1;

    cell2.HorizontalAlignment = 0;

    tbl1.AddCell(cell2);

    prg1.Add(tbl1);

    document.Add(prg);

    document.Add(prg1);

    document.Close();

    }

    catch(Exception

    ex)

    {

    }

    So is it possible to create multiple tables separate from each other.

    If not then what is the way out.Nested tables??

    Thanks for your efforts.

     

     

    Wednesday, February 2, 2011 1:53 AM

All replies

  • User1520641890 posted

    hi,

    first, you'll probably have a better chance for people to respond if you format your code better....

    the problem results from not adding the correct number of cells. in the first table you declare three (3) columns but add only to cells. for the second table you declare two (2) columns but only add one cell. iTextSharp does not "fill-in the blanks" for you. (that's probably why you used the try .. catch block??)

    anyway, try the following example to see for yourself:

    <%@ Page Language='C#' %>
    <%@ Import Namespace='iTextSharp.text' %>
    <%@ Import Namespace='iTextSharp.text.pdf' %>
    <script runat='server'>
      protected override void OnInit(EventArgs e) {
        base.OnInit(e);
        int columns = 3;
        Response.ContentType = "application/pdf";
        Document doc = new Document();
        PdfWriter.GetInstance(doc, Response.OutputStream);
    /* 
     * try **HERE FIRST** - this works
     */
        PdfPTable table = new PdfPTable(columns);
    /*
     * try this SECOND. uncomment line below, then comment out
     * the line above
        //PdfPTable table = new PdfPTable(10);
     * 
     */
        doc.Open();
        for (int k = 0; k < columns; ++k) {
          table.AddCell(k.ToString());
        }
        doc.Add(table); 
        doc.Close();
        Response.End();
      }
    </script>
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml'>
    <head id="Head1" runat='server'><title></title></head>
    <body><form id='form1' runat='server'>
    </form></body></html>

    notice if you run the code you see a table with one row and three columns. then after you switch the commented/uncommented lines an Exception is thrown, since the table is delared with 10 columns, but only three cells are added to the table.

    so just remember to add the correct number of cells to your table and you'll be fine :)

    Wednesday, February 2, 2011 6:21 AM
  • User-547121016 posted

    you can try with pdf templates.

    here a article.

    http://www.c-sharpcorner.com/UploadFile/515294/5201/


    Wednesday, February 2, 2011 6:33 AM
  • User-14001990 posted

    Hi,

    first, you'll probably have a better chance for people to respond if you format your code better....

     

     

    How do I format the code as u have done?

    A table is shown when all the cells are initialized.I was hoping that it would show part of it.

    However there are some problems.

    I don't find a complete tutorial for it.All I find is that they are selling a book.

    IS it possible to resize an image before adding it to a cell.

    Text are added by way of chunks. How do I get the capacity of a cell so that I can shorten a chunk larger than it?

    Is it possible to draw charts or it has to be imported as an image?

    Thanks.

     

     

    Thursday, February 3, 2011 8:36 AM
  • User-14001990 posted

    Hi Lasantha,

    I did not find any templates, only some code which I could not figure out.

     

    Thursday, February 3, 2011 8:40 AM
  • User1520641890 posted

    hi,

    How do I format the code as u have done?

    i assume you used the forum's built-in 'Insert code' button at the top of the post editor.  in a word you need patience. and i'm not being silly when saying that, the code editor on this site is terrible, have a look at the feedback forum to confirm if you like; it's a known and long-running issue. there have been times where i've had to literally make a post three or four times until it came out presentable

    I don't find a complete tutorial for it.All I find is that they are selling a book.

    yes, that's the price you pay for using open source. the book is the documentation. your other option is to dig in the source code and/or the PDF specification.

    IS it possible to resize an image before adding it to a cell.

    yes, but you have to manually add the code to do it. here's one way. there are other examples of resizing images on my site too. one word of caution. iText[Sharp] uses a different unit of measurement when compared to web images. see the best explanation you'll find on the subject. (the link to the iText creator's site referenced in that post is dead, as that post is old, but the information is still relevant) i.e. iText[Sharp] uses points (pt, like what we use in web to define font size), while web images use pixels.

    Text are added by way of chunks. How do I get the capacity of a cell so that I can shorten a chunk larger than it?

    i'm not sure i understand what you mean by that. iText[Sharp] automatically calculates the table cell's height depending on the font size, etc. one thing to take note of is that once you define the cell width when instantiating the PdfPTable, the cell width cannot be changed.

    Is it possible to draw charts or it has to be imported as an image?

    chart suport is not built-in. you need to create yourself and import, as you thought.


    Thursday, February 3, 2011 7:39 PM
  • User-14001990 posted

    Hi

    Thanks for your reply.

    Text are added by way of chunks. How do I get the capacity of a cell so that I can shorten a chunk larger than it?

    i'm not sure i understand what you mean by that. iText[Sharp] automatically calculates the table cell's height depending on the font size, etc. one thing to take note of is that once you define the cell width when instantiating the PdfPTable, the cell width cannot be changed.

     

    So If the cell width cannot be changed and the amount of text in the chunk is more then it will grow in height.

    How can I prevent this behavior?

    There are two ways of  adding chunks according to me.

    One way is to put ... in place of the extra text.So if a cell's capacity is 50 characters and there are more then put ... for the rest.

    The other is to have a textarea kind of a thing which will grow in size inside the cell and put a scroll bar at the side.

    Which of these is possible?

    I don't find a complete tutorial for it.All I find is that they are selling a book.

    yes, that's the price you pay for using open source. the book is the documentation. your other option is to dig in the source code and/or the PDF specification.

     Where is the pdf specification?

    What r the other free sources for knowledge on itextsharp?

    If anybody is willing to purchase the book and would like someone to share the cost then send me an email.

     

    Monday, February 7, 2011 7:03 AM