title: iText: add SVG tile as page to new PDF file
body: <p>I'm trying to create a new PDF file using iText, and the new file should be composed of pages that are generated from an area within an SVG file (tile). And each generated page needs to have a 'drawing border' superimposed upon it, so that it appears as the top layer of each generated page. I think the superimposing can be achieved with the iText stamper, but examples I see work only with an existing PDF, which I don't have as I want to do all this on-the-fly to reduce the time required to generate the resulting PDF. I have a one page PDF file that represents the PDF layer to superimpose upon each page generated in the PDF.</p>
  
  <p>I found an example on the net that divides the SVG canvas into tiles and creates separate PDF files from each tile. However, creating intermediate PDF files wastes time, as I need to read them in again to create the file I am trying to create (with the superimposed layer for each page).</p>
  
  <p>I don't want to dump loads of code here, but here is the code sample that I used to generate one page PDF files for each SVG tile:
  <a href="http://people.apache.org/~clay/batik/rasterizerTutorial.pdf" rel="nofollow">http://people.apache.org/~clay/batik/rasterizerTutorial.pdf</a>  (see page 5: SaveAsJPEGTiles.java)</p>
  
  <p>As I am an iText newbie, maybe someone here has achieved something similar, or knows how to do this?</p>
  
  <p>Thanks.</p>
  
  <p>I'm using a ByteArrayOutputStream instead of the FileOutputStream, but when I try to combine this with iText's Document type, I keep getting an exception saying no pages in document - see code below. I think I need to add the ByteArrayOutputStream for each tile to the iText document so that it creates a new page for each tile, but I can't see how to do that from the iText API. Here's my code so far, but doesn't quite work:</p>
  
  <pre><code>package examples.aoi;
  
  import java.io.*;
  import java.awt.*;
  import org.apache.batik.transcoder.TranscoderInput;
  import org.apache.batik.transcoder.TranscoderOutput;
  import org.apache.fop.svg.PDFTranscoder;
  
  import com.itextpdf.text.Document;
  import com.itextpdf.text.PageSize;
  import com.itextpdf.text.pdf.PdfWriter;
  
  public class FromCanvasToPDFPages {
  
      PDFTranscoder trans = new PDFTranscoder();
  
      public FromCanvasToPDFPages() {
      }
  
      public void tile(TranscoderInput input, TranscoderOutput output, Rectangle aoi) throws Exception {
  
          trans.addTranscodingHint(PDFTranscoder.KEY_WIDTH, new Float(aoi.width));
          trans.addTranscodingHint(PDFTranscoder.KEY_HEIGHT, new Float(aoi.height));
          trans.addTranscodingHint(PDFTranscoder.KEY_AOI, aoi); 
  
          trans.transcode(input, output);
  
      }
  
      public static void main(String [] args) throws Exception {
  
          FromCanvasToPDFPages p = new FromCanvasToPDFPages();
  
          String in = "C:\\Documents and Settings/krell/My Documents/1.svg";
          String svgURI = new File(in).toURL().toString();
          TranscoderInput input = new TranscoderInput(svgURI);
  
          String outputFilename = "canvasPages.pdf";
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          TranscoderOutput output = new TranscoderOutput(baos);
  
          int A4_landscape_width = 1103; // use proper dimensions from class when found
          int A4_landscape_height = 780; // use proper dimensions from class when found
  
          int documentWidth = 3309;     // read this directly from canvas file (svg)
          int documentHeight = 1560;    // read this directly from canvas file (svg)
  
          int columns = documentWidth / A4_landscape_width;
          int rows = documentHeight / A4_landscape_height;
  
          int tile_width = A4_landscape_width;
          int tile_height = A4_landscape_height;
  
          System.err.println("&gt;&gt;&gt; Canvas document width = "+documentWidth);
          System.err.println("&gt;&gt;&gt; Canvas document height = "+documentHeight);
  
          System.err.println("&gt;&gt;&gt; A4 width = "+A4_landscape_width);
          System.err.println("&gt;&gt;&gt; A4 height = "+A4_landscape_height);
  
          System.err.println("&gt;&gt;&gt; canvas columns = "+columns);
          System.err.println("&gt;&gt;&gt; canvas rows = "+rows);
  
          System.err.println("&gt;&gt;&gt; tile_width = "+tile_width);
          System.err.println("&gt;&gt;&gt; tile_height = "+tile_height);
  
  
          // Create A4 document to write tile data into
          Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
          PdfWriter writer = PdfWriter.getInstance(document, baos);
  
          // open the document
          document.open();
  
          // now process each tile, row by row, column by column until done
          for (int row = 0; row &lt; rows; row++) {
              for (int column = 0; column &lt; columns; column++) {
                  // get tile coordinates
                  Rectangle tile = new Rectangle(column*tile_width, row*tile_height, (column+1)*tile_width, (row+1)*tile_height);
                  p.tile(input, output, tile);
              }
          }
  
          // now write the baos to disk:
          OutputStream fileOutputStream = new FileOutputStream(outputFilename);
          baos.writeTo(fileOutputStream);
  
          // close stuff
          fileOutputStream.flush();
          fileOutputStream.close();
          document.close();
  
          baos.close();
  
          System.exit(0);
      }
  
  }
  </code></pre>