【发布时间】:2015-07-01 18:45:32
【问题描述】:
提前谢谢,我知道你很忙..所以我根据你给我的代码编辑了这个...... 首先我想让你看看我在尝试你的代码时得到了什么..
我用作背景的图片是:
如你所见,我遇到了几个问题:
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。我假设您正在将图像添加为此类中的背景。您没有共享该代码,因此我认为这是可行的。你分享了@987654328@ 方法,似乎创建了一个嵌套表,但你没有告诉我们它有什么问题。 -
问题是我无法控制从 'GetCard()' 方法获得的文本的位置 .. 所以我尝试了填充,如您所见 .. 我将文本放在顶部或单元格的中间..但填充不准确
-
那仍然不准确。除了您之外,没有人知道您的期望。如果您要求其他人帮助您,请理解我们无法调查您的想法。阐明。做一张图。解释!
-
感谢您的回复...抱歉误导...所以我会尽量简化...忘记我写的所有编码...1 - 我想创建一个包含一个表格的文档.. 在该表格内,单元格.... 2- 我想让每个单元格具有特定的高度 3- 每个单元格都具有相同的背景图像。 4-我想在单元格内我想要的位置在图像前面放置一个文本。例如:单元格的左上角,单元格的右下角..etc ........我做了 1,2 和 3 .. 但我做不到 4 ..
-
我希望我上次的评论很清楚......请我需要帮助@BrunoLowagie
标签: c# file pdf itextsharp cell