【问题标题】:iTextSharp Image minimum Table cell heightiTextSharp Image 最小表格单元格高度
【发布时间】:2014-03-02 14:09:34
【问题描述】:

我正在尝试将图像附加到 PDF 文档,但图像宽度必须 = doc.PageSize.Width 并且高度与图像宽度成比例。

我正在使用以下方法在自己的表格和单元格中附加单个图像

public void AddImage(Document doc, iTextSharp.text.Image Image)
{
    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;
    table.TotalWidth = doc.PageSize.Width;

    PdfPCell c = new PdfPCell(Image, true);
    c.Border = PdfPCell.NO_BORDER;
    c.Padding = 5;
    c.FixedHeight = (doc.PageSize.Width / Image.Width) * Image.Height;
    c.MinimumHeight = (doc.PageSize.Width / Image.Width) * Image.Height;

    table.AddCell(c);

    doc.Add(table);
}

文档代码部分(但不要认为这是必要的):

using (PDFBuilder pdf = new PDFBuilder())
{
    using (var doc = new Document())
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        var image = iTextSharp.text.Image.GetInstance(Request.MapPath("~/Images/Nemo.jpg"));
        pdf.AddImage(doc, image);
        pdf.AddImage(doc, image);
        pdf.AddImage(doc, image);
    }
}

我想要的是图像为 100% 宽度,如果不是,则图像必须附加在下一页上。

这是我目前得到的

这就是我想要的

任何帮助将不胜感激,在此先感谢您

【问题讨论】:

  • 您使用的是哪个版本的 iTextSharp?我知道这已在 StackOverflow 上得到修复和回答,但我无法立即找到链接。
  • @BrunoLowagie 我不确定在哪里寻找版本,但在 VS Version = 5.4.5.0.net 2.0,将链接发布为答案 - 我会将其标记为正确答案

标签: c# asp.net image width itextsharp


【解决方案1】:

知道了!

需要添加这一行:

c.Image.ScaleToFitHeight = false;

我的方法

public void AddImage(Document doc, iTextSharp.text.Image Image)
{
    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;

    PdfPCell c = new PdfPCell(Image, true);
    c.Border = PdfPCell.NO_BORDER;
    c.Padding = 5;
    c.Image.ScaleToFitHeight = false; /*The new line*/

    table.AddCell(c);

    doc.Add(table);
}

【讨论】:

  • 是的,我正在寻找它,我也刚刚找到它:stackoverflow.com/questions/17336499/…(我很难找到它,因为这个问题是从六月开始的,我正在寻找上次提出的问题几个月)。无论如何:我赞成你的回答。
  • 感谢您的所有努力!