Itextsharp 7 - Scaled and Centered Image as watermark

隐身守侯 提交于 2019-12-07 08:34:27

I have adapted your example to Java, but that shouldn't matter much since it's the Math that is important:

public static final String SRC = "src/main/resources/pdfs/hello.pdf";
public static final String DEST = "results/text/watermark.pdf";
public static final String IMG = "src/main/resources/img/mascot.png";

public static void main(String[] args) throws IOException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new Watermark().createPdf(SRC, DEST);
}
public void createPdf(String src, String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(
            new PdfReader(src), new PdfWriter(dest));
    Document document = new Document(pdfDoc);
    PdfCanvas over;
    PdfExtGState gs1 = new PdfExtGState();
    gs1.setFillOpacity(0.5f);
    int n = pdfDoc.getNumberOfPages();
    Rectangle pagesize;
    ImageData img = ImageDataFactory.create(IMG);
    float iW = img.getWidth();
    float iH = img.getHeight();
    float pW, pH, sW, sH, f, x, y;

    for (int i = 1; i <= n; i++)
    {
        PdfPage pdfPage = pdfDoc.getPage(i);
        pagesize = pdfPage.getPageSize();

        pW = pagesize.getWidth();
        pH = pagesize.getHeight();
        f = (pW / iW) * 0.5f;
        sW = iW * f;
        sH = iH * f;
        x = pagesize.getLeft() + (pW / 2) - (sW / 2);
        y = pagesize.getBottom() + (pH / 2) - (sH / 2);

        over = new PdfCanvas(pdfDoc.getPage(i));
        over.saveState();
        over.setExtGState(gs1);
        over.addImage(img, sW, 0, 0, sH, x, y);
        over.restoreState();
    }
    document.close();
    pdfDoc.close();
}

The result of this code looks like this:

That looks exactly the way I expect it.

Some explanation.

  • I have an image mascot.png with dimensions iW x iH.
  • I have pages with dimensions pW x pH.
  • I want to scale the image so that it takes 50% of the width, hence I create a variable f with value 0.5f (50%) x ``(pW / iW)`.
  • I apply the factor f to the initial values of the images, resulting in the scaled dimensions sW x sH.
  • I define an offset for the image (x, y) by subtracting half of the scaled width and height of the middle of the page.

Now I have the values I need for the addImage() method: over.addImage(img, sW, 0, 0, sH, x, y);

Note: you were adding the images as an inline image. That's a bad idea because it leads to bloated PDF files, especially in the case of watermarks. By adding an image as an inline image to each page, you add the image bytes redundantly as many times as there are pages. It's much better to add the image as an Image XObject, in which case the image bytes will be added to the document only once, no matter how many times you use that same image. Please remove the true value from the parameters of the addImage() method (make a before and after PDF, and compare the file size to understand what I mean).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!