我已经找到了pdfBOX中的换行问题的解决方案。
一般来说,您需要以下三个步骤来换行:
1) 将要换行的字符串中的每个单词拆分并放入一个字符串数组中,例如String [] parts
2) 创建一个字符串缓冲区数组,并将其划分为(textlength /(每行字符数)),例如280/70=5 >> 我们需要5个换行符!
3) 将部分内容放入stringbuffer [i]中,直到达到每行最大字符数的限制。
4) 循环,直到stringbuffer.length < linebreaks
splitString 方法是执行此操作的方法。
writeText 方法只是将换行文本绘制到pdf中。
这是一个示例:
import java.io.IOException;
import java.util.ArrayList;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
public class pdfTest{
private ArrayList<String> arrayList;
private PDDocument document;
private PDFont font = PDType1Font.HELVETICA;
pdfTest(PDDocument document, ArrayList arrayList, PDFont font) throws COSVisitorException, IOException {
this.document = document;
this.arrayList = arrayList;
this.font = font;
writeText(document, arrayList, 30, 750, font);
}
public void writeText(PDDocument document, ArrayList arrayList, int positionX, int positionY, PDFont font) throws IOException, COSVisitorException {
PDPage page = new PDPage();
document.addPage( page );
PDPageContentStream contentStream = new PDPageContentStream(document, page);
for(int i=0;i<arrayList.size();i++) {
String text=(String) arrayList.get(i);
String [] tmpText = splitString(text);
for( int k=0;k<tmpText.length;k++) {
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(positionX, positionY);
contentStream.drawString(tmpText[k]);
contentStream.endText();
positionY=positionY-20;
}
contentStream.setLineWidth((float) 0.25);
}
contentStream.close();
document.save( "Test.pdf");
document.close();
}
public static void main(String[] args) throws COSVisitorException, IOException {
ArrayList arrayList = new ArrayList<String>();
PDDocument document = new PDDocument();
PDFont font = PDType1Font.HELVETICA;
PDPage page = new PDPage();
arrayList.add( "12345 56789 0 aaa bbbew wel kwäer kweork merkweporkm roer wer wer e er"
+ "df sdmfkl slkdfm sdkfdof sopdkfp osdkfo sädölf söldm,f sdkfpoekr re, ä"
+ " sdfk msdlkfmsdlk fsdlkfnsdlk fnlkdn flksdnfkl sdnlkfn kln df sdmfn sn END");
arrayList.add("this is an example");
arrayList.add("java pdfbox stackoverflow");
new pdfTest(document,arrayList,font);
System.out.println("pdf created!");
}
public String [] splitString(String text) {
int linebreaks=text.length()/80;
String [] newText = new String[linebreaks+1];
String tmpText = text;
String [] parts = tmpText.split(" ");
StringBuffer [] stringBuffer = new StringBuffer[linebreaks+1];
int i=0;
int totalTextLength=0;
for(int k=0; k<linebreaks+1;k++) {
stringBuffer[k] = new StringBuffer();
while(true) {
if (i>=parts.length) break;
totalTextLength=totalTextLength+parts[i].length();
if (totalTextLength>80) break;
stringBuffer[k].append(parts[i]);
stringBuffer[k].append(" ");
i++;
}
totalTextLength=0;
newText[k] = stringBuffer[k].toString();
}
return newText;
}
}