【问题标题】:Unable to set a fixed size for image itextsharp无法为图像 itextsharp 设置固定大小
【发布时间】:2013-07-10 10:56:14
【问题描述】:

我一直在使用 iTextSharp 创建报告。我的报告有许多不同大小的图像。即使我缩放它们,每个图像都会以不同的大小呈现。我找到了许多解决方案,但都没有帮助。

我的代码:

PdfPCell InnerCell;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath(@"images\Logo.png"));
logo.ScaleToFit(80f, 80f);
InnerCell.FixedHeight = 80f;
InnerCell = new PdfPCell(logo); 

我尝试将图像添加到块中,但图像将自身定位到顶部。由于是动态报告,我无法在块中指定 x 和 y 值

InnerCell = new PdfPCell(new Phrase(new Chunk(logo, 0, 0))); 

我什至尝试过this,但无法获得固定大小。

【问题讨论】:

  • 如果使用 ScaleToFit 会发生什么?
  • 它们根据原始尺寸进行缩放。有些大,有些小,高度和宽度不统一

标签: c# asp.net pdf itextsharp


【解决方案1】:

ScaleToFit(w,h) 将根据源图像的宽度/高度中的较大者按比例缩放图像。缩放多个图像时,除非尺寸的比例都相同,否则您最终会得到不同的尺寸。这是设计使然。

使用ScaleToFit(80,80):

  • 如果您的源是100x100 的正方形,您将得到一个80x80 的正方形
  • 如果您的源是200x100 的矩形,您将获得80x40 的矩形
  • 如果您的源是100x200 的矩形,您将获得40x80 的矩形

无论结果如何,测量宽度和高度,至少有一个是您指定的尺寸之一。

我创建了一个创建随机大小图像的示例程序,它给了我图像中显示的预期输出 (w=80,h=80,h=80,h=80,w=80)

private void test() {
    //Output the file to the desktop
    var testFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
    //Standard PDF creation here, nothing special
    using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
        using (var doc = new Document()) {
            using (var writer = PdfWriter.GetInstance(doc, fs)) {
                doc.Open();

                //Create a random number generator to create some random dimensions and colors
                var r = new Random();

                //Placeholders for the loop
                int w, h;
                Color c;
                iTextSharp.text.Image img;

                //Create 5 images
                for (var i = 0; i < 5; i++) {
                    //Create some random dimensions
                    w = r.Next(25, 500);
                    h = r.Next(25, 500);
                    //Create a random color
                    c = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
                    //Create a random image
                    img = iTextSharp.text.Image.GetInstance(createSampleImage(w, h, c));
                    //Scale the image
                    img.ScaleToFit(80f, 80f);
                    //Add it to our document
                    doc.Add(img);
                }

                doc.Close();
            }
        }
    }
}

/// <summary>
/// Create a single solid color image using the supplied dimensions and color
/// </summary>
private static Byte[] createSampleImage(int width, int height, System.Drawing.Color color) {
    using (var bmp = new System.Drawing.Bitmap(width, height)) {
        using (var g = Graphics.FromImage(bmp)) {
            g.Clear(color);
        }
        using (var ms = new MemoryStream()) {
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }
    }
}

我认为您正在寻找的是能够按比例缩放图像的能力,但也可以使图像“达到那个大小”,这意味着用清晰的或可能的白色像素填充其余像素。请参阅 this post 了解解决方案。

【讨论】:

  • 是否有可能对于所有图像,我可以为所有图像提供固定尺寸 35x35?
  • @ParthTrivedi,看看这个,如果这不起作用,那么你需要打开一个新问题。 stackoverflow.com/a/14598795/231316
猜你喜欢
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
相关资源
最近更新 更多