BrightSide Workbench Full Report + Source Code
Watermark.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.pdf;
19 
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;
34 
39 public class Watermark {
40 
41  public void addWatermark(InputStream src, OutputStream dest, WatermarkCfg watermark) throws IOException, DocumentException {
42  PdfReader reader = new PdfReader(src);
43  int n = reader.getNumberOfPages();
44  PdfStamper stamper = new PdfStamper(reader, dest);
45  stamper.setRotateContents(false);
46  // transparency
47  PdfGState gs1 = new PdfGState();
48  gs1.setFillOpacity(0.2f);
49  // properties
50  PdfContentByte over;
51  Rectangle pagesize;
52  float x, y;
53  String text[] = watermark.getText();
54  Phrase p[] = new Phrase[text.length];
55  if(watermark.hasText()) {
56  // text watermark
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);
60  }
61  }
62  Image img = null;
63  float w = 0.0F, h = 0.0F;
64  if(watermark.hasImage()) {
65  // image watermark
66  img = Image.getInstance(watermark.getImage());
67  w = img.getScaledWidth();
68  h = img.getScaledHeight();
69  }
70  // loop over every page
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);
76  over.saveState();
77  over.setGState(gs1);
78  if(p.length > 0) {
79  float yp = y + ((p.length * 40) / 2);
80  for(Phrase ph : p) {
81  ColumnText.showTextAligned(over, Element.ALIGN_CENTER, ph, x, yp, watermark.getRotation());
82  yp -= 40;
83  }
84  }
85  if(img != null) {
86  over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
87  }
88  over.restoreState();
89  }
90  stamper.close();
91  reader.close();
92  }
93 
94 }
void addWatermark(InputStream src, OutputStream dest, WatermarkCfg watermark)
Definition: Watermark.java:41