Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 剩余页面中心的段落(水平和垂直)_Java_Alignment_Itext - Fatal编程技术网

Java 剩余页面中心的段落(水平和垂直)

Java 剩余页面中心的段落(水平和垂直),java,alignment,itext,Java,Alignment,Itext,我需要使用iText创建PDF文件。在第一页,页面顶部应该有一个标题,然后在剩余页面区域(水平和垂直)的正中央有一个文档标题 谷歌搜索了很多,我发现最好的解决方案是创建一个表并使用它的单元格对齐方法。问题是:要正确使用垂直对齐,我需要设置单元格的最小高度(cell.setMinimumHeight(…);),但我不知道还有多少高度!使用document.getPageSize().getHeight()和一些硬编码的偏移量看起来不是一个好的选择-我不想在任何时候更改字体大小等 以下是页面顶部“标

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

谷歌搜索了很多,我发现最好的解决方案是创建一个表并使用它的单元格对齐方法。问题是:要正确使用垂直对齐,我需要设置单元格的最小高度(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对象。我刚刚制作了自己的定制PdfWriter,它扩展了PdfWriter

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