我昨天是用手机接电话的,但现在我可以使用电脑了,我改变了ColumnDocumentRenderer
这样的:
public class ColumnDocumentRenderer extends DocumentRenderer {
protected Rectangle[] columns;
protected int nextAreaNumber;
public ColumnDocumentRenderer(Document document, Rectangle[] columns) {
super(document);
this.columns = columns;
}
public ColumnDocumentRenderer(Document document, boolean immediateFlush, Rectangle[] columns) {
super(document, immediateFlush);
this.columns = columns;
}
@Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
if (overflowResult != null && overflowResult.getAreaBreak() != null && overflowResult.getAreaBreak().getType() != AreaBreakType.NEXT_AREA) {
nextAreaNumber = 0;
}
if (nextAreaNumber % columns.length == 0) {
super.updateCurrentArea(overflowResult);
}
return (currentArea = new LayoutArea(currentPageNumber, columns[nextAreaNumber++ % columns.length].clone()));
}
public int getNextAreaNumber() {
return nextAreaNumber;
}
}
更改将在 iText 7.0.1 中进行,但您可以在自己的渲染器中使用此代码。
您现在可以像这样使用此渲染器:
public void createPdf(String dest) throws IOException {
OutputStream fos = new FileOutputStream(dest);
PdfWriter writer = new PdfWriter(fos);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
float offSet = 36;
float gutter = 23;
float columnWidth = (PageSize.A4.getWidth() - offSet * 2) / 3 - gutter * 2;
float columnHeight = PageSize.A4.getHeight() - offSet * 2;
Rectangle[] columns = {
new Rectangle(offSet, offSet, columnWidth, columnHeight),
new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight),
new Rectangle(offSet + 2 * (columnWidth + gutter), offSet, columnWidth, columnHeight)};
ColumnDocumentRenderer renderer = new ColumnDocumentRenderer(document, columns);
document.setRenderer(renderer);
for (int i = 0; i < 50; i++) {
document.add(new Paragraph("Hello World"));
}
while (renderer.getNextAreaNumber() % 3 != 0)
document.add(new AreaBreak());
document.add(new Paragraph("Third column"));
document.add(new AreaBreak());
for (int i = 0; i < 80; i++) {
document.add(new Paragraph("Hello World"));
}
while (renderer.getNextAreaNumber() % 3 != 0)
document.add(new AreaBreak());
document.add(new Paragraph("Third column"));
document.add(new AreaBreak());
for (int i = 0; i < 10; i++) {
document.add(new Paragraph("Hello World"));
}
while (renderer.getNextAreaNumber() % 3 != 0)
document.add(new AreaBreak());
document.add(new Paragraph("Third column"));
document.close();
}
第一列的索引为 0,下一个区域编号为 1,第二列的索引为 1,下一个区域编号为 2,以此类推。
这意味着您可以检查并转到此类页面上的第三列。
while (renderer.getNextAreaNumber() % 3 != 0)
document.add(new AreaBreak());