1

I want to dynamically align the iText PdfTable.

How to set the x and y position based alignment in iTextPDF.

PdfPCell cell;
cell = new PdfPCell(testTable);
cell.setFixedHeight(44f);
cell.setColspan(3);
cell.setBorder(0);
table.addCell(cell);  
table1.addCell(table);
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Pradeep.PG
  • 189
  • 1
  • 1
  • 10

2 Answers2

1

please look in to this example...

public static void Main() {


        // step 1: creation of a document-object
        Document document = new Document();


        try {


            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1002.pdf", FileMode.Create));


            // step 3: we open the document
            document.Open();


            // step 4: we grab the ContentByte and do some stuff with it
            PdfContentByte cb = writer.DirectContent;


            // we tell the ContentByte we're ready to draw text
            cb.beginText();


            // we draw some text on a certain position
            cb.setTextMatrix(100, 400);
            cb.showText("Text at position 100,400.");


            // we tell the contentByte, we've finished drawing text
            cb.endText();
        }
        catch(DocumentException de) {
            Console.Error.WriteLine(de.Message);
        }
        catch(IOException ioe) {
            Console.Error.WriteLine(ioe.Message);
        }


        // step 5: we close the document
        document.Close();
    }
}
ess
  • 663
  • 3
  • 9
  • 17
  • 1
    This doesn't answer the question, does it? Adding a table at an absolute position is done with the `WriteSelectedRows()` method or by wrapping the table in a `ColumnText` object. – Bruno Lowagie Feb 05 '14 at 13:17
0

Please take a look at the C# port of the examples of chapter 4 of my book: http://tinyurl.com/itextsharpIIA2C04

You can add the table to a ColumnText object and add the column at an absolute position:

ColumnText column = new ColumnText(writer.DirectContent);
column.AddElement(table);
column.SetSimpleColumn(llx, lly, urx, ury);
column.Go();

In this snippet llx, lly and urx, ury are the coordinates of the lower-left corner and the upper-right corner of the column on the page (see the ColumnTable example).

In the PdfCalendar example, another method is used:

table.WriteSelectedRows(0, -1, x, y, writer.DirectContent); 

The first parameters define which rows need to be drawn (0 to -1 means all rows), x and y define the absolute position.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165