Woton Sampaio
Woton Sampaio

Reputation: 456

Add image above table - Itext

How to add an image over a table with Itext?

I'm using the version 5.5.10

implementation 'com.itextpdf:itextg:5.5.10'

Edit: The image can not be inside the row / column, it must be independent to populate any position on the screen

I'm trying to add an image over the columns of a table, but the result is this:

enter image description here

It always lies below the rows of the column. To add the image I'm doing so:

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

I have already tried to change the order, create the first table and then add the images or vise versa, but it does not work.

Does anyone know how to put the images in position Z above all?

I create the table like this:

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

These methods mentioned above are in a class:

public class TemplatePDF {
    private Context context;
    private File pdfFile;
    private Document document;
    public PdfWriter pdfWriter;
    private Paragraph paragraph;
    private Rotate event;
    private Font fTitle = new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD);
    private Font fSubTitle = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private Font fHeaderText = new Font(Font.FontFamily.TIMES_ROMAN, 3, Font.NORMAL, BaseColor.WHITE);
    private Font fText = new Font(Font.FontFamily.TIMES_ROMAN, 3);
    private Font fHText = new Font(Font.FontFamily.TIMES_ROMAN, 8);
    private Font fHighText = new Font(Font.FontFamily.TIMES_ROMAN, 15, Font.BOLD, BaseColor.RED);
    private float width = PageSize.A4.getWidth();
    private float height = PageSize.A4.getHeight();
    public float sizeImg;
    public float sizeImgFit;

    public TemplatePDF(Context context){
        this.context = context;
    }

    public void openDocument(){
        createFile();

        try{
            document = new Document(PageSize.A4);
            pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
            event = new Rotate();
            document.open();

        }catch (Exception e){
            Log.e("erro", e.toString());
        }
    }

    private void createFile(){
        File folder = new File(Environment.getExternalStorageDirectory().toString(), "PDF");

        if(!folder.exists()){
            folder.mkdirs();
        }

        pdfFile = new File(folder,  key() + ".pdf");
    }

    public void closeDocument(){
        document.close();
    }

    ...

}

To create PDF, I do so:

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

Upvotes: 2

Views: 2221

Answers (1)

mkl
mkl

Reputation: 95918

The cause for this is that an Image added to the Document is added in a virtual layer underneath that of regular text and tables. Apparently iText developers assumed that text and table elements by default are to be drawn in front of images.

But you can explicitly add the Image to a different virtual layer which is in turn above that of text and tables, in addImg you merely have to replace

document.add(image);

by

pdfWriter.getDirectContent().addImage(image);

Your image in addImg has its AbsolutePosition set. This actually is necessary for images you want to add to the DirectContent because the DirectContent has no idea about current insertion positions or page dimensions.

As an aside, there also is a DirectContentUnder for stuff that shall go even below the layer of Images added via the Document.

Upvotes: 3

Related Questions