I am using PDFBox wit hthe following code:
doc = new PDDocument();
page = new PDPage();
doc.addPage(page);
PDFont font = PDType1Font.COURIER;
pdftitle = new PDPageContentStream(doc, page);
pdftitle.beginText();
pdftitle.setFont( font, 12 );
pdftitle.moveTextPositionByAmount( 40, 740 );
pdftitle.drawString("Here I insert a lot of text");
pdftitle.endText();
pdftitle.close();
Does anyone know how I can wrap the text so that it automatically goes to another line? Thanks a lot!
This worked for me. A combination of WordUtils and split
PdfBox and Boxable both auto wraps the the part of text longer than the cell width, so that means if cell width = 80 sentence width = 100 the remaining portion of the text of width 20 will start from next line (NOTE : I have mentioned width(actual space consumed by the sentence) and not length(no. of characters))
If sentence width = 60, text of width 20 will be required to fill the cell's width, and any text after that will go to the next line Solution : fill this width 20 with spaces
cell's Unfilled Space = cellWidth - sentenceWidth, numberOfSpaces = cell's Unfilled Space / width of a single Space
I found a solution for the linebreak problem in pdfBOX
In general, you need three steps to wrap your text:
1) split each word in string that has to be wrapped and put them into an array of string, e.g. String [] parts
2) create an array of stringbuffer with (textlength/(number of characters in a line)), e.g. 280/70=5 >> we need 5 linebreaks!
3) put the parts into the stringbuffer[i], until the limit of maximum number of characters in a line is allowed,
4) loop until stringbuffer.length < linebreaks
the method splitString is the method which does it. The method writeText just draws the wrapped text to the pdf
Here is an example
I don´t think it is possible to wrap text automatically. But you can wrap your text yourself. See How to Insert a Linefeed with PDFBox drawString and How can I create fix-width paragraph with PDFbox