简体   繁体   中英

C#: Saving iText7 PDFs into a folder chosen by the user with a dialog

I would make a simple program in C# with Windows forms, which gets some data given by the user thanks to some textboxes, and when He presses a button, a dialog (I don't know which one) is displayed, in order to explore the pc folders and choose a destination for saving it there.

Well, I used a FolderBrowserDialog (I don't know if that's the right one for the purpose), but there's a problem: in order to store a PDF with itext7, I have to give an Environment.SpecialFolder variable, while the method selectedPath() to get the user path of the formBrowserDialog returns a string.

I tried to convert the string into Environment.SpecialFolder in some way, but I always get a System.ArgumentException

Here's my code:

string name = txtName.Text;
//
//bla bla bla getting the parameters given by the user
//...

string pdfName = surname+ " - " + hours + "ː" + minutes + ".pdf";

string folder="";
                 
//"fbd" is the FolderBrowserDialog
if (fbd.ShowDialog() == DialogResult.OK)
{
    //here I get the folder path (I hope I've chosen the right dialog for this scope, which is a FolderBrowserDialog)
    folder = fbd.SelectedPath;

     //starting my pdf generation
     //here is my attempt to write something in order to parse the path string into an Environment.SpecialFolder type, to use it as a parameter in getFolderPath()
     Environment.SpecialFolder path = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), folder);

      //here it's supposed to give to the GetFolderPath method the Environment.SpecialFolder type.
      var exportFolder = Environment.GetFolderPath(path);  //ON THIS LINE  I GET THE EXCEPTION


      var exportFile = System.IO.Path.Combine(exportFolder, pdfName);
      using (var writer = new PdfWriter(exportFile))
      {
          using (var pdf = new PdfDocument(writer))
          {
               var doc = new Document(pdf);
               doc.Add(new Paragraph("
                         //bla bla bla writing my things on it
                          "));
          }
       }
      //pdf creation ends
}

To simplify all of this, you don't need to Environment.SpecialFolder variable at all, nor do you need to pass it as a parameter.

The reason that an exception was thrown is because you tried to parse a string into an Environment.SpecialFolder variable enum , when the string could not be parsed into one.

You can look here to see the list of enums included. I would wager that the specific path you selected matches none of those.

Here's what your code is currently doing:

  1. Selecting a path
  2. Trying to parse that path to get an enum for a special folder
  3. Trying to get the string associated with that Environment.SpecialFolder variable (so if you had actually been able to parse it, you would've ended up with just what you started with)
  4. Combining that string with the name you wanted to give the PDF.

You can simplify all of this by omitting steps 2 and 3, which cause the error.

string pdfName = surname+ " - " + hours + "ː" + minutes + ".pdf";
 
//You select the folder here
if (fbd.ShowDialog() == DialogResult.OK)
{ 
     string folder = fbd.SelectedPath;

     //combine the path of the folder with the pdf name
     string exportFile = System.IO.Path.Combine(folder, pdfName);   
  
     using (var writer = new PdfWriter(exportFile))
     {
          using (var pdf = new PdfDocument(writer))
          {
               var doc = new Document(pdf);
               doc.Add(new Paragraph("//bla bla bla writing my things on it"));
          }
     }

     //Pdf creation ends
}
     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM