File: number-pdf.rb

package info (click to toggle)
ruby-gnome 4.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,320 kB
  • sloc: ruby: 55,206; ansic: 29,012; xml: 333; sh: 229; cpp: 45; makefile: 42
file content (51 lines) | stat: -rwxr-xr-x 1,702 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
# Copyright (C) 2017  Ruby-GNOME2 Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# usage :
#   ruby number-pdf.rb example.pdf

require "poppler"
require "pango"

input = ARGV.first
output = input.sub(/\.pdf$/, "-numbered.pdf")

doc = Poppler::Document.new(input)
first_page = doc[0]

Cairo::PDFSurface.new(output, *first_page.size) do |surface|
  context = Cairo::Context.new(surface)

  doc.each_with_index do |page, i|
    width, height = page.size
    surface.set_size(width, height)
    context.save do
      context.render_poppler_page(page)
    end
    context.save do
      layout = context.create_pango_layout
      layout.text = i.to_s
      layout.width = width * Pango::SCALE
      layout.alignment = :center
      layout.font_description = Pango::FontDescription.new("Sans 288")
      context.move_to(0, (height - layout.pixel_size[1]) / 2.0)
      context.set_source_rgba(0.1, 0.1, 0.1, 0.5)
      context.show_pango_layout(layout)
    end
    context.show_page
  end
end