2

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Eduardo
  • 1,169
  • 5
  • 21
  • 56
  • Am I right if I think that the document isn't merely watermarked, but also that the size of the page has changed. It's as if extra space was added to the left. Is this correct? Watermarking is explained here: [How to watermark PDFs using text or images?](http://stackoverflow.com/questions/29560373/how-to-watermark-pdfs-using-text-or-images). The answer is also available in French: [Comment créer un filigrane transparent en PDF?](http://www.developpez.net/forums/blogs/133351-blowagie/b432/creer-filigrane-transparent-pdf/) – Bruno Lowagie Apr 21 '15 at 15:08
  • Yes, the size of the page has changed. I've seen it with adobe reader. The first document page size is 215,9 x 279,4 and the second document page size is 210 x 297 (mm) – Eduardo Apr 21 '15 at 15:13
  • 1
    OK, that's easy to achieve, but I'm currently at Adobe HQ for the ISO meetings about ISO-32000-2, so you'll have to give me some time. – Bruno Lowagie Apr 21 '15 at 15:17
  • Of course, take all the time you need, please – Eduardo Apr 21 '15 at 15:23
  • I told you before that the size had changed, and it was true, but it had changed because the original document size was not 210 x 297. It the original size document is 210 x 297 then the new document size is not changed. Then this make me think that the content of the original document is scaled and translated to the right, leaving a vertical space at the left of the page for including the watermark. – Eduardo Apr 21 '15 at 16:07
  • In that case, you need the [ScaleDown](http://itextpdf.com/sandbox/events/ScaleDown) example. – Bruno Lowagie Apr 21 '15 at 16:09

1 Answers1

2

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

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165