How to specify table position in pdf file using iTextsharp in C #

I created a pdf file with graphics on it, now I'm trying to add a table for this graphics. My problem is that the table is above the graphics, how can I indicate the location / position where I want my table to be placed in a 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 indicate the position of the graphics in the document: // position y, position x, length, height, suspense cb.RoundRectangle (20f, 745f, 200f, 35f, 10f);

This how my document looks:

I want to place a table under the graphics.

+5
source share
2 answers

You mix a low-level approach (adding content in absolute positions) with a high-level approach (using document.add()) for the content of the page.

, . , , . document.add(), iText , ( , ).

, , , itext , .

: Java | # | PDF

, , / . 5 .

, , table.WriteSelectedRows(...). , , .

+1
private static void DemoTableSpacing() { 
    using (FileStream fs = new FileStream("SpacingTest.pdf", FileMode.Create)) { 

        Document doc = new Document(); 
        PdfWriter.GetInstance(doc, fs); 
        doc.Open(); 

        Paragraph paragraphTable1 = new Paragraph(); 
        paragraphTable1.SpacingAfter = 15f; 

        PdfPTable table = new PdfPTable(3); 
        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"); 
        paragraphTable1.Add(table); 
        doc.Add(paragraphTable1); 

        Paragraph paragraphTable2 = new Paragraph(); 
        paragraphTable2.SpacingAfter = 10f; 

        table = new PdfPTable(3); 
        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"); 
        paragraphTable2.Add(table); 
        doc.Add(paragraphTable2); 
        doc.Close(); 
    } 
}

ITEXTSHARP: https://www.codeproject.com/Questions/351802/Its-possible-put-a-table-in-absolute-position-with

0

All Articles