在同一单元格ITextSharp中插入图像和文本时,文本在两个单元格之间不均匀

时间:2017-09-18 03:11:24

标签: c# itext

我有3列和一行的表。在单元格1中,我有一个短语,在单元格2中,短语旁边有一个图像和一个文本。  我能够将图像和文本组合在同一个单元格中。我现在的问题是单元格1中的文本位于比单元格2中的文本更高的位置,我不知道为什么。我需要它们才是平等的。

这是结果:

enter image description here

这是我的代码:

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");

    }

我不知道代码有什么问题。

提前致谢

1 个答案:

答案 0 :(得分:1)

感谢@BrunoLowagie,我能够提出一个解决方案。但是,我还将文本对齐嵌入到中心位置,因此我最终使用段落而不是词组来处理单元格中的所有文本。

这是最终结果:

https://www.imagemagick.org/script/command-line-options.php#deskew

而且,这是最终为我工作的代码:

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");

    }
相关问题