我想一些字符串,图片和表格添加到我的PDF文件(有有有几页),但是当我尝试使用ColumnText
(我用这个,因为我想在的地方绝对位置的字符串),我遇到一个问题。 当列高度不足以添加字符串的内容,内容是不完整的。 我怎样才能避免内容迷失?
下面是相关的代码:
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
String imageUrl = "/Users/nofear/workspace/deneme23/pics/a4-ust.png";
String imageUrlAlt = "pics/a4-alt.png";
Image imageust = null;
Image imageAlt = null;
try {
imageust = Image.getInstance(imageUrl);
imageAlt = Image.getInstance(imageUrlAlt);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("HEIGHT"
+ (document.getPageSize().getHeight() - imageust.getHeight()));
imageust.setAbsolutePosition(0f,
document.getPageSize().getHeight() - imageust.getHeight()-10);
imageAlt.setAbsolutePosition( 0f, 10f);
document.add(imageust);
document.add(imageAlt);
// now draw a line below the headline
cb.setLineWidth(1f);
cb.moveTo(0, 200);
cb.lineTo(200, 200);
cb.stroke();
// first define a standard font for our text
Font helvetica8BoldBlue = FontFactory.getFont(FontFactory.HELVETICA,16);
// create a column object
ColumnText ct = new ColumnText(cb);
// define the text to print in the column
Phrase myText = new Phrase("Very Very Long String!!!" , helvetica8BoldBlue);
ct.setSimpleColumn(myText, 60, 750,
/* width*/document.getPageSize().getWidth() - 40, 100,
20, Element.ALIGN_LEFT);
ct.go();
} catch (Exception e) {
} finally {
document.close();
}