gpt4 book ai didi

java - 段落位于剩余页面的中心(水平和垂直)

转载 作者:行者123 更新时间:2023-12-01 14:02:31 24 4
gpt4 key购买 nike

我需要使用 iText 创建 PDF 文件。在第一页上,页面顶部应该有一个标题,然后文档标题正好位于剩余页面区域的中心(水平和垂直)。

Google 搜索了很多,我发现的最好的解决方案是创建一个表格并使用其单元格对齐方法。问题是:要正确使用垂直对齐,我需要设置单元格的最小高度(cell.setMinimumHeight(...);),但我不知道还剩下多少高度!将 document.getPageSize ().getHeight () 与一些硬编码偏移一起使用看起来不是一个好的选择 - 我不想在每次更改字体大小等时更改此硬编码。

这是页面顶部“标题”的代码(如果重要的话):

Paragraph preface = new Paragraph();
Paragraph o = new Paragraph("test", headerFont);
o.add(new LineSeparator(1, 100, Color.BLACK, Element.ALIGN_CENTER, -5));
preface.add(o);
o.add(new Paragraph(" "));
document.add(preface);

最佳答案

好的,这就是我到目前为止所得到的......

public static float getAvailableHeight(PdfDocument pdfDocument) {
Float indentBottom = pdfDocument.bottomMargin();
try {
Method method = pdfDocument.getClass().getDeclaredMethod("indentBottom");
method.setAccessible(true);
indentBottom = (Float) method.invoke(pdfDocument);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
float offset = pdfDocument.top() - pdfDocument.getVerticalPosition(false);
return pdfDocument.getPageSize().getHeight() - offset - pdfDocument.topMargin() - indentBottom
- pdfDocument.bottomMargin();
}

对我有用。希望这对其他人有帮助。

您需要访问封装在 PdfWriter 内的 PdfDocument 对象。我刚刚制作了自己的 CustomPdfWriter,它扩展了 PdfWriter。

需要带有反射的丑陋部分,因为方法 indentBottom() 是 PdfDocument 类中的本地包。

关于java - 段落位于剩余页面的中心(水平和垂直),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19241984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com