带有文本的单元格背景图像itextsharp

时间:2015-07-01 18:45:32

标签: c# file pdf itextsharp cell

先谢谢,我知道你很忙..所以我根据你给我的代码编辑了这个...... 首先,我想让你看看我在尝试编码时得到的东西..

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

1 个答案:

答案 0 :(得分:2)

在另外的评论中,您阐明了您的要求:

  1. 我想创建一个包含一个表的文档。在该表内,单元格
  2. 我想制作具有特定高度的每个单元格
  3. 每个单元格具有相同的背景图像
  4. 我想在图像前面放置一个文本,位于单元格中我想要的位置。例如:单元格的左上角,单元格的右下角
  5. 换句话说:你想要这样的东西: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
        }
    }
    

    你还有一个问题需要一个额外的例子(很难在评论框中解释它)。我将该示例命名为PositionContentInCell2

    您询问是否可以传递POSITIONx值,而不是使用y枚举。你可以这样做,但你可能不会总是知道单元格的宽度和高度,所以为什么不定义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()多个变量。