0

I am creating a table in pdf using PDFBox library in java. I am able to draw a table and fill data in a table but in a cell there is long data available which I need to do word wrap. In my project no other library are allowed we have to do it by using PDFBox only.

PDFBox version is 2.0.22

here is the code that I am using,

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class Test2 {

private static final String[] PARKING_SPACE_REPORT_HEADERS = { "PARKING SPACE NAME", "PARKING SPACE ADDRESS",
        "APPROVED/REJECTED ON" };

public static void main(String[] args) throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();

    doc.addPage(page);

    PDPageContentStream contentStream = new PDPageContentStream(doc, page);

    String[][] content = { { "Bini k", "TEST1", "01-Sep-2020 01:57 PM" }, { "c",
            "Job Description: Lorem ipsum dolor sit knajdsn kabsjflsnijasd kjdoisaosd sisa saihsihsahc sassah",
            "2" }, { "e", "f", "3" }, { "g", "h", "4" }, { "i", "j", "5" } };

    drawTable(page, contentStream, 700, 100, content);
    contentStream.close();
    doc.save("C:\\software\\hello1030.pdf");
}

public static void drawTable(PDPage page, PDPageContentStream contentStream, float y, float margin,
        String[][] content) throws IOException {
    final int rows = content.length + 1;
    final int cols = content[0].length;
    final float rowHeight = 20f;
    final float tableWidth = page.getMediaBox().getWidth() - margin - margin;
    final float tableHeight = rowHeight * rows;
    final float colWidth = tableWidth / (float) cols;
    final float cellMargin = 5f;

    // draw the rows
    float nexty = y;
    for (int i = 0; i <= rows; i++) {
        contentStream.moveTo(margin, nexty);
        contentStream.lineTo(margin + tableWidth, nexty);
        contentStream.stroke();
        nexty -= rowHeight;
    }

    // draw the columns
    float nextx = margin;
    for (int i = 0; i <= cols; i++) {
        contentStream.moveTo(nextx, y);
        contentStream.lineTo(nextx, y - tableHeight);
        contentStream.stroke();
        nextx += colWidth;
    }

    // now add the text
    contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
    float textx = margin + cellMargin;
    float texty = y - 15;
    for (int i = 0; i < content.length; i++) {
        for (int j = 0; j < content[i].length; j++) {
            String text = content[i][j];
            contentStream.beginText();
            contentStream.newLineAtOffset(textx, texty);
            if (text.length() > 20) {
                printMultipleLines(text, contentStream);
                textx += colWidth;
                contentStream.endText();
                continue;
            }
            contentStream.showText(text);
            contentStream.endText();
            textx += colWidth;
        }
        texty -= rowHeight;
        textx = margin + cellMargin;
    }
}

private static void printMultipleLines(String line, PDPageContentStream contentStream) {
    try {
        // Print line as 2 lines
        contentStream.showText(line.substring(0, 90));
        contentStream.newLine();
        contentStream.showText(line.substring(90, line.length()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}   }

Here is my pdf file enter image description here

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Karthikeyan Raju
  • 109
  • 2
  • 11
  • 1
    Read [this question](https://stackoverflow.com/a/19683618/1729265) to understand how to break long lines in general. Then apply that code to the content of each cell individually and draw borders accordingly. Alternatively take an open source library and copy its code into your project - that way you don't have any other library... (of course check the license of the library beforehand and only use that library if the license allows this use...) – mkl Mar 09 '21 at 09:09
  • 2
    The PDFBox source code has a class `PlainTextFormatter.java`, maybe you can reuse / adjust that one. – Tilman Hausherr Mar 09 '21 at 09:14

0 Answers0