18 package org.turro.pdf;
20 import com.lowagie.text.DocumentException;
21 import com.lowagie.text.Element;
22 import com.lowagie.text.Font;
23 import com.lowagie.text.Image;
24 import com.lowagie.text.Phrase;
25 import com.lowagie.text.Rectangle;
26 import com.lowagie.text.pdf.ColumnText;
27 import com.lowagie.text.pdf.PdfContentByte;
28 import com.lowagie.text.pdf.PdfGState;
29 import com.lowagie.text.pdf.PdfReader;
30 import com.lowagie.text.pdf.PdfStamper;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
42 PdfReader reader =
new PdfReader(src);
43 int n = reader.getNumberOfPages();
44 PdfStamper stamper =
new PdfStamper(reader, dest);
45 stamper.setRotateContents(
false);
47 PdfGState gs1 =
new PdfGState();
48 gs1.setFillOpacity(0.2f);
53 String text[] = watermark.getText();
54 Phrase p[] =
new Phrase[text.length];
55 if(watermark.hasText()) {
57 Font f =
new Font(Font.HELVETICA, 30, Font.BOLD);
58 for(
int i = 0; i < text.length; i++) {
59 p[i] =
new Phrase(text[i], f);
63 float w = 0.0F, h = 0.0F;
64 if(watermark.hasImage()) {
66 img = Image.getInstance(watermark.getImage());
67 w = img.getScaledWidth();
68 h = img.getScaledHeight();
71 for (
int i = 1; i <= n; i++) {
72 pagesize = reader.getPageSize(i);
73 x = (pagesize.getLeft() + pagesize.getRight()) / 2;
74 y = (pagesize.getTop() + pagesize.getBottom()) / 2;
75 over = stamper.getOverContent(i);
79 float yp = y + ((p.length * 40) / 2);
81 ColumnText.showTextAligned(over, Element.ALIGN_CENTER, ph, x, yp, watermark.getRotation());
86 over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
void addWatermark(InputStream src, OutputStream dest, WatermarkCfg watermark)