vandeseer/easytable

Is possible set and image at the left and the table at the right of the same line

Closed this issue · 2 comments

I have an image position on the left side and i need a table on the right side of an A4 landing, is possible braw the the table at the same line of that image?

Hi @NtsCiccio,

yep, that should be possible. You can specify the coordinates where the table should start to be drawn. E.g.:

package org.vandeseer;

import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.vandeseer.easytable.TableDrawer;
import org.vandeseer.easytable.structure.Row;
import org.vandeseer.easytable.structure.Table;
import org.vandeseer.easytable.structure.cell.TextCell;

import java.io.IOException;

public class MinimumWorkingExample {

    public static void main(String[] args) throws IOException {

        try (PDDocument document = new PDDocument()) {
            final PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);

            try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
                final byte[] bytes = IOUtils.toByteArray(MinimumWorkingExample.class.getClassLoader().getResourceAsStream("glider.png"));
                final PDImageXObject img = PDImageXObject.createFromByteArray(document, bytes, "test1");
                contentStream.drawImage(img, 20, page.getMediaBox().getUpperRightY() - 70f, 50, 50);

                // Build the table
                Table myTable = Table.builder()
                        .addColumnsOfWidth(200, 200)
                        .padding(2)
                        .borderWidth(1)
                        .addRow(Row.builder()
                                .add(TextCell.builder().text("One One").build())
                                .add(TextCell.builder().text("One Two").build())
                                .build())
                        .addRow(Row.builder()
                                .padding(10)
                                .add(TextCell.builder().text("Two One").build())
                                .add(TextCell.builder().text("Two Two").build())
                                .build())
                        .build();

                // Set up the drawer
                TableDrawer tableDrawer = TableDrawer.builder()
                        .contentStream(contentStream)
                        .startX(120f)
                        .startY(page.getMediaBox().getUpperRightY() - 20f)
                        .table(myTable)
                        .build();

                // And go for it!
                tableDrawer.draw();
            }

            document.save("example.pdf");
        }
    }

}

That renders to:

image

Obviously enough you need to adapt it a bit but I leave this to the interested reader 😉

HTH
Stefan

thank you very much this is what i need. i really thank you.