3
我想添加一些字符串,图像和表格到我的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();
}
您提供的2个选项不能解决我的问题,因为字体大小必须保持不变,所以如果内容达到矩形大小,则内容必须换行到具有简单列的另一个页面。 –
好的,我添加了第三个选项。 –