title: Watermarking with Prawn (using templates)
body: <p>I need to be able to watermark a document that was created from a template. I have the following code right now:</p>
  
  <pre><code># Note: the raw PDF text (body variable below) is sent from a remote server.
  Prawn::Document.new(:template =&gt; StringIO.new(body), :page_size =&gt;
  'A4') do |document|
    # ... including other pages and sections to the template here ...
  
    # watermark
    d.page_count.times do |i|
      d.go_to_page i
      d.stroke_line [d.bounds.left, d.bounds.bottom], [d.bounds.right, d.bounds.top]
      d.draw_text "Watermark", :rotate =&gt; 45, :at =&gt; [100,100], :size =&gt; 100
    end
  end
  </code></pre>
  
  <p>This is ignoring the templated pages for some reason that I can't comprehend. Now here's where the plot thickens: if the server adds a watermark, then this code will work as expected (e.g. straight Ruby code = no overlaying text on the non-prawn-generated pages, yet watermarking works on a pre-watermarked template). My only guess is there is some way to create a z-index/layer that the server is doing but Prawn itself cannot.</p>
  
  <p>Here is a portion of the code from the server that does the PDF
  generation itself, this does the watermarking using iText:</p>
  
  <pre><code>PdfStamper stamper = new PdfStamper(...);
  PdfContentByte over = stamper.GetOverContent(i + 1);
  over.BeginText();
  over.SetTextMatrix(20, 40);
  over.SetFontAndSize(bf, 20);
  over.SetColorFill(new Color(97, 150, 58));
  over.ShowTextAligned(Element.ALIGN_CENTER,
                       watermarkText,
                       document.PageSize.Width / 2,
                       document.PageSize.Height / 2,
                       55);
  over.EndText();
  over.Stroke();
  </code></pre>
  
  <p>If that runs before I use the raw data in Prawn I can watermark, go
  figure.</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>Anyone know how I can achieve the same effect using prawn instead
  of a hybrid? I'd rather handle the watermarking locally. </p></li>
  <li><p>Is there a basic equivalent to <code>GetOverContent()</code> in Prawn?</p></li>
  <li><p>Is there a better way to get a string of raw PDF data into Prawn
  without using <code>:template</code> and <code>StringIO</code>? (I saw the <code>#add_content</code> method
  but that didn't work)</p></li>
  </ol>
  
  <hr>
  
  <p>TL;DR: I need to float text above the Prawn templated text a la
  watermarking the document.</p>
  
  <p>Any insight or paths I can research will be appreciated. If this
  makes no sense I can clarify.</p>