title: iTextSharp - Printing Merged PDF Templates
body: <p>I am using iTextSharp in asp.net (C#) in order to merge more than one PDf template. There is a functionality of print which prints only data of the template. While merging the templates the fields/Controls which are available in the template is renamed using RenameField Method of iTextSharp. 
  This implementation has broken the Print functionality. As the Print functionality has been written according to the fields.
  For merging Templates, I am using a PDFCopy.</p>
  
  <pre><code>Document document = new Document();
  bool flag = true;
  using (FileStream fileStream = File.Create(newFile))
  {
      PdfSmartCopy copy = new PdfSmartCopy(document, fileStream);
      PdfReader reader;
      MemoryStream baos;
      for (int i = 0; i &lt; loopCount; i++)
      {
          reader = new PdfReader(pdfTemplate);
          baos = new MemoryStream();
          stamper = new PdfStamper(reader, baos);
          AcroFields pdfDoc = stamper.AcroFields;
          BuildData(datarow,pdfDoc, obj)
          renameFields(reader);
           stamper.FormFlattening = false;
           stamper.Close();
          reader = new PdfReader(baos.ToArray());
          copy.AddPage(copy.GetImportedPage(reader, 1));
      }
      document.Close();
      strFileName = newFile;
  }
  
  private static void renameFields(PdfReader pdfReader)
  {
      string prepend = String.Format("_{0}", counter++);
      foreach (KeyValuePair&lt;string, AcroFields.Item&gt; de in pdfReader.AcroFields.Fields)
      {
           pdfReader.AcroFields.RenameField(de.Key.ToString(), prepend + de.Key.ToString());
      }
   }
  </code></pre>
  
  <p>Edit1: This is the solution i found in itextsharp doc,BUT it's not working
  "Using PdfCopy with documents
  that have named destinations is one of these exceptions. All annotations, such
  as link annotations, are kept with PdfCopy, but they no longer work for links to local
  named destinations. There is a workaround for this problem." </p>
  
  <pre><code>PdfReader[] readers = {
  new PdfReader(LinkActions.RESULT2),
  new PdfReader(LinkActions.RESULT1) };
  Document document = new Document();
  PdfCopy copy =
  new PdfCopy(document, new FileOutputStream(RESULT1));
  document.open();
  int n;
  for (int i = 0; i &lt; readers.length; i++) {
  readers[i].consolidateNamedDestinations();
  n = readers[i].getNumberOfPages();
  for (int page = 0; page &lt; n; ) {
  copy.addPage(copy.getImportedPage(readers[i], ++page));
  }
  } 
  </code></pre>