Errol Chaves Moya
Errol Chaves Moya

Reputation: 377

Text is not even between two cells when inserting an image and text in the same cell, ITextSharp

I have table with 3 columns and one row. In cell 1 I have a phrase and in cell 2, next to the phrase I have an Image and a text. I am able to combine the image and the text in a same cell but. The problem I have now is that the text in cell 1 is in a higher position than the text in cell 2 and I don't know why. And I need them to be even.

this is the result:

enter image description here

this is my code:

private void btnCrear_Click(object sender, EventArgs e)
    {
        Document doc = new Document(PageSize.LETTER);

        PdfWriter writer = PdfWriter.GetInstance(doc,
                                    new FileStream((Application.StartupPath+"\\PSC.pdf"), FileMode.Create));

        doc.AddTitle("Recibo de Pago de Derechos Laborales");
        doc.AddCreator("Errol");


        doc.Open();

        /*Get Image and set size*/
        iTextSharp.text.Image SC = iTextSharp.text.Image.GetInstance(Application.StartupPath+"\\SimboloColones.png");
        SC.ScaleAbsolute(5, 8);

        PdfPTable table = new PdfPTable(3);/*3 columns*/
        table.TotalWidth = 588;
        table.LockedWidth = true;

        /*Cell 1*/
        PdfPCell cell = new PdfPCell(new Phrase("Preaviso"));
        cell.Colspan = 1;
        cell.HorizontalAlignment = 2; 
        cell.BorderColor = BaseColor.BLACK;
        cell.BorderWidthBottom = 0;
        cell.BorderWidthTop = 0;
        cell.BorderWidthRight = 0;
        cell.BorderWidthLeft = 0;
        table.AddCell(cell);

        /*Cell 2*/
        cell = new PdfPCell();
        cell.Colspan = 1;
        cell.HorizontalAlignment = 0;
        cell.BorderColor = BaseColor.BLACK;

        /*Insert Image and text into the cell*/
        Phrase pPreaviso = new Phrase();
        pPreaviso.Add(new Chunk(SC, 0, 0));
        pPreaviso.Add(new Chunk("Cant Pre"));

        cell.AddElement(pPreaviso);

        table.AddCell(cell);

        /*Cell 3*/
        cell = new PdfPCell(new Phrase("                 "));
        cell.Colspan = 1;
        cell.HorizontalAlignment = 1;
        cell.BorderColor = BaseColor.BLACK;
        cell.BorderWidthBottom = 0;
        cell.BorderWidthTop = 0;
        cell.BorderWidthRight = 0;
        cell.BorderWidthLeft = 0;
        table.AddCell(cell);

        doc.Add(table);

        doc.Close();
        writer.Close();
        System.Diagnostics.Process.Start(Application.StartupPath+"\\PSC.pdf");

    }

I don't know what is wrong with the code.

Thanks in advance

Upvotes: 1

Views: 128

Answers (1)

Errol Chaves Moya
Errol Chaves Moya

Reputation: 377

Thanks to @BrunoLowagie I was able to come up with a solution. However, I also nedded the text alignment to be centered, therefore I ended up using Paragraph instead of Phrase for all the texts in the cells.

This is the final result:

enter image description here

And, this is the code that ended up working for me:

private void btnCrear_Click(object sender, EventArgs e)
    {
        Document doc = new Document(PageSize.LETTER);

        PdfWriter writer = PdfWriter.GetInstance(doc,
                                    new FileStream((Application.StartupPath+"\\PSC.pdf"), FileMode.Create));

        doc.AddTitle("Recibo de Pago de Derechos Laborales");
        doc.AddCreator("Errol");

        doc.Open();

        /*Get Image and set size*/
        iTextSharp.text.Image SC = iTextSharp.text.Image.GetInstance(Application.StartupPath+"\\SimboloColones.png");
        SC.ScaleAbsolute(5, 8);

        PdfPTable table = new PdfPTable(3);/*3 columns*/
        table.TotalWidth = 588;
        table.LockedWidth = true;


        /*Cell 1*/
        Paragraph Preaviso = new Paragraph();
        Preaviso.Add(new Chunk("Preaviso"));
        Preaviso.Alignment = 2;
        PdfPCell cell = new PdfPCell();
        cell.Colspan = 1;
        cell.HorizontalAlignment = 2;
        cell.BorderColor = BaseColor.BLACK;
        cell.BorderWidthBottom = 0;
        cell.BorderWidthTop = 0;
        cell.BorderWidthRight = 0;
        cell.BorderWidthLeft = 0;

        cell.AddElement(Preaviso);

        table.AddCell(cell);

        /*Cell 2*/

        Paragraph pPreaviso = new Paragraph();

        SC.ScaleAbsolute(5, 8);
        pPreaviso.Add(new Chunk(SC, 0, 0));
        pPreaviso.Add(new Chunk("Cant Pre"));
        pPreaviso.Alignment = 0;
        cell = new PdfPCell();
        cell.Colspan = 1;
        cell.HorizontalAlignment = 0;
        cell.BorderColor = BaseColor.BLACK;

        cell.AddElement(pPreaviso);

        table.AddCell(cell);

        /*Cell 3*/
        cell = new PdfPCell(new Phrase("                 "));
        cell.Colspan = 1;
        cell.HorizontalAlignment = 1;
        cell.BorderColor = BaseColor.BLACK;
        cell.BorderWidthBottom = 0;
        cell.BorderWidthTop = 0;
        cell.BorderWidthRight = 0;
        cell.BorderWidthLeft = 0;
        table.AddCell(cell);


        doc.Add(table);

        doc.Close();
        writer.Close();
        System.Diagnostics.Process.Start(Application.StartupPath+"\\PSC.pdf");

    }

Upvotes: 1

Related Questions