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

C# 带有文本的单元格背景图像,c#,file,pdf,itextsharp,cell,C#,File,Pdf,Itextsharp,Cell,提前谢谢,我知道你很忙。。所以我根据你给我的代码编辑了这个。。。 首先,我想让你看看我在尝试你的代码时得到了什么 我用作背景的图片是: 如你所见,我遇到了几个问题: 1-图像不是单元格的背景,我希望它被拉伸 2-我试着把文本放在不同的位置,但失败了 3-我还有一个丢失的手机没有显示出来 我使用的代码是: 1-ImageEvent类: class ImageEvent : IPdfPCellEvent { protected Image img; public ImageEvent(Imag

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

我用作背景的图片是:

如你所见,我遇到了几个问题:

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

在附加注释中,您阐明了您的要求:

  • 我想创建一个包含一个表的文档。在那张桌子里面,有很多细胞
  • 我想让每个单元格都有特定的高度
  • 每个单元格都有相同的背景图像
  • 我想把一个文本放在图像前面单元格内我想要的位置。例如:单元格的左上角、右下角
  • 换句话说:你想要这样的东西:

    实现这一点的方法不止一种。我不理解您在问题中使用的代码示例。它使用嵌套表,我不明白为什么需要嵌套表

    在本例中,我使用了一种方法,允许您真正微调文本的确切位置。我创建了一个
    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

    更新:

    在评论中,您又问了几个问题。例如:如何拉伸图像:

    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
            }
        }
    }
    

    您能够回答这些问题中的大多数,例如,根据我的提示使用
    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
    枚举,而是询问是否可以传递
    x
    y
    值。您可以这样做,但您可能并不总是知道单元格的宽度和高度,因此为什么不定义百分比,例如
    wPct
    hPct
    ,以及对齐方式:

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

    当然,如果您更喜欢传递
    x
    y
    (毕竟:您知道高度,因为您定义的是固定高度,所以代码可以变得更简单:您不需要使用
    position.getWidth
    position.getHeight()对变量进行多重运算)

    问题的标题有误导性。我看到您使用了名为
    CellBackgroundEvent
    PdfPCellEvent
    。我假设您正在将图像作为背景添加到此类中。您没有共享该代码,因此我假设它正常工作。您共享了
    GetCard()
    方法,该方法似乎创建了嵌套表,但您没有告诉我们它有什么问题。问题是我无法控制从“GetCard()获取的文本的位置'方法..所以我尝试了填充,正如你所看到的..我把文本放在单元格的顶部或中间..但填充不准确这仍然不准确.除了你之外,没有人知道你期望的是什么.如果你请其他人帮助你,请理解我们无法了解你的想法.澄清.画一幅画.解释!谢谢你的回复。。。很抱歉让人误解…所以我会尽可能简单…忘记我写的所有编码…1-我想创建一个包含一个表的文档…在该表中,单元格…2-我想使每个单元格具有特定高度3-每个单元格具有相同的背景图像。4-我想在图像前面放置一个文本在我想在牢房内的位置。例如:牢房左上角,牢房右下角……等等……我做了1、2和3……但我做不到4……我希望我最后的评论是清楚的……请告诉我需要帮助@BrunoLowagiethank非常感谢……这正是我想要的……再次抱歉把你和我的要求混淆了这些都不是一次有太多的问题。把它们分成不同的问题。你想让我把这个作为一个新问题发布吗???。对不起,我没有得到你想要的mean@lasheul我认为这样最好。例如:您现在使用的是
    ScaleToFit
    ,但您真正想要的是
    ScaleAbsolute
    。我无法知道这一点,因为您是quest离子不清楚。如果使用
    ScaleAbsolute
    ,将图像定位为单元格背景会更容易。您可以使用
    img.setAbsolutePosition(position.getLeft(),position.getBottom());
    因为图像的尺寸与单元格的尺寸完全相同,所以无需将其居中。您的附加问题中没有任何问题是无法用常识解决的。最后一个问题..是否可以将定位转换为更灵活的方式..例如,不使用开关盒和类似的短语“左下角”。。我可以用数字吗?比如(x=0,y=0)表示左上角,如果我用(x=width,y=height),它将是右下角,中间中间的意思是(x=width/2,y=height/2)…等等