2010-06-11 65 views
8

我在iTextSharp庫中遇到了奇怪的問題。 我正在將圖像添加到PdfPCell中,出於某種原因它被放大了。 我如何保持原始尺寸? Here's the image PDF爲100%,原始大小的圖像在paint.net中打開。 alt text http://a.yfrog.com/img697/7102/3kn.png 我雖然打印時圖像相同,但圖像上的差異與印刷版本相同。必須使用ScaleXXX手動縮放圖像才能將其右移,看起來有點不合邏輯,並且不能給出好的結果。使用iTextSharp在PdfPCell中自動調整大小

那麼如何將圖像原始大小放在表格的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); 
+0

iTextSharp的使用版本是? – Stewbob 2010-06-12 02:02:51

+0

我使用iTextSharp 5.0.2.0 – 2010-06-12 02:10:38

+0

這就像斯洛文尼亞的凌晨4點。你有沒有睡過? :) – Stewbob 2010-06-12 02:13:43

回答

13

我使用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究竟有什麼不同。
我以三種不同的分辨率,72dpi,96 dpi和110 dpi創建了一個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設置沒有區別。

+0

我試過PPCPCell cell = new PdfPCell(image);和doc.Add(image); (直接到文檔添加)並且它們給出相同大小的結果。它可能與DPI有關嗎? – 2010-06-12 02:17:53

+0

是的。很多圖像,比如Paint.NET中顯示的圖像都是96 dpi(或其他)。 iTextSharp始終使用72 dpi。我知道如果圖像的dpi與72不同,您可能會得到不同的結果。我忘記了這一點,因爲我總是在72dpi創建我的'用於pdf'圖像。 – Stewbob 2010-06-12 02:26:08

+0

72?在這裏接受的答案是110:http://stackoverflow.com/questions/2752789/how-to-calculate-the-correct-image-size-in-out-pdf-using-itextsharp 或我誤解了那個答案? – 2010-06-12 03:06:32

2

所以,如果你有十個分量圖像的尺寸在PdfPCell可以loock這段代碼:

   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; 
        } 
       } 
9

@ 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); 

我不t確切地知道爲什麼這是,如果您在構造函數中添加圖像,而在稍後添加圖像時(這種情況下每個對象應該照看它),那麼它與單元格處於「文本模式」有關它自己對齊)。

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

+0

這可能是最奇特的功能,而不是我見過的錯誤。非常感謝 – Tommy 2012-05-05 15:29:18

+0

Upvoting這個答案,因爲它讓我找到同樣的問題的解決方案。但是我遇到了一個完全不同的行爲 - AddElement方法會將圖像保留爲您設置的大小,但構造函數將調整圖像大小。我正在使用iTextSharp 5.5.3.0。 – 2015-06-29 10:49:58

1

功能必須適應圖像的特性。只添加一個true

cell.AddElement(image,true); 
+4

您使用哪個版本的itextsharp或變量「cell」的類型?在我的(類型PdfPCell)我只能找到方法:AddElement(IElement元素) – Bronek 2013-07-04 10:34:00

+0

我沒有這種重載要麼 - 使用iTextSharp 5.5.9。 – rumblefx0 2016-07-25 12:13:08