谢谢你,我知道你很忙..所以我编辑此基础上,你给我的代码... 首先我想让你看看我得到了什么,当我试图您代码..单元格背景图像iTextSharp的提前
,我作为背景的图片:
你看我有几个问题:
1-图像不是细胞的背景,我希望它被拉伸。
2 - 我试图把该文本在不同的位置,但我失败了。
3 - 还我得到了不显示缺少的细胞。
我使用的代码是:
1-的ImageEvent类:
class ImageEvent : IPdfPCellEvent
{
protected Image img;
public ImageEvent(Image img) {
this.img = img;
}
void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
img.ScaleToFit(position.Width, position.Height);
img.SetAbsolutePosition(position.Left + (position.Width - img.Width)/2,
position.Bottom + (position.Height - img.ScaledHeight/2));
PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
try {
canvas.AddImage(img);
} catch (DocumentException ex) {
// do nothing
}
}
}
2-位置类:
class PositionEvent : IPdfPCellEvent
{
protected Phrase content;
protected string pos;
public PositionEvent(Phrase content, string pos)
{
this.content = content;
this.pos = pos;
}
void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
float x = 0;
float y = 0;
int alignment = 0;
switch (pos)
{
case "TOP_LEFT":
x = position.GetLeft(3);
y = position.GetTop(content.Leading);
alignment = Element.ALIGN_LEFT;
break;
case "TOP_RIGHT":
x = position.GetRight(3);
y = position.GetTop(content.Leading);
alignment = Element.ALIGN_RIGHT;
break;
case "BOTTOM_LEFT":
x = position.GetLeft(3);
y = position.GetBottom(3);
alignment = Element.ALIGN_LEFT;
break;
case "BOTTOM_RIGHT":
x = position.GetRight(3);
y = position.GetBottom(3);
alignment = Element.ALIGN_RIGHT;
break;
case "CENTER_TOP":
x = position.GetRight(3) + position.GetLeft(3)/2;
y = position.GetTop(3);
alignment = Element.ALIGN_RIGHT;
break;
case "CENTER_BOTTOM":
x = position.GetRight(3) + position.GetLeft(3)/2;
y = position.GetBottom(3);
alignment = Element.ALIGN_RIGHT;
break;
case "CENTER_MIDDLE":
x = position.GetRight(3) + position.GetLeft(3)/2;
y = x;
alignment = Element.ALIGN_RIGHT;
break;
}
ColumnText.ShowTextAligned(canvas, alignment, content, x, y, 0);
}
}
3-的方法:
public void createPdf(string dest)
{
// 1. Create a Document which contains a table:
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(3);
table.WidthPercentage = 100f;
PdfPCell cell1 = new PdfPCell();
PdfPCell cell2 = new PdfPCell();
PdfPCell cell3 = new PdfPCell();
PdfPCell cell4 = new PdfPCell();
PdfPCell cell5 = new PdfPCell();
PdfPCell cell6 = new PdfPCell();
PdfPCell cell7 = new PdfPCell();
// 2. Inside that table, make each cell with specific height:
cell1.FixedHeight=50;
cell2.FixedHeight = 50;
cell3.FixedHeight = 50;
cell4.FixedHeight = 50;
cell5.FixedHeight = 50;
cell6.FixedHeight = 50;
cell7.FixedHeight = 50;
// 3. Each cell has the same background image
string path = string.Concat(this.openFileDialog_pic.FileName);
string imageFilePath = string.Concat(Environment.GetEnvironmentVariable("."), path);
iTextSharp.text.Image IMG = iTextSharp.text.Image.GetInstance(imageFilePath);
ImageEvent imgEvent = new ImageEvent(iTextSharp.text.Image.GetInstance(IMG));
cell1.CellEvent=imgEvent;
cell2.CellEvent = imgEvent;
cell3.CellEvent = imgEvent;
cell4.CellEvent = imgEvent;
cell5.CellEvent = imgEvent;
cell6.CellEvent = imgEvent;
cell7.CellEvent = imgEvent;
// 4. Add text in front of the image at specific position
cell1.CellEvent= new PositionEvent(new Phrase("Top left"), "TOP_LEFT");
cell2.CellEvent=new PositionEvent(new Phrase("Top right"), "TOP_RIGHT");
cell3.CellEvent=new PositionEvent(new Phrase("Bottom left"), "BOTTOM_LEFT");
cell4.CellEvent=new PositionEvent(new Phrase("Bottom right"), "BOTTOM_RIGHT");
cell5.CellEvent = new PositionEvent(new Phrase("Center Top"), "CENTER_TOP");
cell6.CellEvent = new PositionEvent(new Phrase("Center Bottom"), "CENTER_BOTTOM");
cell7.CellEvent = new PositionEvent(new Phrase("Center Middle"), "CENTER_MIDDLE");
// Wrap it all up!
table.AddCell(cell1);
table.AddCell(cell2);
table.AddCell(cell3);
table.AddCell(cell4);
table.AddCell(cell5);
table.AddCell(cell6);
table.AddCell(cell7);
document.Add(table);
document.Close();
}
问题的标题具有误导性。我看到你使用了名为'CellBackgroundEvent'的'PdfPCellEvent'。我假设你将这个图像作为背景添加到这个类中。你没有分享这个代码,所以我认为这是行得通的。您共享似乎创建嵌套表格的'GetCard()'方法,但您不会告诉我们它有什么问题。 –
问题是我无法控制从'GetCard()'方法得到的文本的位置..所以我尝试填充,因为你看到..我把例如文本的顶部或中间的单元格..但填充不准确 – Lashuel
这仍然不准确。除了你,没有人知道你的期望。如果你问别人来帮助你,请理解我们无法看到你的想法。澄清。画一张图。说明! –