Java – how to expand the page size of PDF to add watermark?

My web application signs PDF documents I want users to download the original PDF document (unsigned), but add an image and signer on the left edge of the PDF document

I saw this idea in another web application and I want to do it Of course, I want to do it with the iText library

I attached two pictures, the original PDF file (unsigned) and the modified PDF file

Solution

First: it is important to change the document before digitally signing the data After being digitally signed, these changes will destroy the signature

I will break down the problem in two parts, and I will skip the part about the actual watermark, because this has been explained here: how to watermark PDFs using text or images?

This question is not a repetition of that question, because additional requirements add additional margin on the right

Take a look at primes Pdf document This is the source file we will use in the addextramargin example, and the result is as follows: primes_ extra_ margin. pdf. As you can see, a half inch margin is added to the left side of each page

This is how it is done:

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 media@R_673_2419@;
    float llx,lly,ury;
    // loop over every page
    for (int i = 1; i <= n; i++) {
        pageDict = reader.getPageN(i);
        media@R_673_2419@ = pageDict.getAsArray(PdfName.MEDIA@R_673_2419@);
        llx = media@R_673_2419@.getAsNumber(0).floatValue();
        lly = media@R_673_2419@.getAsNumber(1).floatValue();
        ury = media@R_673_2419@.getAsNumber(3).floatValue();
        media@R_673_2419@.set(0,new PdfNumber(llx - 36));
        over = stamper.getOverContent(i);
        over.saveState();
        over.setColorFill(new GrayColor(0.5f));
        over.rectangle(llx - 36,36,ury - llx);
        over.fill();
        over.restoreState();
    }
    stamper.close();
    reader.close();
}

The pdfdictionary we get using the getpagen () method is called a page dictionary It contains a lot of information about specific pages in PDF Let's just look at one item:/ Media@R_673_2419 @This is just a proof of concept If you want to write more powerful applications, you should also look at/ Crop@R_673_2419 @And / or rotate entries By the way, I know primes These entries do not exist in PDF, so I omit them here

The media frame of a page is an array containing four values that represent a rectangle defined by the coordinates of its lower left and upper right corners (usually, I call them LLX, urx and ury)

In my code example, I change the value of LLX by subtracting 36 user units If you compare the page sizes of two PDFs, you will see that we have added half an inch

We also use these coordinates to draw a rectangle covering an additional half inch Now switch to other watermark examples to see how to add text or other content to each page

to update:

If you need to shrink an existing page, please read fix the orientation of a PDF in order to scale it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>