Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - How to extend the page size of a PDF to add a watermark?

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.

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.

I have attached two images, the original PDF document (not signed) and the modified PDF document.

enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First this: it is important to change the document before you digitally sign it. Once digitally signed, these changes will break the signature.

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.

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.

This is how it's 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 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();
}

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.

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).

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.

Update:

if you need to scale down the existing pages, please read Fix the orientation of a PDF in order to scale it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...