我在报告页脚中有一个HTML代码。但是,此页脚的高度可能会根据我的HTML代码的内容而有所不同。是否可以做一些工作来根据代码的大小自动调整页脚的高度?
Footer.java
public class Footer extends PdfPageEventHelper {
protected ElementList footer;
public Footer(String html) throws IOException {
footer = XMLWorkerHelper.parseToElementList(html, null);
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
ColumnText ct = new ColumnText(writer.getDirectContent());
// In my case this rectagle is resizeable height
ct.setSimpleColumn(new Rectangle(36, 200, 559, 32));
for (Element e : footer) {
ct.addElement(e);
}
ct.go();
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
答案 0 :(得分:1)
您的页脚似乎没有任何动态部分,各页之间有所不同。在这种情况下,您可以简单地从将html渲染为一些一次性PDF并测量其尺寸开始,然后使用这些尺寸设置您创建的实际文档的边距。
您可以实施此方法,例如使用这样的页面事件侦听器类:
Movie.java
@OneToMany(mappedBy = "movie", targetEntity = Clip.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Clip> clips = new HashSet<Clip>();
//the below getter will look for data of related Clip
public Set<Clip> getClips(){ return this.clips}
Clip.java
@ManyToOne
@JoinColumn(name="movie_id")
private Movie movie;
//the below getter will look for related Movie again
public Movie getMovie() { return this.movie }
(StaticFooter助手类)
您可以这样应用它:
public static class Footer extends PdfPageEventHelper {
private final Rectangle boundingBox;
private final List<Element> elements;
private PdfTemplate template = null;
private float blankAfter = 0;
public Footer(String html, float width) throws DocumentException, IOException {
this(XMLWorkerHelper.parseToElementList(html, null), width);
}
public Footer(List<Element> elements, float width) throws DocumentException, IOException {
this.elements = elements;
try ( OutputStream os = new NullOutputStream() ) {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, os);
document.open();
Rectangle bbox = new Rectangle(0, 0, width, 1000);
float bottomLine = createTemplate(writer, bbox);
boundingBox = new Rectangle(width, bbox.getTop() - bottomLine);
template = null;
writer.setPageEmpty(false);
document.close();
}
}
float createTemplate(PdfWriter writer, Rectangle boundingBox) throws DocumentException {
template = writer.getDirectContent().createTemplate(10000, 10000);
template.setBoundingBox(boundingBox);
ColumnText columnText = new ColumnText(template);
Rectangle bbox = template.getBoundingBox();
columnText.setSimpleColumn(bbox.getLeft(), bbox.getBottom(), bbox.getRight(), bbox.getTop());
for (Element element : elements) {
columnText.addElement(element);
}
columnText.go();
return columnText.getYLine() + columnText.getDescender();
}
void ensureTemplate(PdfWriter writer) throws DocumentException {
if (template == null) {
createTemplate(writer, boundingBox);
}
}
public void setBottomMargin(Document document, float blankBefore, float blankAfter) {
float marginBottom = boundingBox.getHeight() + blankBefore + blankAfter;
document.setMargins(document.leftMargin(), document.rightMargin(), document.topMargin(), marginBottom);
this.blankAfter = blankAfter;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
ensureTemplate(writer);
Rectangle bbox = template.getBoundingBox();
Rectangle pageSize = document.getPageSize();
float x = pageSize.getLeft((pageSize.getWidth() - bbox.getWidth()) / 2f - bbox.getLeft());
float y = pageSize.getBottom(blankAfter - bbox.getBottom());
writer.getDirectContentUnder().addTemplate(template, x, y);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onEndPage(writer, document);
}
}
(StaticFooter测试Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "StaticFooterShort.pdf")));
String html = "<p>Short Test Footer.</p>";
Footer footer = new Footer(html, document.right() - document.left());
writer.setPageEvent(footer);
footer.setBottomMargin(document, 10, 10);
document.open();
for (int i = 0; i < 200; i++) {
document.add(new Paragraph("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ..."));
}
document.close();
)
PS:我使用的testShortFooter
类是Apache Commons IO帮助器,但是如果您不希望额外的依赖关系,可以轻松实现类似的东西,只需实现忽略所有输入的NullOutputStream
即可。