下面的代码使用ColumnDocumentRenderer将Paragraph渲染为4个Rectangle区域。在调用cdr.draw()方法之前,我想确定文本是否太长而不适合。如果是,我会使用setHorizontalScaling使其适合。
我想使用生成LayoutResult的ColumnDocumentRenderer.layout方法来完成此操作。但是,它需要一个Rectangle bBox:cdr.layout(new LayoutContext(new LayoutArea(pageNumber,bBox)));
有没有办法迭代这些区域并找出所有区域是否已填满且最后一个区域是否会溢出?
public static void main(String[] args) throws FileNotFoundException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
PdfPage page = pdfDoc.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Document doc = new Document(pdfDoc);
Rectangle r1 = new Rectangle(50, 560, 200, 20);
Rectangle r2 = new Rectangle(50, 540, 200, 20);
Rectangle r3 = new Rectangle(25, 520, 275, 20);
Rectangle r4 = new Rectangle(25, 500, 275, 20);
Rectangle[] rects = { r1, r2, r3, r4 };
Text t = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
+ "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
+ "nisi ut aliquip ex ea commodo consequat. ");
t.setFontSize(10f);
//t.setHorizontalScaling(.85f);
Paragraph p = new Paragraph();
p.setMargin(0);
p.setMultipliedLeading(1);
p.add(t);
p.setTextAlignment(TextAlignment.JUSTIFIED);
ParagraphRenderer pr = (ParagraphRenderer)p.createRendererSubTree();
ColumnDocumentRenderer cdr = new ColumnDocumentRenderer(doc, rects);
cdr.addChild(pr);
cdr.draw(new DrawContext(pdfDoc, pdfCanvas));
pdfDoc.close();
}
答案 0 :(得分:1)
这里重要的一点是,根呈现器不支持layout
操作。但是,它们具有getCurrentArea()
方法,允许您跟踪文档中当前未使用的位置。
想法是布置段落,看看我们是否发现自己在第二页或第一页,如果发生溢出(第二页),则缩小文本。
根据您的使用案例,此方法可能需要进行一些更改,尤其是在删除页面的部分,但我假设您从头开始创建文档,就像在问题中提供的示例中一样。
该方法在代码中如下所示:
float step = 0.05f;
float curHorScaling = 1;
ColumnDocumentRenderer cdr;
while (true) {
// Remove extra pages and create a new, clean ColumnDocumentRenderer
while (doc.getPdfDocument().getNumberOfPages() > 1) {
doc.getPdfDocument().removePage(1);
}
cdr = new ColumnDocumentRenderer(doc, false, rects);
t.setHorizontalScaling(curHorScaling);
cdr.getChildRenderers().clear();
cdr.addChild(p.createRendererSubTree());
If one page is not enough, decrease the scaling, otherwise go to drawing
if (cdr.getCurrentArea().getPageNumber() > 1) {
curHorScaling -= step;
} else {
break;
}
}
完整修改后的代码:
Document doc = new Document(pdfDoc);
Rectangle r1 = new Rectangle(50, 560, 200, 20);
Rectangle r2 = new Rectangle(50, 540, 200, 20);
Rectangle r3 = new Rectangle(25, 520, 275, 20);
Rectangle r4 = new Rectangle(25, 500, 275, 20);
Rectangle[] rects = { r1, r2, r3, r4 };
Text t = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
+ "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
+ "nisi ut aliquip ex ea commodo consequat. ");
t.setFontSize(10f);
Paragraph p = new Paragraph();
p.setMargin(0);
p.setMultipliedLeading(1);
p.add(t);
p.setTextAlignment(TextAlignment.JUSTIFIED);
float step = 0.05f;
float curHorScaling = 1;
ColumnDocumentRenderer cdr;
while (true) {
while (doc.getPdfDocument().getNumberOfPages() > 0) {
doc.getPdfDocument().removePage(1);
}
cdr = new ColumnDocumentRenderer(doc, false, rects);
t.setHorizontalScaling(curHorScaling);
cdr.getChildRenderers().clear();
cdr.addChild(p.createRendererSubTree());
if (cdr.getCurrentArea().getPageNumber() > 1) {
curHorScaling -= step;
} else {
break;
}
}
cdr.draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.getFirstPage())));
pdfDoc.close();