Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the position of the table in a pdf file using iTextsharp in c#

Tags:

c#

itextsharp

I have created a pdf file that has graphics on it, now am trying to add a table under those graphics. My problem is the table is over the graphics, how do i specify the location/position where i want my table to be placed on the pdf document??

This is my code

        docl.Open();
        docl.Add(new Paragraph("My first PDF file"));

        PdfContentByte cb = writer.DirectContent;
        //employee
        //                position y,position x,length,height,  unknown
        cb.RoundRectangle(   20f,      745f,     200f,   35f,      10f);
        //title
        cb.RoundRectangle(235f, 745f, 35f, 35f, 10f);
        //identity number
        cb.RoundRectangle(280f, 745f, 105f, 35f, 10f);
        //date of birth
        cb.RoundRectangle(390f, 745f, 105f, 35f, 10f);
        //employee number
        cb.RoundRectangle(500f, 745f, 105f, 35f, 10f);
        //address
        cb.RoundRectangle(20f, 660f, 200f, 80f, 10f);
        //pay method
        cb.RoundRectangle(235f, 700f, 35f, 35f, 10f);
        //brantch code
        cb.RoundRectangle(235f, 660f, 35f, 35f, 10f);
        //bank
        cb.RoundRectangle(280f, 700f, 215f, 35f, 10f);
        //account type
        cb.RoundRectangle(500f, 700f, 105f, 35f, 10f);
        //account number
        cb.RoundRectangle(280f, 660f, 160f, 35f, 10f);
        //pay point
        cb.RoundRectangle(445f, 660f, 35f, 35f, 10f);
        //date of payment
        cb.RoundRectangle(506f, 660f, 90f, 35f, 10f);
        //marital status
        cb.RoundRectangle(20f, 600f, 35f, 35f, 10f);
        //gender
        cb.RoundRectangle(60f, 600f, 35f, 35f, 10f);
        //date of appointment
        cb.RoundRectangle(100f, 600f, 70f, 35f, 10f);
        //Tax number
        cb.RoundRectangle(175f, 600f, 70f, 35f, 10f);
        cb.Stroke();

        PdfPTable table = new PdfPTable(2);
        table.HorizontalAlignment = 0;
        table.SetTotalWidth(new float[] { 800, 200 }); 
        PdfPCell cell = new PdfPCell(new Phrase("EARNINGS"));
        cell.Colspan = 2;
        cell.HorizontalAlignment = 1;
        table.AddCell(cell);
        table.AddCell("Description");
        table.AddCell("Amount");

I used this line to specify the position of the graphics on the document: // position y, position x,length,height, unknown cb.RoundRectangle( 20f, 745f, 200f, 35f, 10f);

This how my document looks:

I want to place the table below the graphics.

like image 578
Thando Tee Avatar asked Nov 13 '22 07:11

Thando Tee


1 Answers

You are mixing the low-level approach (adding content at absolute positions) with the high-level approach (using document.add()) for page content.

Either you stick to the high-level approach, by using a table to create the round rectangles. You can create tables with borders that have rounded corners using cell and table events. When you use document.add(), iText will take care of positioning everything (including splitting the table if it doesn't fit the page).

Or you stick to the low-level approach, by adding the table at an absolute position, but be aware that itext won't split the table if it doesn't fit the page.

Take a look at this example: Java | C# | PDF

It shows how to create rounded borders for tables by using cell events and/or table events. See the other examples of chapter 5 for less complex sample code.

As you can see in the calendar example, the table is added at an absolute position using the method table.WriteSelectedRows(...). As you know the coordinates of the rounded rectangles, you can use this method to add your table at an absolute position.

like image 186
Bruno Lowagie Avatar answered Nov 14 '22 23:11

Bruno Lowagie