title: C# iTextSharp - Code overwriting instead of appending pages
body: <p>I've seen many posts that have helped me get to where I am, I'm new to programming. My intention is to get the files within the directory "sourceDir" and look for a Regex Match. When it finds a Match, I want to create a new file with the Match as the name. If the code finds another file with the same Match (the file already exists) then create a new page within that document. </p>
  
  <p>Right now the code works, however instead of adding a new page, it overwrites the first page of the document. NOTE: Every document in the directory is only one page!</p>
  
  <pre><code>string sourceDir = @"C:\Users\bob\Desktop\results\";
  string destDir = @"C:\Users\bob\Desktop\results\final\";
  string[] files = Directory.GetFiles(sourceDir);
  foreach (string file in files)
      {
         using (var pdfReader = new PdfReader(file.ToString()))
              {
                  for (int page = 1; page &lt;= pdfReader.NumberOfPages; page++)
                  {
                      var text = new StringBuilder();
  
                      ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                      var currentText = 
                      PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
  
                      currentText = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                      text.Append(currentText);
  
                      Regex reg = new Regex(@"ABCDEFG");
                      MatchCollection matches = reg.Matches(currentText);
  
                      foreach (Match m in matches)
                      {
                          string newFile = destDir + m.ToString() + ".pdf";
  
                          if (!File.Exists(newFile))
                          {
                              using (PdfReader reader = new PdfReader(File.ReadAllBytes(file)))
                              {
                                  using (Document doc = new Document(reader.GetPageSizeWithRotation(page)))
                                  {
                                      using (PdfCopy copy = new PdfCopy(doc, new FileStream(newFile, FileMode.Create)))
                                      {
                                          var importedPage = copy.GetImportedPage(reader, page);
                                          doc.Open();
                                          copy.AddPage(importedPage);
                                          doc.Close();
                                      }
                                  }
                              }
                          }
                          else
                          {
                              using (PdfReader reader = new PdfReader(File.ReadAllBytes(newFile)))
                              {
                                  using (Document doc = new Document(reader.GetPageSizeWithRotation(page)))
                                  {
                                      using (PdfCopy copy = new PdfCopy(doc, new FileStream(newFile, FileMode.OpenOrCreate)))
                                      {
                                          var importedPage = copy.GetImportedPage(reader, page);
                                          doc.Open();
                                          copy.AddPage(importedPage);
                                          doc.Close();
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          }
  </code></pre>