0

I have created a pdf where I have a table but if the length of the string exceeds then I want to use new line.

public static void drawtable1(PDPage page, PDPageContentStream contentStream, float y, float margin, String[][] content) throws IOException {
    int rows = content.length;
    int cols = content[0].length;
    float rowHeight = 20f;
    float tableWidth = page.getMediaBox().getWidth() - margin - margin;
    float tableHeight = rowHeight * rows;
    float colWidth = tableWidth / (float)cols;
    float cellMargin = 5f;
    PDFont font1 = PDType1Font.HELVETICA_BOLD;
    PDFont font2 = PDType1Font.HELVETICA;

    //draw the rows
    float nexty = y;
    for (int i = 0; i <= rows; i++) {
        contentStream.moveTo(margin, nexty);
        contentStream.lineTo(margin + tableWidth, nexty);
        contentStream.setStrokingColor(220,220,220);
        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.setStrokingColor(220,220,220);
        contentStream.stroke();

        //     contentStream.stroke();
        if(content[0].length == 2){
            if(i==0){
                nextx += colWidth/2;
            }if(i == 1){
                nextx += colWidth + colWidth/2;
            }
        }else{
            nextx += colWidth;
        }
    }    

    //now add the text
    contentStream.setFont(PDType1Font.HELVETICA, 10);
    float textx = margin + cellMargin;
    float texty = y - 15;
    float col = tableWidth / (float)cols;

    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.setLeading(12.5f);
            contentStream.newLineAtOffset(textx, texty);
            if (j == 0 || j == 2){
                if(font1.getStringWidth(content[i][j]) > col){
                    System.out.println(content[i][j]);   
                }
                contentStream.setFont(font1, 10);
                contentStream.showText(text);
                contentStream.setFont(font2, 10);
            }else{
                contentStream.showText(text);
            }

            contentStream.endText();

            if(content[0].length == 2){
                if(j==0){
                    textx += colWidth/2;
                }
                if(j == 1){
                    textx += colWidth + colWidth/2;
                }
            }else{
                textx += colWidth;
            }
        }

        texty -= rowHeight;
        textx = margin + cellMargin;

    }
}

I tried comparing the length of the string and column width but it is not working. I have no idea how to display half string in the next line.

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
user2243471
  • 21
  • 2
  • 9
  • I have no clear idea what you're asking. Please read https://stackoverflow.com/help/mcve and edit your code. – Tilman Hausherr Mar 10 '16 at 18:17
  • @user2243471 [This answer](http://stackoverflow.com/a/19683618/1729265) explains how to split text and draw it in separate lines as page content (given the coordinates of the top-left corner and the width of the content area). You can analogously split your cell content. (It's quite a lot of code but as PDFBox does not bring along layout'ing routines, you have to hand-code everything...) – mkl Mar 11 '16 at 09:45
  • @TilmanHausherr i am asking that i have created a table where i have 2 columns and many rows and if one of the cell contain string which has length larger than the cell size than it either overlap or it will get cut. so how to put the rest of the string in the new line. – user2243471 Mar 11 '16 at 19:29
  • @mkl thanks, but actually i tried this one. but it is too long and not working for me though. i think somewhere i must be lacking. But i will again give it a try. if you find any other trick then please inform me though :D – user2243471 Mar 11 '16 at 19:32
  • *it is too long* - too long?? That answer shows the most primitive way to split long strings into separate lines. For serious layout'ing there is much more to do! *not working for me* - perhaps you should explain your problems with it as the topic of a question here. – mkl Mar 11 '16 at 21:05

1 Answers1

0

See actually only this thing worked for me,

String text = content[i][j];
List<String> strings = new ArrayList<String>();
int index = 0;
while (index < text.length()) {
    strings.add(text.substring(index, Math.min(index + 25,text.length())));
    index += 25;
}                               

for (String line: strings){
    contentStream.showText(line);
    contentStream.newLineAtOffset(0, -leading);
} 

But here I have no idea how to increase height of the row which have many characters.

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
user2243471
  • 21
  • 2
  • 9