title: ASP.Net C# Save PDF to directory
body: <p>I have a webpage that should work as the following:
  When a user fills the form's fields and hits submit, a PDF file will be generated for the user (filled with the info the user entered) and that PDF file gets saved in a directory.</p>
  
  <p>What I have is, when the user hits Submit the PDF gets generated for the user:</p>
  
  <pre><code>        var pdfPath = Path.Combine(Server.MapPath(path_name));
          String file_name_pdf = "Test.pdf";
          var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
          PDFHelper.ReturnPDF(pdfContents, file_name_pdf);
  </code></pre>
  
  <p>Here is my attempt for saving the PDF in the directory,</p>
  
  <pre><code>    var pdfPath = Path.Combine(Server.MapPath(path_name));
      var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
  
      string username = "Test";
      string password = "12345";      
      String file_name_pdf = "Test.pdf";
  
      var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
      WebRequest request = WebRequest.Create(Server.MapPath("~/PDF/" + pdfContents));
      request.Method = WebRequestMethods.Ftp.UploadFile;
  
      request.Credentials = new NetworkCredential(username, password);
      Stream reqStream = request.GetRequestStream();
      reqStream.Close();
  </code></pre>
  
  <p>When I ran the above I get a file: System.Byte[]
  Saved in the PDF directory, please advise what am I missing to get the generated PDF get saved in the directory.</p>
  
  <p>The code for each Method as requested:</p>
  
  <pre><code>    protected void generate_pdf()
      {
              String path_name = "~/Forms/Form1.pdf";
              var pdfPath = Path.Combine(Server.MapPath(path_name));
              String file_name_pdf = "Test.pdf";
              var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
              PDFHelper.ReturnPDF(pdfContents, file_name_pdf);
      }
  
      protected void save_pdf()
      {
             String path_name = "~/PDF/";
             var pdfPath = Path.Combine(Server.MapPath(path_name));
             var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
  
             string username = "Test";
             string password = "12345";      
             String file_name_pdf = "Test.pdf";
  
             var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
             WebRequest request = WebRequest.Create(Server.MapPath("~/PDF/" + pdfContents));
             request.Method = WebRequestMethods.Ftp.UploadFile;
  
             request.Credentials = new NetworkCredential(username, password);
             Stream reqStream = request.GetRequestStream();
             reqStream.Close();
       }
  </code></pre>
  
  <p>PDFHelper code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections;
  using System.Linq;
  using System.Web;
  using System.IO;
  using iTextSharp.text.pdf;
  
  public class PDFHelper
  {
      public static Dictionary&lt;string, string&gt; GetFormFieldNames(string pdfPath)
      {
          var fields = new Dictionary&lt;string, string&gt;();
  
          var reader = new PdfReader(pdfPath);
          foreach (DictionaryEntry entry in reader.AcroFields.Fields)
              fields.Add(entry.Key.ToString(), string.Empty);
          reader.Close();
  
          return fields;
      }
  
      public static byte[] GeneratePDF(string pdfPath, Dictionary&lt;string, string&gt; formFieldMap)
      {
          var output = new MemoryStream();
          var reader = new PdfReader(pdfPath);
          var stamper = new PdfStamper(reader, output);
          var formFields = stamper.AcroFields;
  
          foreach (var fieldName in formFieldMap.Keys)
              formFields.SetField(fieldName, formFieldMap[fieldName]);
  
          stamper.FormFlattening = true;
          stamper.Close();
          reader.Close();
  
          return output.ToArray();
      }
  
  
      public static string GetExportValue(AcroFields.Item item)
      {
          var valueDict = item.GetValue(0);
          var appearanceDict = valueDict.GetAsDict(PdfName.AP);
  
          if (appearanceDict != null)
          {
              var normalAppearances = appearanceDict.GetAsDict(PdfName.N);
               if (normalAppearances != null)
              {
                  foreach (var curKey in normalAppearances.Keys)
                      if (!PdfName.OFF.Equals(curKey))
                          return curKey.ToString().Substring(1); // string will have a leading '/' character, so remove it!
              }
          }
  
          var curVal = valueDict.GetAsName(PdfName.AS);
          if (curVal != null)
              return curVal.ToString().Substring(1);
          else
              return string.Empty;
      }
  
      public static void ReturnPDF(byte[] contents)
      {
          ReturnPDF(contents, null);
      }
  
      public static void ReturnPDF(byte[] contents, string attachmentFilename)
      {
          var response = HttpContext.Current.Response;
  
          if (!string.IsNullOrEmpty(attachmentFilename))
              response.AddHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);
  
          response.ContentType = "application/pdf";
          response.BinaryWrite(contents);
          response.End();
      }
  }
  </code></pre>