-3

Below is a code I've written to add table to 8 page in PDF using itext7 .

Unfortunately, it is adding only to first page instead of 8th page.

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
Table table = new Table(new float[]{1, 15});
table.setWidthPercent(80);
table.addHeaderCell("#");
table.addHeaderCell("description");
PageSize ps = pdfDoc.getDefaultPageSize();
PdfPage p =pdfDoc.getPage(8);
Document doc = new Document(p.getDocument());
table.setFixedPosition(doc.getLeftMargin(), doc.getBottomMargin(), ps.getWidth() - doc.getLeftMargin() - doc.getRightMargin());
IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
LayoutResult tableLayoutResult = tableRenderer.layout(new LayoutContext(new LayoutArea(0, new Rectangle(ps.getWidth(), 1000))));
float totalHeight = tableLayoutResult.getOccupiedArea().getBBox().getHeight();
doc.add(table);
doc.close();

I am unable to identify where the problem is.

What am I doing wrong and how can I effectively add table to particular pages?

Println
  • 438
  • 1
  • 6
  • 14
  • Do you want to modify existing PdfDocument? Because `Document doc = new Document(p.getDocument());` creates brand new PdfDocument. Read this question [How to update a PDF without creating a new PDF?](https://stackoverflow.com/questions/16081831/how-to-update-a-pdf-without-creating-a-new-pdf) – Sergey May 31 '17 at 09:34
  • 1
    So you **know** that the table will have to be put on page 8 of an existing PDF? Using PdfStamper might be a good idea in that case – Jan May 31 '17 at 09:37
  • Or this [iText - add content to existing PDF file](https://stackoverflow.com/questions/3335126/itext-add-content-to-existing-pdf-file). – Sergey May 31 '17 at 09:40
  • i am using itext 7, unable to find the pdfstamper @Jan – Println May 31 '17 at 10:05
  • PdfStamper does not exist in iText7. You'll want to create a PdfCanvas on page 8 (make sure it exists first), and then use the PdfCanvas to create a Canvas (in the layout package). Add the table to this canvas. If I can find some free time this week, I might whip up an example. – Samuel Huylebroeck May 31 '17 at 12:09
  • @SamuelHuylebroeck thanks for the suggestion. Example would be much helpful. – Println Jun 01 '17 at 04:57

1 Answers1

3

When working with the Documentobject in iText7, there are two ways to add a table to specific page:

  1. Setting a fixed position for the table via Table#setFixedPosition. The method contains two overloads that allow you to specify a pagenumber. The upside is that you have absolute control over the position of the table. The downside is that you need to specify absolute position and width, so the table isn't really placed dynamically anymore.

  2. Creating a layout Canvas on the specified page, and add the table to the canvas.

A small example showcasing both methods:

public void createPdf(String dest) throws IOException, FileNotFoundException{
    PdfWriter writer = new PdfWriter(dest);
    PdfDocument pdfDoc = new PdfDocument(writer);

    //Turn off immediate flush to be certain the page you want to insert the table on hasn't been flushed yet
    Document doc = new Document(pdfDoc, pdfDoc.getDefaultPageSize(),false);

    //Add a some empty pages
    for (int i = 1; i < nrOfPages ; i++) {
        doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    }

    //Method 1: Fixed Pos table
    Table fixedPagefixedPosTable = createTable();
    float margin = 10;
    PageSize defaultSize = pdfDoc.getDefaultPageSize();
    float ypos = defaultSize.getHeight()/2;
    fixedPagefixedPosTable.setFixedPosition(tablePage,margin,ypos,defaultSize.getWidth()-2*margin);
    doc.add(fixedPagefixedPosTable);

    //Method 2: Using a canvas
    Table canvasTable = createTable();
    PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc.getPage(tablePage+1));
    margin = 10;
    Rectangle canvasArea = new Rectangle(margin,margin,defaultSize.getWidth()-margin,defaultSize.getHeight()-margin);
    Canvas canvas = new Canvas(pdfCanvas,pdfDoc,canvasArea);
    canvas.add(canvasTable);
    canvas.close();

    doc.close();
}

private Table createTable(){
    //Create table
    float[] colWidths = {20f,20f,20f};
    Table table = new Table(colWidths);
    for (int j = 0; j < colWidths.length*nrOfRows; j++) {
        Cell c = new Cell();
        if(j/colWidths.length == 0){
            //Header
            c.add("Header  " +j);
        }else{
            //Data
            c.add("Data " + j/colWidths.length + ","+ j%colWidths.length);
        }
        table.addCell(c);
    }
    return table;
}
Samuel Huylebroeck
  • 1,639
  • 11
  • 15