jpdfer 0.5.0-java → 0.6.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/jpdfer/pdf.rb +46 -0
  2. data/lib/jpdfer/version.rb +1 -1
  3. metadata +2 -2
data/lib/jpdfer/pdf.rb CHANGED
@@ -8,7 +8,11 @@ module Jpdfer
8
8
  class Pdf
9
9
  class NonexistentFieldError < Exception; end
10
10
  class ReadOnlyError < Exception; end
11
+ include_class Java::com.itextpdf.text.Element
12
+ include_class Java::com.itextpdf.text.Font
13
+ include_class Java::com.itextpdf.text.Font::FontFamily
11
14
  include_class Java::com.itextpdf.text.Image
15
+ include_class Java::com.itextpdf.text.Phrase
12
16
  include_class Java::java.net.URL
13
17
  include_package "com.itextpdf.text.pdf"
14
18
  include_package "com.itextpdf.text.xml.xmp"
@@ -288,6 +292,40 @@ module Jpdfer
288
292
  canvas.addImage(image, false)
289
293
  end
290
294
 
295
+ # Add watermark text to all pages at coordinates +x+ and +y+
296
+ #
297
+ #
298
+ # options:
299
+ #
300
+ # :x The placement of the watermark on the x-axis
301
+ # Default: The center of the pdf
302
+ #
303
+ # :y The placement of the watermark on the y-axis
304
+ # Default: The center of the pdf
305
+ #
306
+ # :font should be an instance of com.itextpdf.text.Font to be used for the watermark.
307
+ # Default: Helvetica Bold 132pt 0.9 Gray
308
+ #
309
+ # :rotation is an angle given in degrees that will rotate the watermark.
310
+ # Default: 45
311
+ #
312
+ # :alignment one of the com.itextpdf.text.Element alignment values
313
+ # Default: ALIGN_CENTER
314
+ def add_watermark(text, options={})
315
+ raise ReadOnlyError.new('Previously saved pdfs are read-only') if @saved
316
+
317
+ x = options[:x] || @reader.crop_box(1).width / 2
318
+ y = options[:y] || @reader.crop_box(1).height / 2
319
+ alignment = options[:alignment] || Element::ALIGN_CENTER
320
+ phrase = Phrase.new(text, options[:font] || default_watermark_font)
321
+ rotation = options[:rotation] || 45
322
+
323
+ 1.upto(@reader.getNumberOfPages).each do |page|
324
+ canvas = @stamper.getUnderContent(page)
325
+ ColumnText.showTextAligned(canvas, alignment, phrase, x, y, rotation)
326
+ end
327
+ end
328
+
291
329
  # Replaces any javascript currently attached to the page with
292
330
  # the contents of +script+
293
331
  #
@@ -306,6 +344,14 @@ module Jpdfer
306
344
  @stamper = create_stamper
307
345
  end
308
346
 
347
+ def default_watermark_font
348
+ Font.new \
349
+ FontFamily::HELVETICA,
350
+ 132,
351
+ Font::BOLD,
352
+ GrayColor.new(0.90)
353
+ end
354
+
309
355
  def create_stamper
310
356
  if @keystore
311
357
  stamper = PdfStamper.createSignature(@reader, @output_buffer.to_outputstream, 0)
@@ -1,3 +1,3 @@
1
1
  module Jpdfer
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpdfer
3
3
  version: !ruby/object:Gem::Version
4
+ version: 0.6.0
4
5
  prerelease:
5
- version: 0.5.0
6
6
  platform: java
7
7
  authors:
8
8
  - Scott Nielsen
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-22 00:00:00.000000000Z
13
+ date: 2013-05-09 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri