简体   繁体   English

如何使用iText库在PDF的表格上应用背景图像?

[英]How to Apply background image on Table in PDF using iText library?

I am creating a PDF using the iText Library and not been able to set the background image for Table in PDF. 我正在使用iText库创建PDF,但无法为PDF中的表格设置背景图像。

like below image 像下面的图像 在此处输入图片说明

Edit 编辑

Currently i am using this to set Background , its setting image to absolute position , i want to set it relative to table 目前我正在使用它来将Background设置为背景,将其设置为绝对位置,我想将其相对于表进行设置

class CellBackgroundPic  implements PdfPTableEvent {

Activity mActivity;
public CellBackgroundPic (Activity Activity){

    this.mActivity=Activity;

}
Image bgImage;


public void tableLayout(PdfPTable table, float[][] widths, float[] heights,
        int headerRows, int rowStart, PdfContentByte[] canvases){
    PdfContentByte pdfContentByte = canvases[PdfPTable.BACKGROUNDCANVAS];
    Drawable   myImage  = mActivity.getResources().getDrawable(R.drawable.table_bg);

      Bitmap bitmap = ((BitmapDrawable) myImage).getBitmap();
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
       byte[] bitmapdata = stream.toByteArray();
       try {
        bgImage = Image.getInstance(bitmapdata);
        bgImage.setAbsolutePosition(330f, 642f);
        pdfContentByte.addImage(bgImage);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

There are different approaches possible. 可能有不同的方法。

One would be to use a pattern color created with an image and use that pattern color as background color. 一种是使用随图像创建的图案颜色,然后将该图案颜色用作背景颜色。 However, that doesn't seem to be a match with what you need. 但是,这似乎与您的需求不符。

Based on your example I would suggest using a table event as demonstrated here: http://itextpdf.com/examples/iia.php?id=93 根据您的示例,我建议使用一个表事件,如下所示: http : //itextpdf.com/examples/iia.php?id=93

In this example, the background color of the rows is drawn in the tableLayout() method. 在此示例中,在tableLayout()方法中绘制了行的背景色。 You need to adapt this method and instead of drawing colored rectangles, you need to add an image at the appropriate coordinates. 您需要调整此方法,而不是绘制彩色矩形,而需要在适当的坐标处添加图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM