使用pdfContentByte
访问新页面的问题很明显。我使用下面的代码将第一页之后的数据放到下一页但不幸的是iText
没有生成新页面。
//step1
itextDocument = new com.itextpdf.text.Document(PageSize.A4, 50, 50, 30, 65);
writer = PdfWriter.getInstance(itextDocument, new FileOutputStream(RESULT));
itextDocument.open();
writer.setPageEmpty(true);
itextDocument.newPage();
// step 2 == design and set the postions
// Measuring a String in Helvetica
Font font = new Font(FontFamily.TIMES_ROMAN, 10);
BaseFont romanFont = font.getCalculatedBaseFont(false);
// Drawing lines to see where the text is added
PdfContentByte canvas = writer.getDirectContent();
canvas.saveState();
canvas.stroke();
canvas.restoreState();
// Adding text with PdfContentByte.showTextAligned()
canvas.beginText();
canvas.setFontAndSize(romanFont, 10);
//=================== get data from xml and put in pdf
createPDF(xmlDoc, canvas);
createPDF(xmlDoc, canvas){
for(int i=0;i<300;i++){
contentByte.showTextAligned(Element.ALIGN_LEFT, "sample value", flotX, flotY, 0);
}
}
static int flotX = 50;
static int flotY = 800;
如何生成新页面?任何建议。
答案 0 :(得分:5)
您使用
writer.setPageEmpty(true);
你应该使用
writer.setPageEmpty(false);
代替表示当前页面不应被视为空。只要它被认为是空的,newPage就不会改变任何东西。
如果您真的想使用低级方法创建PDF内容(即直接在PdfContentByte
画布上定位文本而不是将布局留给iText),您必须意识到每个页面都有自己的内容画布显示一个矩形(裁剪框默认为媒体框),其余部分保持隐藏状态。
PdfContentByte
返回的writer.getDirectContent
会在新网页启动时自动切换。
因此,对于跨页面传播的内容,您必须在要进入下一页时准确调用itextDocument.newPage
,然后再次开始填充裁剪框。
沿着示例代码行:
Document itextDocument = new Document(PageSize.A4, 50, 50, 30, 65);
PdfWriter writer = PdfWriter.getInstance(itextDocument, new FileOutputStream(RESULT));
itextDocument.open();
PdfContentByte canvas = writer.getDirectContent();
BaseFont romanFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
// first page content
canvas.setFontAndSize(romanFont, 10);
canvas.beginText();
canvas.showTextAligned(Element.ALIGN_LEFT, "Line 1 on page 1", 50, 800, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "Line 2 on page 1", 50, 785, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "Line 3 on page 1", 50, 770, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "................", 50, 755, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "................", 50, 740, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "................", 50, 725, 0);
canvas.setFontAndSize(romanFont, 800);
canvas.showTextAligned(Element.ALIGN_LEFT, "1", 0, 100, 0);
canvas.endText();
itextDocument.newPage();
// first page content
canvas.setFontAndSize(romanFont, 10);
canvas.beginText();
canvas.showTextAligned(Element.ALIGN_LEFT, "Line 1 on page 2", 50, 800, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "Line 2 on page 2", 50, 785, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "Line 3 on page 2", 50, 770, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "................", 50, 755, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "................", 50, 740, 0);
canvas.showTextAligned(Element.ALIGN_LEFT, "................", 50, 725, 0);
canvas.setFontAndSize(romanFont, 800);
canvas.showTextAligned(Element.ALIGN_LEFT, "2", 0, 100, 0);
canvas.endText();
itextDocument.close();
这会产生以下两页:
或者你也可以创建一个独立的,更大的PdfTemplate
(从PdfContentByte
派生),在其上绘制所有内容,然后在不同的页面上显示它的部分:
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("big-panel.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(PageSize.A4.getWidth(), 3 * PageSize.A4.getHeight());
// draw all your content on tp
cb.addTemplate(tp, 0, -2 * PageSize.A4.getHeight());
document.newPage();
cb.addTemplate(tp, 0, -PageSize.A4.getHeight());
document.newPage();
cb.addTemplate(tp, 0, 0);
document.newPage();
cb.addTemplate(tp, 0.3333f, 0, 0, 0.3333f, PageSize.A4.getWidth() / 3, 0);
document.close();