本文介绍了如何扩展PDF的页面大小以添加水印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的网络应用程序签署PDF文档。我想让用户下载原始PDF文档(未签名),但在pdf文档的左边缘添加图像和签名者。My web application signs PDF documents. I would like to let users download the original PDF document (not signed) but adding an image and the signers in the left margin of the pdf document.我见过这个想法在另一个Web应用程序中,我想做同样的事情。当然我想用itext库来做。I've seen this idea in another web application, and I would like to do the same. Of course I would like to do it using itext library.我附上了两张图片,原始PDF文件(未签名)和修改后的PDF文件。I have attached two images, the original PDF document (not signed) and the modified PDF document.推荐答案首先:在对进行数字签名之前更改文档非常重要。一旦经过数字签名,这些更改将破坏签名。First this: it is important to change the document before you digitally sign it. Once digitally signed, these changes will break the signature.我将分两部分分解问题,我将跳过关于实际水印的部分,因为这已经是这里解释:如何使用文本或图像为PDF水印?I will break up the question in two parts and I'll skip the part about the actual watermarking as this is already explained here: How to watermark PDFs using text or images?这个问题与该问题不重复,因为额外要求在右边添加额外的保证金。This question is not a duplicate of that question, because of the extra requirement to add an extra margin to the right.查看 primes.pdf 文档。这是我们将在 AddExtraMargin 示例中使用的源文件,其结果如下: primes_extra_margin.pdf 。正如您所看到的,每页左侧增加了半英寸的边距。Take a look at the primes.pdf document. This is the source file we are going to use in the AddExtraMargin example with the following result: primes_extra_margin.pdf. As you can see, a half an inch margin was added to the left of each page.这就是它的完成方式:public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); int n = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); // properties PdfContentByte over; PdfDictionary pageDict; PdfArray mediabox; float llx, lly, ury; // loop over every page for (int i = 1; i <= n; i++) { pageDict = reader.getPageN(i); mediabox = pageDict.getAsArray(PdfName.MEDIABOX); llx = mediabox.getAsNumber(0).floatValue(); lly = mediabox.getAsNumber(1).floatValue(); ury = mediabox.getAsNumber(3).floatValue(); mediabox.set(0, new PdfNumber(llx - 36)); over = stamper.getOverContent(i); over.saveState(); over.setColorFill(new GrayColor(0.5f)); over.rectangle(llx - 36, lly, 36, ury - llx); over.fill(); over.restoreState(); } stamper.close(); reader.close();} PdfDictionary 我们得到 getPageN()方法被称为页面字典。它包含有关PDF中特定页面的大量信息。我们只查看一个条目: / MediaBox 。这只是一个概念证明。如果你想编写一个更强大的应用程序,你还应该查看 / CropBox 和 / Rotate 条目。顺便说一句,我知道这些条目在primes.pdf中不存在,所以我在这里省略它们。The PdfDictionary we get with the getPageN() method is called the page dictionary. It has plenty of information about a specific page in the PDF. We are only looking at one entry: the /MediaBox. This is only a proof of concept. If you want to write a more robust application, you should also look at the /CropBox and the /Rotate entry. Incidentally, I know that these entries don't exist in primes.pdf, so I am omitting them here. 媒体框 page是一个包含四个值的数组,表示由其左下角和右上角的坐标定义的矩形(通常,我将它们称为 llx , lly , urx 和 ury )。The media box of a page is an array with four values that represent a rectangle defined by the coordinates of its lower-left and upper-right corner (usually, I refer to them as llx, lly, urx and ury).在我的代码示例中,我通过减去36个用户单位来更改 llx 的值。如果您比较两个PDF的页面大小,您会看到我们已经添加了半英寸。In my code sample, I change the value of llx by subtracting 36 user units. If you compare the page size of both PDFs, you'll see that we've added half an inch.我们还使用这些坐标绘制一个覆盖额外的半英寸。现在切换到其他水印示例,了解如何为每个页面添加文字或其他内容。We also use these coordinates to draw a rectangle that covers the extra half inch. Now switch to the other watermark examples to find out how to add text or other content to each page. 更新:如果您需要缩小现有页面,请阅读修正PDF的方向以便扩展它if you need to scale down the existing pages, please read Fix the orientation of a PDF in order to scale it 这篇关于如何扩展PDF的页面大小以添加水印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 13:36
查看更多