2014-04-29 55 views
2

我想使用它的自然尺寸的細胞背景,如果他們不適合細胞 - 圖像應該裁剪。 當我使用圖案填充圖像時,還有一件事實際上是90度旋轉。所以這個問題的主要原因是爲什麼模式圖像旋轉後添加 我已經搜索答案和閱讀文檔,但無法找到任何解釋。 這裏是代碼:iTextSharp。爲什麼細胞背景圖像順時針旋轉90度?

Image img = Image.GetInstance("someImage.png"); 

var cell = new PdfPCell() 
{ 
    BorderWidthTop = 0, 
    BorderWidthBottom = 0, 
    BorderWidthLeft = 0, 
    BorderWidthRight = 0, 
    Padding = 0, 
    BackgroundColor = new BaseColor(244, 244, 244), 
    BorderColor = new BaseColor(217, 217, 217), 
    HorizontalAlignment = Element.ALIGN_CENTER 
}; 
cell.CellEvent = new CellBackgroundEvent() {Image = img}; 
table.AddCell(cell) 
; 

這裏是事件處理類:

private class CellBackgroundEvent : IPdfPCellEvent 
{ 
    public Image Image { get; set; } 

    void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) 
    { 
     PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; 
     PdfPatternPainter patternPainter = cb.CreatePattern(position.Right - position.Left, position.Top - position.Bottom); 
     patternPainter.AddImage(Image, position.Right - position.Left, 0, 0, position.Top - position.Bottom, 0, 0); 
     cb.SaveState(); 
     cb.SetPatternFill(patternPainter); 
     cb.Rectangle(position.Left, position.Bottom, position.Width, position.Height); 
     cb.Fill(); 
     cb.RestoreState(); 
    } 
} 

後執行單元格背景圖像順時針旋轉90度,爲什麼呢?

圖片:enter image description here

實際結果細胞是:

enter image description here

C#iTextSharp的庫版本:5.5.0.0

+0

我很困惑。您縮放背景圖像以適應單元格,然後使用模式填充背景? **你爲什麼要這樣做?**此外,你有單元格的位置,但你在硬編碼座標(100,100)處加上*它的背景*。 **爲什麼?**我很抱歉,但您的代碼沒有意義。 –

+0

是的,你是正確的擴展是額外的,只是刪除它。我需要獲取細胞背景圖片 - 所以我使用圖案。硬編碼(100,100)實際上僅用於測試自己以查看完整背景圖像,現在已更正。但一般圖片是相同的 - 圖像順時針旋轉90度,我不知道爲什麼,這就是爲什麼我要求幫助 – Vova

+0

[1.],所以你想使用圖像作爲圖案顏色(平鋪),而不是比將完整圖像作爲單元格的背景? [2.]我無法重現該問題。你能分享圖像嗎? –

回答

1

你試圖使用非常複雜的實現很簡單的東西碼。這就是當你代碼由Google而不是reading the documentation發生的情況!

請試試這個:

PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; 
Image.ScaleToFit(position.Top - position.Bottom,position.Right - position.Left); 
Image.SetAbsolutePosition(position.Bottom, position.Left); 
cb.AddImage(image); 

現在你縮放圖像以適應單元格,並在單元格的左下角的座標添加。

更新1:

已經建立,你實際上要平鋪圖像後,我寫了一個例子試圖重現該問題。由於我不瞭解你的代碼(*),我不得不重寫大部分代碼。

無論如何,你會發現一個正在運行的Java示例here。由此產生的PDF看起來像this圖像不旋轉,是嗎?它看起來與你的問題看起來完全一樣。

這是相關代碼:你忽略了這樣一個事實:Rectangle也可以給你它的寬度和高度

public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
    try { 
     PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; 
     PdfPatternPainter patternPainter = cb.createPattern(image.getScaledWidth(), image.getScaledHeight()); 
     image.setAbsolutePosition(0, 0); 
     patternPainter.addImage(image); 
     cb.saveState(); 
     cb.setPatternFill(patternPainter); 
     cb.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight()); 
     cb.fill(); 
     cb.restoreState(); 
    } catch (DocumentException e) { 
     throw new ExceptionConverter(e); 
    } 
} 

(*)注。您最好使用AddImage()方法的難度版本。你困惑與圖像的尺寸電池的尺寸...

更新2:

Java的例子被移植到C#(使用iTextSharp的5.5.0.0):

using System; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace ConsoleApplication1 { 
    public class TiledImageBackground : IPdfPCellEvent { 
     protected Image image; 

     public TiledImageBackground(Image image) { 
      this.image = image; 
     } 

     public void CellLayout(PdfPCell cell, Rectangle position, 
      PdfContentByte[] canvases) { 
      PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; 
      PdfPatternPainter patternPainter = cb.CreatePattern(image.ScaledWidth, image.ScaledHeight); 
      image.SetAbsolutePosition(0, 0); 
      patternPainter.AddImage(image); 
      cb.SaveState(); 
      cb.SetPatternFill(patternPainter); 
      cb.Rectangle(position.Left, position.Bottom, position.Width, position.Height); 
      cb.Fill(); 
      cb.RestoreState(); 
     } 
    } 


    public class TiledBackground { 
     public const String DEST = "results/tables/tiled_pattern.pdf"; 
     public const String IMG1 = "resources/images/ALxRF.png"; 
     public const String IMG2 = "resources/images/bulb.gif"; 

     private static void Main(string[] args) { 
      Directory.CreateDirectory(Directory.GetParent(DEST).FullName); 
      new TiledBackground().CreatePdf(DEST); 
     } 

     public void CreatePdf(String dest) { 
      Document document = new Document(); 
      PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create)); 
      document.Open(); 
      PdfPTable table = new PdfPTable(2); 
      PdfPCell cell = new PdfPCell(); 
      Image image = Image.GetInstance(IMG1); 
      cell.CellEvent = new TiledImageBackground(image); 
      cell.FixedHeight = 770; 
      table.AddCell(cell); 
      cell = new PdfPCell(); 
      image = Image.GetInstance(IMG2); 
      cell.CellEvent = new TiledImageBackground(image); 
      cell.FixedHeight = 770; 
      table.AddCell(cell); 
      document.Add(table); 
      document.Close(); 
     } 
    } 
} 

該代碼無法複製問題。換句話說:iTextSharp中沒有錯誤,功能被正確移植。

請花點時間嘗試從此答案的示例代碼,以確保您自己的錯誤是在您的代碼中。如果您在更改方向時仍然存在問題,則需要查看代碼中的不同內容。

更新3:

額外的評論後,我們現在已經建立的90度旋轉是故意的。文檔是使用創建的:

Document document = new Document(PageSize.A4.Rotate()); 

在這種情況下,將創建一個文件,它的寬度比(所述媒體框被定義爲在縱向矩形)的高度較小,但你旋轉該網頁(將一個旋轉入口等於90到頁面字典)。

如果你想在模式匹配的景觀頁面的方向,你有兩個選擇:

  • 任你旋轉模式:img_pattern.setPatternMatrix(0, 1, -1, 0, 0, 0);
  • 或您創建的景觀MediaBoxnew Document(new Rectangle(842, 595));
+0

(*)評論:是的,謝謝你的注意,更新的描述。 一個小小的說明:我使用的是C#iTextSharp lib v.5.5.0.0,並且在代碼集成之後,它仍然顯示爲旋轉(見更新的問題詳細信息) – Vova

+0

通常,C#端口的行爲應該與Java版本。我會製作一張內部(付費)票證來檢查這個(低優先級,除非你是付費客戶)。如果你是對的,我們會解決它。 –

+0

所以,您已經將我的Java示例移植到C#中,並且您說它的行爲有所不同。我的例子中燈泡的方向是什麼?它是否也旋轉? http://www.itextpdf.com/img/bulb.gif請讓我看看PDF。 –