Search code examples
c#alignmentitext

Align Cells inside a pdfTable - ITextSharp


I'm trying to centralize the header of the columns and the "Money values"align right.

This is my header,is currentlyn align left,i need to center

 PdfPCell cell = new PdfPCell(new iText.Phrase(""));
                cell.HorizontalAlignment = 1;
                cell.Colspan = 7;
                pdfTable.HorizontalAlignment = 1;
                pdfTable.AddCell(new iText.Phrase("NF", font4));
                pdfTable.AddCell(new iText.Phrase("Emissão", font4));
                pdfTable.AddCell(new iText.Phrase("Vencimento", font4));
                pdfTable.AddCell(new iText.Phrase("Dias", font4));
                pdfTable.AddCell(new iText.Phrase("Valor(R$)", font4));
                pdfTable.AddCell(new iText.Phrase("Encargos(R$)", font4));
                pdfTable.AddCell(new iText.Phrase("Vlr. Final(R$)", font4));

And this is where i fill the table,the Columns "Valor(R$)","Encargos(R$)","Vlr. Final(R$)" need to be align right,someone knows what i'm doing wrong?

 foreach (DataRow r in dtNotaAceite.Rows)
                {
                    if (dtNotaAceite.Rows.Count > 0)
                    {
                        DateTime dataEmissao = (DateTime) r["Dtemissao"];
                        DateTime dataVenc = (DateTime)r["Dtvenc"];
                        string emissaoFormatada = dataEmissao.ToString("dd/M/yyyy");
                        string vencFormatada = dataVenc.ToString("dd/M/yyyy");
                        r["VLFINAL"] = Convert.ToDecimal(r["Valor"]) - Convert.ToDecimal(r["VLENCARGOS"]);
                        pdfTable.AddCell(new iText.Phrase(r["Belnr"].ToString(), font3));
                        pdfTable.AddCell(new iText.Phrase(emissaoFormatada, font3));
                        r["Dtvenc"] = String.Format("{0:dd-MM-yyyy}", r["Dtvenc"]);
                        pdfTable.AddCell(new iText.Phrase(vencFormatada, font3));
                        pdfTable.AddCell(new iText.Phrase(r["DIASANTEC"].ToString(), font3));
                        pdfTable.AddCell(new iText.Phrase(r["Valor"].ToString(), font3));
                        r["VLENCARGOS"] = String.Format("{0:0.##}", r["VLENCARGOS"]);
                        pdfTable.AddCell(new iText.Phrase(r["VLENCARGOS"].ToString(), font3));
                        r["VLFINAL"] =  String.Format("{0:0.##}", r["VLFINAL"]);
                        pdfTable.AddCell(new iText.Phrase(r["VLFINAL"].ToString(), font3));
                    }
                }

Solution

  • What are you doing wrong?

    You create a PdfPCell named cell for which you define an alignment (in an ugly way, you should use Element.ALIGN_CENTER so that we can read and understand your code) and a colspan of 7. But you don't use that cell object anywhere...

    You create a PdfPTable for which you define an alignment (in the same ugly way). But this defines the alignment of the table as a whole, not the content of the cells.

    You then add a series of cells to this table using the AddCell() method. This means that you want iText to create the PdfPCell. In that case, you can pre-define some properties in the default cell property:

    pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
    

    You can change the properties of the default cell after adding a certain number of cells, for instance:

     pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
     pdfTable.AddCell(new iText.Phrase("NF", font4));
     pdfTable.AddCell(new iText.Phrase("Emissão", font4));
     pdfTable.AddCell(new iText.Phrase("Vencimento", font4));
     pdfTable.AddCell(new iText.Phrase("Dias", font4));
     pdfTable.AddCell(new iText.Phrase("Valor(R$)", font4));
     pdfTable.AddCell(new iText.Phrase("Encargos(R$)", font4));
     pdfTable.AddCell(new iText.Phrase("Vlr. Final(R$)", font4));
     pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
    

    All the cells added with AddCell() after this last line will be right-aligned.