代码之家  ›  专栏  ›  技术社区  ›  Mladen Prajdic

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

  •  10
  • Mladen Prajdic  · 技术社区  · 15 年前

    我将图像添加到PdfPCell中,由于某种原因,它被放大了。 如何保持原样尺寸?

    我原以为照片在印刷时是一样的,但照片上的差别在印刷版上是一样的。必须用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);
    
    5 回复  |  直到 6 年前
        1
  •  14
  •   Mick Byrne    13 年前

    使用此代码,通过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有什么区别。
    我创建了一个分辨率为72dpi、96dpi和110dpi的600x400px图像。然后我将这些图像添加到一个新的文档中,这个文档正好是600x400。

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

    document.add(image)
    

    它们非常适合文档,不同的DPI设置没有差异。

        2
  •  9
  •   Mick Byrne    13 年前

    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

        3
  •  3
  •   Alexei    14 年前

                    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;
                        }
                    }
    
        4
  •  1
  •   Iván Ruisoto Alonso    12 年前

    函数有一个适合图像的属性。只添加一个 是的

    cell.AddElement(image,true);
    
        5
  •  0
  •   Zak    7 年前

    对于要求过载的用户,请使用以下命令:

    var imageCell = new PdfPCell(image, true);
    

    而不是:

    cell.AddElement(image,true);