你試圖使用非常複雜的實現很簡單的東西碼。這就是當你代碼由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);
- 或您創建的景觀
MediaBox
:new Document(new Rectangle(842, 595));
我很困惑。您縮放背景圖像以適應單元格,然後使用模式填充背景? **你爲什麼要這樣做?**此外,你有單元格的位置,但你在硬編碼座標(100,100)處加上*它的背景*。 **爲什麼?**我很抱歉,但您的代碼沒有意義。 –
是的,你是正確的擴展是額外的,只是刪除它。我需要獲取細胞背景圖片 - 所以我使用圖案。硬編碼(100,100)實際上僅用於測試自己以查看完整背景圖像,現在已更正。但一般圖片是相同的 - 圖像順時針旋轉90度,我不知道爲什麼,這就是爲什麼我要求幫助 – Vova
[1.],所以你想使用圖像作爲圖案顏色(平鋪),而不是比將完整圖像作爲單元格的背景? [2.]我無法重現該問題。你能分享圖像嗎? –