简体   繁体   English

如何扩展现有PDF的页面大小以在iText中添加页脚

[英]How to extend the page size of existing PDF to add a footer in iText

I need to add content in the form a footer with a paragraph centered to all pages of an existing PDF file. 我需要以页脚的形式添加内容,其中的段落以现有PDF文件的所有页面为中心。 I've followed this previous answer but the solution does not solve my issue. 我已经遵循了之前的答案,但是解决方案无法解决我的问题。 Although the code works, my PDFs do NOT have enough bottom margin to fit the footer so the text gets on top of the content. 尽管该代码有效,但我的PDF的底部页边距不足以适合页脚,因此文本位于内容顶部。

What I need to do before adding the footer is adding some extra margin at the bottom (effectively extending the page size). 添加页脚之前,我需要做的是在底部添加一些额外的边距(有效地扩展了页面大小)。 Similar to what is done in this question but ONLY that I need to add the extra margin at the bottom instead. 类似于在此问题中所做的操作,但只需要在底部添加额外的边距即可。

I also tried this but didn't work. 我也试过这个 ,但没有奏效。 I'm trying to figure it out myself but no luck so far. 我想自己弄清楚,但到目前为止还没有运气。

If you know how to add extra space on the side as explained in this question: How to extend the page size of a PDF to add a watermark? 如果您知道如何按此问题的说明在侧面添加额外的空间: 如何扩展PDF的页面大小以添加水印? then you should know how to add extra space to the bottom too. 那么您也应该知道如何在底部添加额外的空间。 Your question is a duplicate. 您的问题是重复的。

The page size of a PDF document is defined using the /MediaBox . PDF文档的页面大小是使用/MediaBox定义的。 It can be cropped using the /CropBox . 可以使用/CropBox对其进行裁剪。 In the answer I gave, we change the /MediaBox like this: 在给出的答案中,我们将/MediaBox更改为:

PdfArray mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
llx = mediabox.getAsNumber(0).floatValue();
mediabox.set(0, new PdfNumber(llx - 36));

The value of llx is the coordinate of the lower-left X-coordinate, and we subtract half an inch (36 user units). llx的值是左下角X坐标的坐标,我们减去半英寸(36个用户单位)。

If you want to change the bottom border, you have to change the lower-left Y-coordinate: 如果要更改底部边框,则必须更改左下角的Y坐标:

PdfArray mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
lly = mediabox.getAsNumber(1).floatValue();
mediabox.set(1, new PdfNumber(llx - 36));

That's what Mark is doing in his answer to the question How do I resize an existing PDF with Coldfusion/iText 这就是Mark在回答问题“我如何使用Coldfusion / iText调整现有PDF大小”时所做的事情

Of course, this won't have any effect if there's a crop box. 当然,如果有一个农作物箱,这将不会有任何效果。 If there's a crop box, you need to change the llx value of the crop box too: 如果有一个裁剪框,则还需要更改裁剪框的llx值:

PdfArray cropbox = pageDict.getAsArray(PdfName.CROPBOX);
if (cropbox != null) {
    lly = cropbox.getAsNumber(1).floatValue();
    cropbox.set(1, new PdfNumber(llx - 36));
}

Obviously, you need to take the change into account when adding the footer. 显然,添加页脚时需要考虑更改。 The bottom coordinate is no longer lly but lly - 36 in my example. 底部坐标不再是lly而是lly - 36在我的示例中为lly - 36

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM