我正在尝试使用页面右上角的Itext添加页码以合并PDF文件,但我的pdf内容大小不同,在尝试打印页面大小后合并PDF时我得到大致相同的大小(高度和宽度) )在每个页面上,但由于内容大小不同,我无法看到页码 . 请参阅下面的代码和pdf附件,用于合并PDF和添加页码 .
public class PageNumber {
public static void main(String[] args) {
PageNumber number = new PageNumber();
try {
String DOC_ONE_PATH = "C:/Users/Admin/Downloads/codedetailsforartwork/elebill.pdf";
String DOC_TWO_PATH = "C:/Users/Admin/Downloads/codedetailsforartwork/PP-P0109916.pdf";
String DOC_THREE_PATH = "C:/Users/Admin/Downloads/codedetailsforartwork/result.pdf";
String[] files = { DOC_ONE_PATH, DOC_TWO_PATH };
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(DOC_THREE_PATH));
document.open();
PdfReader reader;
int n;
for (int i = 0; i < files.length; i++) {
reader = new PdfReader(files[i]);
n = reader.getNumberOfPages();
for (int page = 0; page < n; ) {
copy.addPage(copy.getImportedPage(reader, ++page));
}
copy.freeReader(reader);
reader.close();
}
// step 5
document.close();
number.manipulatePdf(
"C:/Users/Admin/Downloads/codedetailsforartwork/result.pdf",
"C:/Users/Admin/Downloads/codedetailsforartwork/PP-P0109916_1.pdf");
} catch (IOException | DocumentException | APIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void manipulatePdf(String src, String dest)
throws IOException, DocumentException, APIException {
PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte pagecontent;
for (int i = 0; i < n;) {
pagecontent = stamper.getOverContent(++i);
System.out.println(i);
com.itextpdf.text.Rectangle pageSize = reader.getPageSize(i);
pageSize.normalize();
float height = pageSize.getHeight();
float width = pageSize.getWidth();
System.out.println(width + " " + height);
ColumnText.showTextAligned(pagecontent, Element.ALIGN_CENTER,
new Phrase(String.format("page %d of %d", i, n)),
width - 200, height-85, 0);
}
stamper.close();
reader.close();
}
}
2 回答
@ Bruno的答案解释和/或参考答案,并解释了有关手头问题的所有相关事实 .
简而言之,OP代码的两个问题是:
他使用
reader.getPageSize(i)
;虽然这确实返回了页面大小,但PDF查看器不显示整个页面大小,而只显示其上的裁剪框 . 因此,OP应该使用reader.getCropBox(i)
代替 . 根据PDF规范,"the crop box defines the region to which the contents of the page shall be clipped (cropped) when displayed or printed. ... The default value is the page’s media box."他使用
pageSize.getWidth()
和pageSize.getHeight()
确定右上角,但应使用pageSize.getRight()
和pageSize.getTop()
. 定义PDF坐标系的框可能在其左下角没有原点 .我不明白为什么你这样定义页码的位置:
你在哪里使用
这有什么意义?
如果您的纵向A4页面为
(0,0)
作为左下角的坐标,则页面编号将添加到位置x = 395; y = 757
. 但是,(0,0)
并不总是左下角的坐标,因此原点位于另一个位置的第一个A4页面已经将页码放在另一个位置 . 如果页面大小不同,页码将移动到其他位置 .它's as if you'完全没有意识到以前回答的问题,例如How should I interpret the coordinates of a rectangle in PDF?和Where is the Origin (x,y) of a PDF page?
我知道,我知道,在StackOverflow上找到这些具体答案很难,但我花了很多时间在官方网站上组织StackOverflow上最好的iText问题 . 例如,请参阅:How should I interpret the coordinates of a rectangle in PDF?和Where is the origin (x,y) of a PDF page?
这些问答甚至可以在_1241085中获得!如果您花点时间通过阅读文档来教育自己,您将找到2013年在StackOverflow上已经回答的问题How to position text relative to page?的答案:How to position text relative to page using iText?
例如,如果要将页码放在底部和中间,则需要像下面这样定义坐标:
我希望这个答案能激发你阅读文档 . 我花了数周的时间来组织文档,当我发现人们没有阅读它时,这是令人沮丧的 .