c# - 图像覆盖 iTextSharp 中的表格单元格边框

标签 c# itext border

我正在尝试使用 iTextSharp 库(版本 4.1.2.0)创建 PDF。在文档的顶部,我想添加 Logo 、水平线以及线下方的一些文本(标题)。

我试图通过以下方式实现这一目标:

  1. 创建具有一列大小的 PdfPTable
  2. 向其中添加一个 PdfPCell,其边框设置为 BOTTOM_BORDER,其中包含 Logo 图像
  3. 将另一个带有标题文本的 PdfPCell 添加到 PdfPTable

但是,在生成的 PDF 中,图像会覆盖底部边框(否则显示正常)。我怎样才能阻止它覆盖它?我能够做到这一点的唯一方法是将 PdfPCell 构造函数的 FIT 参数设置为 true,但这完全破坏了布局(图像巨大)。

代码:

        document.Open();

        Image img = Image.GetInstance("Logo.PNG");
        PdfPCell cell = new PdfPCell(img, false);
        PdfPTable table = new PdfPTable(1);
        cell.Border = PdfPCell.BOTTOM_BORDER;
        table.DefaultCell.Border = PdfPCell.NO_BORDER;
        table.WidthPercentage = 100;
        table.AddCell(cell);

        PdfPCell cell2 = new PdfPCell(new Phrase("Title"));
        cell2.Border = PdfPCell.TOP_BORDER;
        table.AddCell(cell2);
        document.Add(table);

        document.Close();

它是这样的:

This is how it looks like

最佳答案

尝试添加以下内容:

cell.PaddingBottom = 5;

所以更新后的代码是:

PdfPTable table = new PdfPTable(1);
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.WidthPercentage = 100;

Image img = Image.GetInstance("Logo.PNG");
PdfPCell cell = new PdfPCell(img, false);
cell.Border = PdfPCell.BOTTOM_BORDER;
cell.PaddingBottom = 5;
table.AddCell(cell);

PdfPCell cell2 = new PdfPCell(new Phrase("Title"));
table.AddCell(cell2);

document.Add(table);
document.Close();

还有其他可以尝试的事情:

cell.UseBorderPadding = true;

关于c# - 图像覆盖 iTextSharp 中的表格单元格边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240313/

相关文章:

java - iText 单元格宽度似乎表现不一致

html - 边框 CSS - 意外行为

CSS 边框锯齿状,带有不同大小/颜色的边框

html - CSS:td 上的双边框显示为单边框

c# - 如何更改 exe 文件的文件和产品版本

c# - Visual Studio 2019 for .Net Core 3.1 中的 WinForms 设计器窗口

c# - 使用 BouncyCaSTLe 在 C# 中读取 DER 私钥

java - 条形码生成服务器端还是客户端?

c# - iTextSharp-如何从 pdf 文件的第一页创建缩略​​图

c# - ASP.Net MVC - String.Join()