Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在表上方添加图像-Itext_Java_Android_Position_Itext_Itext7 - Fatal编程技术网

Java 在表上方添加图像-Itext

Java 在表上方添加图像-Itext,java,android,position,itext,itext7,Java,Android,Position,Itext,Itext7,如何使用Itext在表上添加图像 我正在使用5.5.10版 实现'com.itextpdf:itextg:5.5.10' 编辑:图像不能位于行/列内,必须独立才能填充屏幕上的任何位置 我试图在表的列上添加图像,但结果如下: 它始终位于列的行下方。 要添加我正在添加的图像,请执行以下操作: public void addImg (int dwb, float x, float y, float desc) { try{ Bitmap bitmap = dwbToBitma

如何使用Itext在表上添加图像

我正在使用5.5.10版

实现'com.itextpdf:itextg:5.5.10'

编辑:图像不能位于行/列内,必须独立才能填充屏幕上的任何位置

我试图在表的列上添加图像,但结果如下:

它始终位于列的行下方。 要添加我正在添加的图像,请执行以下操作:

public void addImg (int dwb, float x, float y, float desc) {
    try{
        Bitmap bitmap = dwbToBitmap(context, dwb);
        ByteArrayOutputStream stream3 = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream3);

        Image image = Image.getInstance(stream3.toByteArray());
        stream3.close();
        image.scaleToFit(sizeImgFit, sizeImgFit);
        image.setAbsolutePosition(35.6f + 10f + x, height-y-sizeImg-(height-desc));

        document.add(image);

    }catch (Exception e){
        log("addImg", e);
    }
}
我已经尝试过改变顺序,创建第一个表,然后添加图像,或者反之亦然,但它不起作用

有人知道如何将图像放在最上面的位置Z吗

我创建如下表:

public void createTable(ArrayList<String> header, ArrayList<String[]> clients){
    float height = 569/header.size();
    sizeImg = height;
    sizeImgFit = sizeImg - 2;
    PdfPTable pdfPTable = new PdfPTable(header.size());
    pdfPTable.setWidthPercentage(100);

    PdfPCell pdfPCell;

    int indexC = 0;

    while(indexC < header.size()){
        pdfPCell = new PdfPCell(new Phrase(header.get(indexC++), fHeaderText));
        pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfPCell.setBackgroundColor(BaseColor.GRAY);
        pdfPTable.addCell(pdfPCell);

    }

    int i = 0;
    for(String[] row : clients){
        int p = 0;
        for(String linha : row){
            pdfPCell = new PdfPCell(new Phrase(linha, fText));
            pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdfPCell.setVerticalAlignment(Element.ALIGN_CENTER);
            pdfPCell.setFixedHeight(height);

            pdfPTable.addCell(pdfPCell);
            log("linha - coluna", i + " - " + p);
            p++;
        }
        i++;
    }

    //paragraph.add(pdfPTable);

    try {
        document.add(pdfPTable);

    }catch (Exception e){
        log("paragraph", e);
    }
}
要创建PDF,请执行以下操作:

        //Creating the object
        TemplatePDF templatePDF = new TemplatePDF(ficha_pre.this);
        templatePDF.openDocument();
        templatePDF.addMetaData("Relatório", "Situs", "Woton Sampaio");
        templatePDF.addTitles("Relatório", "","Data: " + getDate());

        //Creating the table
        ArrayList<String> header = new ArrayList<>();
        for(int i = 0; i < 55; i++){
            header.add(forString(i));
        }

        ArrayList<pdfItens> itens = arrayItens();
        ArrayList<String[]> files = array();

        templatePDF.createHeaderFicha(itens);
        templatePDF.createTable(header, files);

        //Adding image
        templatePDF.addImg(R.drawable.ic_a, 0, 20, 566);
//创建对象
TemplatePDF TemplatePDF=新的TemplatePDF(ficha_pre.this);
templatePDF.openDocument();
templatePDF.addMetaData(“Relatório”、“Situs”、“Woton Sampaio”);
templatePDF.addTitles(“Relatório”,“Data:”+getDate());
//创建表
ArrayList标头=新的ArrayList();
对于(int i=0;i<55;i++){
标题。添加(对于字符串(i));
}
ArrayList itens=ArrayItems();
ArrayList文件=数组();
模板PDF.createHeaderFicha(itens);
templatePDF.createTable(头文件);
//添加图像
模板pdf.addImg(R.drawable.ic_a,0,20566);

原因是添加到
文档
图像
被添加到常规文本和表格下方的虚拟层中。显然,iText开发人员假定默认情况下,文本和表元素是在图像前面绘制的

但是您可以显式地将
图像
添加到另一个虚拟层,该虚拟层依次位于文本和表格的虚拟层之上,在
addImg
中,您只需替换

document.add(图像);

您在
addImg
中的
图像
已设置其
绝对位置。这实际上对于要添加到
DirectContent
的图像是必需的,因为
DirectContent
不知道当前插入位置或页面尺寸


另外,还有一个
DirectContentUnder
用于通过
文档添加的
Images
层下的内容,不清楚如何应用您发布的代码。@mkl我使用表格部分的代码创建表格,并使用底部的代码将图片添加到pdf中,你哪里不明白?所以我添加了更多的信息,我真的需要在这个问题上的帮助
但是您显示的
addImg
方法代码有四个参数。因此,您要么调用了错误的方法,要么显示了错误的方法代码。@mkl完成,我在这里复制了错误的方法代码
        //Creating the object
        TemplatePDF templatePDF = new TemplatePDF(ficha_pre.this);
        templatePDF.openDocument();
        templatePDF.addMetaData("Relatório", "Situs", "Woton Sampaio");
        templatePDF.addTitles("Relatório", "","Data: " + getDate());

        //Creating the table
        ArrayList<String> header = new ArrayList<>();
        for(int i = 0; i < 55; i++){
            header.add(forString(i));
        }

        ArrayList<pdfItens> itens = arrayItens();
        ArrayList<String[]> files = array();

        templatePDF.createHeaderFicha(itens);
        templatePDF.createTable(header, files);

        //Adding image
        templatePDF.addImg(R.drawable.ic_a, 0, 20, 566);
pdfWriter.getDirectContent().addImage(image);