BrightSide Workbench Full Report + Source Code
org.turro.pdf.Watermark Class Reference

Public Member Functions

void addWatermark (InputStream src, OutputStream dest, WatermarkCfg watermark) throws IOException, DocumentException
 

Detailed Description

Author
Lluis TurrĂ³ Cutiller lluis.nosp@m.@tur.nosp@m.ro.or.nosp@m.g

Definition at line 39 of file Watermark.java.

Member Function Documentation

◆ addWatermark()

void org.turro.pdf.Watermark.addWatermark ( InputStream  src,
OutputStream  dest,
WatermarkCfg  watermark 
) throws IOException, DocumentException

Definition at line 41 of file Watermark.java.

41  {
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  }
Here is the caller graph for this function:

The documentation for this class was generated from the following file: