title: Create PDF by copying it from template with PdfCopy (lost of data)
body: <p>I'm trying to create a new pdf file based on another one using PdfCopy.
  Everything work fine during generation and the generated file can be opened without any problem on my desktop, but the file seems to be corrupted and isn't accepted by the service that I must use :</p>
  
  <blockquote>
    <p>SignService error when calling 'sign', probably caused by a bad file format.</p>
  </blockquote>
  
  <p>I noticed that the generated pdf is always ligther than the original template, so i compared the template version with the generated one. There are some big parts of missing data, especially a whole bunch of xml. I guess PdfCopy does not copying every of my original pdf but i cannot figured out what am i missing.</p>
  
  <p>here is my method :</p>
  
  <pre><code>byte[] completedDocument = null;
  string originalUri = Path.Combine(this.PdfPath, pdfName);
  string generatedUri = Path.Combine(this.PdfGeneratedPath, generatedPdfName);
  
          using(MemoryStream streamCompleted = new MemoryStream())
          {
              using(Document doc = new Document())
              {
                  PdfCopy copy = new PdfCopy(doc, streamCompleted);
  
                  copy.PdfVersion = PdfWriter.VERSION_1_6;
                  doc.Open();
                  copy.Open();
  
                  byte[] mergedDocument = null;
                  PdfReader pdfReader = new PdfReader(originalUri);
                  int pdfPageNumber = pdfReader.NumberOfPages;
  
                  using(MemoryStream streamTemplate = new MemoryStream())
                  {
                      using (PdfStamper pdfStamper = new PdfStamper(pdfReader, streamTemplate))
                      {
                          AcroFields acrofields = pdfStamper.AcroFields;
  
                          foreach (KeyValuePair&lt;string, AcroFields.Item&gt; field in acrofields.Fields)
                          {
                              string data;
                              if (pdfFieldsValues.TryGetValue(field.Key, out data))
                              {
                                  if (data == null)
                                  {
                                      data = string.Empty;
                                  }
  
                                  acrofields.SetField(field.Key, data);
                              }
                          }
  
                          pdfStamper.FormFlattening = true;
                          pdfStamper.Writer.CloseStream = false;
                      }
  
                      mergedDocument = streamTemplate.ToArray();
                  }
  
                  pdfReader = new PdfReader(mergedDocument);
  
                  for (int page = 1; page &lt;= pdfPageNumber; page++)
                  {
                      if (!excludedPages.Any(s =&gt; s == page))
                      {
                          copy.AddPage(copy.GetImportedPage(pdfReader, page));
                      }
                  }
  
                  doc.Close();
                  copy.Close();
              }
  
              completedDocument = streamCompleted.ToArray();
          }
  
          File.WriteAllBytes(generatedUri, completedDocument);
  </code></pre>
  
  <p>I tried to upload the "mergedDocument" rather than the "completedDocument" and my service accepting it, so i'm pretty sure it has something to do with this part :</p>
  
  <pre><code>for (int page = 1; page &lt;= pdfPageNumber; page++)
  {
      if (!excludedPages.Any(s =&gt; s == page))
      {
          copy.AddPage(copy.GetImportedPage(pdfReader, page));
      }
  }
  </code></pre>
  
  <p>Or pdfCopy init</p>