使用ColumnText会导致“文档没有页面”例外 [英] Using ColumnText results in "The document has no pages" exception
问题描述
我想将文字换成图像下方(或左侧或右侧)的矩形,如下所示:
I want to wrap text in a rect which is below (or left or right) of a image as below :
请参阅链接: http://upanh.in/SLk/
我使用ColumnText在我的代码中包装文本:
I use ColumnText for wrapping text in my code:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/pdf");
try {
// step 1
Document document = new Document(PageSize.A4.rotate());
// step 2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
// step 3
document.open();
// step 4
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(new Phrase("text is very long ..."), 10, 10, 20, 20, 18, Element.ALIGN_CENTER);
column.go();
// step 5
document.close();
ServletOutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
} catch (DocumentException e) {
throw new IOException(e.getMessage());
}
}
结果:
ExceptionConverter:java.io.IOException:该文档没有页面。
ExceptionConverter: java.io.IOException: The document has no pages.
您对此有何建议?
问题2
I尝试使用下面的代码在rect中显示文本(中间和中间)但不成功。文本只是矩形的中心。
I try to display text (center and middle) in the rect with below code but it wasn't success. The text was only center in the rect.
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(RectImg1[0], RectImg1[1], RectImg1[0] + squareHeight, RectImg1[1] + squareHeight
* 1 / 4);
Paragraph p = new Paragraph(imgr.getText(), fontH);
p.setAlignment(Element.ALIGN_CENTER | Element.ALIGN_MIDDLE);
p.setLeading(18);
column.addElement(p);
column.go();
我的错误在哪里?
推荐答案
我已经编辑了你的问题的标题,因为它有误导性:你遇到的例外情况也会发生在一个独立的应用程序中。您在Servlet中使用代码这一事实无关紧要。
I have edited the title of your question because it was misleading: the exception you encounter will occur in a standalone application too. The fact that you are using the code in a Servlet is irrelevant.
我看到您有以下行:
column.go();
你没有使用这样的东西:
You did not use something like this:
int status = column.go();
如果你这样做,并且你检查了状态
,你会注意到列
对象仍然包含一些文字。
If you did, and if you examined status
, you would have noticed that the column
object still contained some text.
什么文字? 所有文字。
此行中存在严重错误:
column.setSimpleColumn(new Phrase("text is very long ..."), 10, 10, 20, 20, 18, Element.ALIGN_CENTER);
您正在尝试添加文本文字很长...
进入一个带有以下坐标的矩形:
You are trying to add the text "text is very long ..."
into a rectangle with the following coordinates:
float llx = 10;
float lly = 10;
float urx = 20;
float ury = 20;
您没有定义字体,因此字体是Helvetica,字体大小为12pt,您定义了18pt的领先优势。
You didn't define a font, so the font is Helvetica with font size 12pt and you defined a leading of 18pt.
这意味着你试图将12pt高的文本与额外的6pt相匹配,以便进入一个10×10 pt的正方形。当然你明白这不起作用!
This means that you are trying to fit text that is 12pt high with an extra 6pt for the leading into a square that measures 10 by 10 pt. Surely you understand that this can't work!
因此,没有任何内容添加到PDF而不是显示空白页面,iText会抛出异常说:那里没有页面! 您没有在文档中添加任何内容!
As a result, nothing is added to the PDF and rather than showing an empty page, iText throws an exception saying: there are no pages! You didn't add any content to the document!
您可以解决此问题,例如将错误的行更改为以下内容:
You can fix this, for instance by changing the incorrect line into something like this:
column.setSimpleColumn(new Phrase("text is very long ..."), 36, 36, 559, 806, 18, Element.ALIGN_CENTER);
另一种选择是:
column.setSimpleColumn(rect);
column.addElement(paragraph);
在这两行 rect
是 Rectangle
对象。前导和对齐将在段落
对象的级别定义(在这种情况下,您不使用短语
)。
In these two lines rect
is a Rectangle
object. The leading and the alignment are to be defined at the level of the Paragraph
object (in this case, you don't use a Phrase
).
这篇关于使用ColumnText会导致“文档没有页面”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!