iText HTML页眉和页脚尺寸 [英] iText HTML header and footer dimensions
问题描述
我正在查看示例: http://developers.itextpdf.com/question/how-add-html-headers-and-footers-page
但在onEndPage方法中
but in the onEndPage method
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
for (Element e : header) {
ct.addElement(e);
}
ct.go();
ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
for (Element e : footer) {
ct.addElement(e);
}
ct.go();
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
我们必须知道标题元素将进入的ractangle的高度和宽度.
we must know height and width of the ractangle in which the header elements will go.
就我在编译时的情况而言,我不知道将使用哪种HTML,因此我想推断尺寸,或使用其他不需要宽度和高度的代码
In my case at compilation time I don't know which HTML will be used, so I would like to deduce dimensions, or use another code that don't need width and heght
推荐答案
您在onEndPage()
方法的参数中有一个Document
对象.您可以像这样获取文档的页面大小:
You have a Document
object in the parameters of the onEndPage()
method. You can get the page size of the document like this:
Rectangle pageSize = document.getPageSize();
您还可以要求文档提供空白:
You can also ask the document for its margins:
float left = document.leftMargin();
float right = document.rightMargin();
float top = document.topMargin();
float bottom = document.bottomMargin();
您可以使用这些值来定义页眉和页脚的可用空间.
You can use these values to define the available space for headers and footers.
例如标头:
Rectangle rect1 = new Rectangle(
pageSize.left() + left, pageSize.top() - top,
pageSize.right() - right, pageSize.top());
或页脚:
Rectangle rect2 = new Rectangle(
pageSize.left() + left, pageSize.bottom(),
pageSize.right() - right, pageSize.bottom() + bottom);
注意事项:对于您要添加的内容,矩形可能会太小.在这种情况下,某些内容将丢失.通过查看go()
方法的返回值,您将知道何时发生这种情况:
Caveat: there is a chance that your rectangles will be too small for the content that you are adding. In that case, some of the content will be lost. You will know when that happens by looking at the return value of the go()
method:
int status = ct.go();
boolean fits = !ColumnText.hasMoreText(status);
如果fits
是true
,则不会丢失任何内容.如果fits
为false
,则可能需要重新考虑如何添加内容.您可以先在模拟模式下添加内容,检查其是否适合,然后再将其添加为真实内容.如果不合适,则可以缩小内容(例如,通过减小字体大小),然后重试,直到内容适合为止.
If fits
is true
no content was lost. If fits
is false
, you may want to reconsider how you're adding the content. You could add the content in simulation mode first, check if it fits, and then add it for real. If it doesn't fit, you can make the content smaller (for instance by reducing the font size) and try again until the content fits.
这篇关于iText HTML页眉和页脚尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!