title: iText: Can PdfTemplate of one Document object be used in another Document Object?
body: <p>I am using iText2.1.2 and my requirement is to use the template created from Document1 object onto Document2 object. </p>
  
  <pre><code>import com.lowagie.text.Document;
  import com.lowagie.text.DocumentException;
  import com.lowagie.text.pdf.PdfContentByte;
  import com.lowagie.text.pdf.PdfTemplate;
  import com.lowagie.text.pdf.PdfWriter;
  
  import java.awt.Graphics2D;
  import java.awt.geom.Line2D;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  
  public class TestITextTemplate {
  
  public TestITextTemplate() {
      super();
  }
  
  public static final String DEST = "D:\\Doc1.pdf";
  public static final String DEST1 = "D:\\Doc2.pdf";
  
  public static void main(String[] args) throws IOException, DocumentException   {
      File file = new File(DEST);
      file.getParentFile().mkdirs();
      new TestITextTemplate().createPdf();
  }
  
  public void createPdf() {
      try {
          //1st Document Object.
          Document document = new Document();
          FileOutputStream fos = new FileOutputStream(DEST);
          PdfWriter writer = PdfWriter.getInstance(document, fos);
          document.open();
          PdfContentByte contentByte = writer.getDirectContent();
          PdfTemplate template =
              contentByte.createTemplate(document.getPageSize().getWidth(), document.getPageSize().getHeight());
          Graphics2D g2 =
              template.createGraphicsShapes(document.getPageSize().getWidth(), document.getPageSize().getHeight());
          template.setLineWidth(Float.parseFloat(2 + "") * 0.72f);
          g2.drawString("Test String", 100, 120);
          g2.dispose();
  
          contentByte.addTemplate(template, 0, 0);
          writer.releaseTemplate(template);
  
          document.close();
  
          //2nd Document Object.
          Document document1 = new Document();
          FileOutputStream fos1 = new FileOutputStream(DEST1);
          PdfWriter writer1 = PdfWriter.getInstance(document1, fos1);
          document1.open();
          PdfContentByte contentByte1 = writer1.getDirectContent();
          PdfTemplate template1 =
              contentByte1.createTemplate(document1.getPageSize().getWidth(), document1.getPageSize().getHeight());
          Graphics2D g21 =
              template1.createGraphicsShapes(document1.getPageSize().getWidth(), document1.getPageSize().getHeight());
          template1.setLineWidth(Float.parseFloat(1 + "") * 0.72f);
          g21.draw(new Line2D.Double(0, 120, 400, 120));
          g21.dispose();
  
          contentByte1.addTemplate(template, 0, 0);//Adding template of 1st Document object to 2nd Document object.
          contentByte1.addTemplate(template1, 50, 50); 
  
          writer1.releaseTemplate(template);
          writer1.releaseTemplate(template1);
  
          document1.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>When the contents of the <strong>template1</strong> is added after adding the <strong>template</strong> to the <strong>contentByte1</strong>, the contents of <strong>template</strong> is being overwritten by <strong>template1</strong>. </p>
  
  <p>Is this not possible? How should this work ideally? </p>
  
  <p>Thanks in advance.</p>