使用iTextSharp在PdfPCell中自动调整图像大小

我对iTextSharp库中的图像有一个奇怪的问题。 我正在将图像添加到PdfPCell中,并且由于某种原因它会被放大。 我如何保持原始尺寸? 这是 100%的PDF 图像 ,并在paint.net中打开原始大小的图像。 alt text http://sofzh.miximages.com/c%23/3kn.png我虽然打印时图像相同,但图片上的差异在打印版本上是相同的。 必须使用ScaleXXX手动缩放图像以使其向右移动似乎有点不合逻辑并且不会给出好的结果。

那么如何将图像的原始大小放在表格的PdfPCell中而不必缩放呢?

这是我的代码:

private PdfPTable CreateTestPDF() { PdfPTable table = new PdfPTable(1); table.WidthPercentage = 100; Phrase phrase = new Phrase("MY TITLE", _font24Bold); table.AddCell(phrase); PdfPTable nestedTable = new PdfPTable(5); table.WidthPercentage = 100; Phrase cellText = new Phrase("cell 1", _font9BoldBlack); nestedTable.AddCell(cellText); cellText = new Phrase("cell 2", _font9BoldBlack); nestedTable.AddCell(cellText); cellText = new Phrase("cell 3", _font9BoldBlack); nestedTable.AddCell(cellText); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(@"d:\MyPic.jpg"); image.Alignment = iTextSharp.text.Image.ALIGN_CENTER; PdfPCell cell = new PdfPCell(image); cell.HorizontalAlignment = PdfPCell.ALIGN_MIDDLE; nestedTable.AddCell(cell); cellText = new Phrase("cell 5", _font9BoldBlack); nestedTable.AddCell(cellText); nestedTable.AddCell(""); string articleInfo = "Test Text"; cellText = new Phrase(articleInfo, _font8Black); nestedTable.AddCell(cellText); nestedTable.AddCell(""); nestedTable.AddCell(""); nestedTable.AddCell(""); table.AddCell(nestedTable); SetBorderSizeForAllCells(table, iTextSharp.text.Rectangle.NO_BORDER); return table; } static BaseColor _textColor = new BaseColor(154, 154, 154); iTextSharp.text.Font _font8 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, _textColor); iTextSharp.text.Font _font8Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); iTextSharp.text.Font _font9 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, _textColor); iTextSharp.text.Font _font9BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.BOLD, BaseColor.BLACK); iTextSharp.text.Font _font10 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, _textColor); iTextSharp.text.Font _font10Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); iTextSharp.text.Font _font10BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK); iTextSharp.text.Font _font24Bold = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 24, iTextSharp.text.Font.BOLD, _textColor); 

我正在使用iTextSharp v4.1.2,我得到以下行为:

使用此代码,通过AddCell方法将图像直接添加到表中,图像将按比例放大以适合单元格:

 nestedTable.AddCell(image); 

使用此代码,将图像添加到单元格,然后将单元格添加到表格中,图像将以其原始大小显示:

 PdfPCell cell = new PdfPCell(image); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; nestedTable.AddCell(cell); 

您是否已将图像直接添加到pdf文档(表格外)以比较/仔细检查图像大小?

 document.add(image); 


我假设您希望图像以单元格为中心,周围有一些空间。 作为最后的手段,您可以更改图像。 使其成为具有透明背景的png,并确保图像的所有边缘周围都有一些透明的“边距”。

编辑

我刚下载了v5.0.2,得到了与上面提到的相同的结果。 我尝试过的图像既小又大于细胞的大小,行为是一样的; 第一种方法缩放图像,第二种方法不缩放。

编辑

好吧,显然我多年来一直错误地谈到整个DPI的事情。 我似乎无法看到它对图像的DPI有什么影响。
我以三种不同的分辨率创建了一个600x400px的图像,72dpi,96 dpi和110 dpi。 然后我将每个这些图像添加到一个恰好600×400的新文档中。

 Dim pSize As Rectangle = New Rectangle(600, 1000) Dim document As Document = New Document(pSize, 0, 0, 0, 0) 

对于三个图像文件中的每一个,当添加到文档中时

 document.add(image) 

它们完美地适合文档,不同的DPI设置没有区别。

@ Stewbob的答案确实有效,但它只是与表的方法有关。

iTextSharp的问题在于它的行为会有所不同,具体取决于您使用的构造函数。 这将(恼人地)放大图像以填充单元格:

 PdfPCell c = new PdfPCell(); c.Add(image); c.setHorizontalAlignment(Element.ALIGN_CENTER); // this will be ignored 

但这会使图像保持您设置的大小(并允许对齐):

 PdfPCell c = new PdfPCell(image); c.setHorizontalAlignment(Element.ALIGN_CENTER); 

我不确切地知道为什么会这样,如果你在构造函数中添加图像而不是’复合模式’,如果你稍后添加它(这种情况下每个对象都是假设的话)照顾它自​​己的对齐方式)。

更多信息(在Java中,但仍然适用) http://tutorials.jenkov.com/java-itext/table.html#cell-modes

因此,如果您必须保留PdfPCell中图像的大小,您可以使用以下代码:

  iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilePath); // Save the image width float width = image.Width; PdfPCell cell = new PdfPCell(); cell.AddElement(image); // Now find the Image element in the cell and resize it foreach (IElement element in cell.CompositeElements) { // The inserted image is stored in a PdfPTable, so when you find // the table element just set the table width with the image width, and lock it. PdfPTable tblImg = element as PdfPTable; if (tblImg != null) { tblImg.TotalWidth = width; tblImg.LockedWidth = true; } } 

该函数具有适合图像的属性。 只添加一个真实的

 cell.AddElement(image,true); 

对于那些要求过载的人,请使用:

 var imageCell = new PdfPCell(image, true); 

代替 :

 cell.AddElement(image,true);