【发布时间】:2019-03-15 13:29:54
【问题描述】:
所以...我一直在尝试使用 itext 文档中提供的示例来合并文档并为合并结果创建目录。但是向每一页添加页码文本的部分并没有像我预期的那样工作。发生的情况是添加的文本在某个水平轴上翻转,如下图所示:
此外,用于为添加的文本 (public T setFixedPosition(int pageNumber, float left, float bottom, float width)) 设置固定位置的方法的 java 文档对我来说没有意义:
为元素的绝对重新定位设置值。指定的坐标对应于元素的左下角,并且向上增长。
但是当我运行setFixedPosition(pageNumber, 0, 0, 50) 时,文本最终出现在左上角,同样也翻转了。如果我分别使用源 PdfDocument 的页面大小的宽度和高度作为左侧和底部位置的参数,它甚至不会到达右下角。
我可能做错了什么或误解了什么。无论哪种方式,这是我正在使用的代码:
private static int copyPdfPages(PdfDocument source, Document document, Integer start, Integer pages, Integer number) {
int oldC;
int max = start + pages - 1;
Text text;
for (oldC = start; oldC <= max; oldC++) {
text = new Text(String.format("Page %d", number));
PageSize pageSize = source.getDefaultPageSize();
source.copyPagesTo(oldC, oldC, document.getPdfDocument());
document.add(new Paragraph(text).setBorder(new SolidBorder(ColorConstants.RED, 1))
.setFixedPosition(number++, pageSize.getWidth() - 55, pageSize.getHeight() - 30, 50));
}
return oldC - start;
}
public static void main(String[] args) throws IOException {
String path = "/path/to/target";
FileOutputStream fos = new FileOutputStream(path);
PdfDocument pdfDocTgt = new PdfDocument(new PdfWriter(fos));
Document document = new Document(pdfDocTgt);
PdfDocument pdfDocSrc = new PdfDocument(new PdfReader(new FileInputStream("path/to/source")));
copyPdfPages(pdfDocSrc, document, 1, pdfDocSrc.getNumberOfPages(), 1);
pdfDocTgt.close();
pdfDocSrc.close();
document.flush();
document.flush();
fos.flush();
fos.close();
}
这里是pdf源:https://drive.google.com/open?id=11_9ptuoRqS91hI3fDcs2FRsIUEiX0a84
请帮忙(对我的英语感到抱歉)。
【问题讨论】: