如何在矩形内拟合String? [英] How to fit a String inside a rectangle?
问题描述
我正在尝试将一些字符串,图像和表添加到我的pdf文件中(必须有几个页面),但是当我尝试使用 ColumnText
时(我使用)这是因为我想把字符串放在绝对位置),我遇到了一个问题。
如果列高度不足以添加字符串的内容,则内容不完整。如何避免内容丢失?
I'm trying to add some strings, images and tables into my pdf file (there have to be several pages) but when i try to use ColumnText
(I use this because I want to place strings at absolute positions), I encounter a problem.
When the column height is not sufficient to add the content of the strings, the content is incomplete. How can I avoid that content gets lost?
以下是相关代码:
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();
}
推荐答案
有三种选择:
- 要么提供更大的矩形,以便内容适合内部,
- 或者减少内容(例如较小的字体,较少的文字),...
- 保持矩形的大小,保持字体大小等...但添加不适合的内容下一页。
您如何知道内容是否适合?
How do you know if the content doesn't fit?
您可以先在模拟模式中添加内容,然后测试所有内容是否已消耗:
You can add the content in simulation mode first, and test if all the content was 'consumed':
int status = ct.go(true);
boolean fits = !ColumnText.hasMoreText(status);
根据的值
,你可以决定改变矩形或内容的大小。有一个示例说明如何执行此操作: http://itextpdf.com/examples/iia .php?id = 163
Based on the value of fits
, you can decide to change the size of the rectangle or the content. There's an example that shows how to do this: http://itextpdf.com/examples/iia.php?id=163
如果你可以在不同的页面上分发内容,你不需要模拟模式,你只需要插入一个 document.newPage();
If you can distribute the content over different pages, you don't need simulation mode, you just need to insert a document.newPage();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(rect);
int status = ct.go();
while (ColumnText.hasMoreText(status)) {
document.newPage();
ct.setSimpleColumn(rect);
status = ct.go();
}
在此示例中 rect
包含矩形的坐标。
In this example rect
contains the coordinates of the rectangle.
这篇关于如何在矩形内拟合String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!