Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align itextsharp table

Tags:

itext

Does anyone know how to left align a iTextSharp table?

like image 993
Muleskinner Avatar asked Nov 10 '10 15:11

Muleskinner


People also ask

How do I set the cell alignment in iText?

How do I set Table’s cell alignment in iText? We can set table cell’s content alignment horizontally and vertically. To set the horizontal alignment we use the setHorizontalAlignment () method. To align it vertically we use the setVerticalAlignment () method. The alignment constant is defined in the com.itextpdf.text.Element class.

How difficult is it to work with tables using iTextSharp?

Working with tables using iTextSharp is not that difficult, especially as many of the property names are so similar or identical to their counterparts within CSS and HTML.

What is the equivalent replacement for iTextSharp?

the equivalent replacement for iTextSharp : paragraph.Alignment = Element.ALIGN_CENTER; or try this way paragraph.IndentationRight = 100; paragraph.IndentationLeft = 100; Share

How do I set the alignment of an element in HTML?

To set the horizontal alignment we use the setHorizontalAlignment () method. To align it vertically we use the setVerticalAlignment () method. The alignment constant is defined in the com.itextpdf.text.Element class. A programmer, runner, recreational diver, live in the island of Bali, Indonesia.


3 Answers

You can use the PdfPTable's HorizontalAlignment property.

Here's a C# test method you can use to experiment:

    private void TestTableCreation() {
        using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {
            Document doc = new Document(PageSize.A4);
            PdfWriter.GetInstance(doc, fs);
            doc.Open();

            PdfPTable table = new PdfPTable(4);
            table.WidthPercentage = 50.0f;
            // Options: Element.ALIGN_LEFT (or 0), Element.ALIGN_CENTER (1), Element.ALIGN_RIGHT (2).
            table.HorizontalAlignment = Element.ALIGN_LEFT;

            for (int i = 1; i <= 20; i++) {
                PdfPCell cell = new PdfPCell(new Phrase(String.Format("Cell # {0}", i)));
                cell.FixedHeight = 30.0f;
                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;

                table.AddCell(cell);
            }

            doc.Add(table);
            doc.Close();
        }
    }
like image 107
Jay Riggs Avatar answered Sep 23 '22 15:09

Jay Riggs


table.HorizontalAlignment = 1;

1:center

0:left

2:right

like image 22
PhilM Avatar answered Sep 23 '22 15:09

PhilM


There are two ways of doing it:

  1. cell.HorizontalAlignment = Element.ALIGN_LEFT;

  2. cell.HorizontalAlignment = 0;

like image 37
vijay Avatar answered Sep 22 '22 15:09

vijay