我正在尝试将几个表放在一个页面上。我已经实现了一个潜在的解决方案,我的问题是如何在我想要的时候进入下一个打印区域,而不是在当前打印区域被填满时。 这就是它现在的输出。我增加了字体大小,以便第二个区域自动部分填充。我有没有办法告诉它在我想要的时候使用下一个区域而不是iText7想要的时候?
我的代码是:
import java.io.File;
import java.io.FileNotFoundException;
import com.itextpdf.io.IOException;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.layout.LayoutArea;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.renderer.DocumentRenderer;
public class ColumnDocumentRendererTest {
static PdfDocument pdfDoc;
static Document document;
Table tble;
protected int nextAreaNumber = 0;
public static final String DEST = "./target/test/resources/sandbox/tables/horizontzl_alignment.pdf";
public ColumnDocumentRendererTest(Document document2) {
// TODO Auto-generated constructor stub
}
public static void main(String[] args)
throws IOException, FileNotFoundException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ColumnDocumentRendererTest(document).createPdf(DEST);
}
public void createPdf(String dest)
throws IOException, FileNotFoundException {
pdfDoc = new PdfDocument(new PdfWriter(DEST));
document = new Document(pdfDoc);
ColumnDocumentRenderer renderer = new ColumnDocumentRenderer(document);
document.setRenderer(renderer);
//Add paragraph to the document
tble = new Table(new float[] { 3f, 3f, 3f, 3f, 3f, 3f, 3f });
/*tble.addCell(new Cell()
.add(new Paragraph(tokenizer.nextToken()).setFont(font)));*/
for (int i = 1; i <= 21; i++) {
tble.addCell(new Cell().add(new Paragraph(Integer.toString(i))
.setTextAlignment(TextAlignment.RIGHT)
.setFontSize((float) 20.5)));
}
document.add(tble);
document.close();
}
}
答案 0 :(得分:2)
我没有阅读iText7的文档,它为这个问题提供了一个解释清楚的答案。 (见iText7 documentation)
答案证明是使用“画布”而不是“文档”。 以下代码解决了用例:
public class TablePrinter {
static PdfDocument pdfDoc;
Table tble;
protected int nextAreaNumber = 0;
public TablePrinter() {
}
public static final String DEST = "./target/test/resources/sandbox/tables/tables_printed.pdf";
public void createPdf(String dest) throws IOException, java.io.IOException {
//
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdfDoc.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(70, 722, 200, 100);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, pdfDoc, rectangle);
Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde")
.setFont(bold);
Text author = new Text("Robert Louis Stevenson").setFont(font);
Paragraph p = new Paragraph().add(title).add(" by ").add(author);
canvas.add(p);
rectangle = new Rectangle(310, 722, 200, 100);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
canvas.close();
canvas = new Canvas(pdfCanvas, pdfDoc, rectangle);
tble = new Table(new float[] { 3f, 3f, 3f, 3f, 3f, 3f, 3f });
/*tble.addCell(new Cell()
.add(new Paragraph(tokenizer.nextToken()).setFont(font)));*/
int DaysInMonth = 31;
for (int i = 1; i <= 35; i++) {
if (i <= DaysInMonth) {
tble.addCell(new Cell().add(new Paragraph(Integer.toString(i))
.setTextAlignment(TextAlignment.RIGHT)
.setFontSize((float) 8.0)));
if ((i >= DaysInMonth) && (i % 7 == 0))
break;
} else {
tble.addCell(new Cell());
if (i % 7 == 0)
break;
}
}
p = new Paragraph().add(tble);
canvas.add(p);
canvas.close();
pdfDoc.close();
//
}
public void addParagraph(Document document, int count) {
Paragraph p;
TextAlignment alignment;
for (int i = 1; i < count + 1; i++) {
p = new Paragraph("entry" + String.valueOf(i));
alignment = (i % 2 != 0) ? TextAlignment.LEFT : TextAlignment.RIGHT;
p.setTextAlignment(alignment);
document.add(p);
}
}
public static void main(String[] args)
throws IOException, java.io.IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TablePrinter().createPdf(DEST);
}
}