2015-07-01 31 views
1

谢谢你,我知道你很忙..所以我编辑此基础上,你给我的代码... 首先我想让你看看我得到了什么,当我试图您代码..单元格背景图像iTextSharp的提前

enter image description here

,我作为背景的图片:

enter image description here

你看我有几个问题:

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(); 
    } 
+0

问题的标题具有误导性。我看到你使用了名为'CellBackgroundEvent'的'PdfPCellEvent'。我假设你将这个图像作为背景添加到这个类中。你没有分享这个代码,所以我认为这是行得通的。您共享似乎创建嵌套表格的'GetCard()'方法,但您不会告诉我们它有什么问题。 –

+0

问题是我无法控制从'GetCard()'方法得到的文本的位置..所以我尝试填充,因为你看到..我把例如文本的顶部或中间的单元格..但填充不准确 – Lashuel

+0

这仍然不准确。除了你,没有人知道你的期望。如果你问别人来帮助你,请理解我们无法看到你的想法。澄清。画一张图。说明! –

回答

2

在附加评论,你明确你的要求:

  1. 我想创建一个包含一个表的文档。该表内,细胞
  2. 我想使每个单元具有特定的高度
  3. 每个单元具有相同的背景图像
  4. 我想提出一个文本前面的图像中我想在细胞内的位置。例如:左上细胞,细胞

换句话说右下:你想是这样的:position_content_in_cell.pdf

enter image description here

还有就是要做到这一点的方法不止一种。我不明白你在你的问题中使用的代码示例。它使用嵌套表,我不明白你为什么需要嵌套表。

PositionContentInCell例子中,我用了一个方法,可以让你真正微调文字的准确位置。我创建了一个ImageEvent于比例和中心的形象:

class ImageEvent implements PdfPCellEvent { 
    protected Image img; 
    public ImageEvent(Image img) { 
     this.img = img; 
    } 
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
     img.scaleToFit(position.getWidth(), position.getHeight()); 
     img.setAbsolutePosition(position.getLeft() + (position.getWidth() - img.getScaledWidth())/2, 
       position.getBottom() + (position.getHeight() - img.getScaledHeight())/2); 
     PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS]; 
     try { 
      canvas.addImage(img); 
     } catch (DocumentException ex) { 
      // do nothing 
     } 
    } 
} 

我创建了一个PositionEvent到文本增加该小区内:

class PositionEvent implements PdfPCellEvent { 
    protected Phrase content; 
    protected POSITION pos; 

    public PositionEvent(Phrase content, POSITION pos) { 
     this.content = content; 
     this.pos = pos; 
    } 

    public void 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.getLeading()); 
       alignment = Element.ALIGN_LEFT; 
       break; 
      case TOP_RIGHT: 
       x = position.getRight(3); 
       y = position.getTop(content.getLeading()); 
       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; 
     } 
     ColumnText.showTextAligned(canvas, alignment, content, x, y, 0); 
    } 
} 

这是我如何使用这些事件:

public void createPdf(String dest) throws IOException, DocumentException { 
    // 1. Create a Document which contains a table: 
    Document document = new Document(); 
    PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    PdfPTable table = new PdfPTable(2); 
    PdfPCell cell1 = new PdfPCell(); 
    PdfPCell cell2 = new PdfPCell(); 
    PdfPCell cell3 = new PdfPCell(); 
    PdfPCell cell4 = new PdfPCell(); 
    // 2. Inside that table, make each cell with specific height: 
    cell1.setFixedHeight(50); 
    cell2.setFixedHeight(50); 
    cell3.setFixedHeight(50); 
    cell4.setFixedHeight(50); 
    // 3. Each cell has the same background image 
    ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG)); 
    cell1.setCellEvent(imgEvent); 
    cell2.setCellEvent(imgEvent); 
    cell3.setCellEvent(imgEvent); 
    cell4.setCellEvent(imgEvent); 
    // 4. Add text in front of the image at specific position 
    cell1.setCellEvent(new PositionEvent(new Phrase("Top left"), POSITION.TOP_LEFT)); 
    cell2.setCellEvent(new PositionEvent(new Phrase("Top right"), POSITION.TOP_RIGHT)); 
    cell3.setCellEvent(new PositionEvent(new Phrase("Bottom left"), POSITION.BOTTOM_LEFT)); 
    cell4.setCellEvent(new PositionEvent(new Phrase("Bottom right"), POSITION.BOTTOM_RIGHT)); 
    // Wrap it all up! 
    table.addCell(cell1); 
    table.addCell(cell2); 
    table.addCell(cell3); 
    table.addCell(cell4); 
    document.add(table); 
    document.close(); 
} 

通常情况下,我会以更有效的方式编写代码,但我以某种方式排列代码行,以便从字面上反映出您的要求1,2,3和4。

更新:

在评论,你问了几个其他问题。例如:如何拉伸图像:

enter image description here

您能够根据我的暗示使用ScaleAbsolute回答大部分问题,比如:

public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
    img.scaleAbsolute(position.getWidth(), position.getHeight()); 
    img.setAbsolutePosition(position.getLeft(), position.getBottom()); 
    PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS]; 
    try { 
     canvas.addImage(img); 
    } catch (DocumentException ex) { 
     // do nothing 
    } 
} 

你有一个问题这需要额外的例子(很难在评论框中解释它)。我命名,而不是使用POSITION列举的那个例子PositionContentInCell2

,你问是否有可能通过xy值。你可以这样做,但你可能不会总是知道的宽度和单元格的高度,那么为什么不实施取向定义的百分比,如wPcthPct,沿:

class PositionEvent implements PdfPCellEvent { 
    protected Phrase content; 
    protected float wPct; 
    protected float hPct; 
    protected int alignment; 

    public PositionEvent(Phrase content, float wPct, float hPct, int alignment) { 
     this.content = content; 
     this.wPct = wPct; 
     this.hPct = hPct; 
     this.alignment = alignment; 
    } 

    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
     PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; 
     float x = position.getLeft() + wPct * position.getWidth(); 
     float y = position.getBottom() + hPct * (position.getHeight() - content.getLeading()); 
     ColumnText.showTextAligned(canvas, alignment, content, x, y, 0); 
    } 
} 

现在你可以添加这些事件是这样的:

cell1.setCellEvent(new PositionEvent(new Phrase(14, "Top left"), 0, 1, Element.ALIGN_LEFT)); 
cell2.setCellEvent(new PositionEvent(new Phrase(14, "Top right"), 1, 1, Element.ALIGN_RIGHT)); 
cell3.setCellEvent(new PositionEvent(new Phrase(14, "Top center"), 0.5f, 1, Element.ALIGN_CENTER)); 
cell4.setCellEvent(new PositionEvent(new Phrase(14, "Bottom center"), 0.5f, 0, Element.ALIGN_CENTER)); 
cell5.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER)); 
cell6.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER)); 
cell7.setCellEvent(new PositionEvent(new Phrase(14, "Bottom left"), 0, 0, Element.ALIGN_LEFT)); 
cell8.setCellEvent(new PositionEvent(new Phrase(14, "Bottom right"), 1, 0, Element.ALIGN_RIGHT)); 

当然,如果你喜欢传球xy(毕竟:你知道的高度,因为你正在定义一个固定的高度,可以把代码变得更加简单:你不” t用position.getWidthposition.getHeight()来表示变量的倍数

+0

非常感谢你..那正是我想要的......并且再次为我的要求而感到困惑 – Lashuel

+0

我编辑了我的帖子..请帮助我.. @BrunoLowagie – Lashuel

+0

这些问题同时存在太多问题。将它们分成不同的问题。 –