How to convert a batch of files PDF to RTF within a folder using C#

Hello all,

The company I work at are looking at purchasing this software, and currently, I’m tasked with building a very basic piece of conversion software for a proof of concept prototype. The examples I’ve found all deal with converting one SPECIFIC file i.e. NameOfFile.pdf.

I’m attempting to build this using .NET (C#) using .NET 4.5 framework.

I’ve looked through the documentation, but can’t seem to find the syntax that would allow me to perform a batch conversion on any files within a folder that is of PDF format.

I also can’t find a topic similar to mine.

Here is a snippet of my code below:

            ConversionConfig config = new ConversionConfig();
        config.StoragePath = @"\\TEST\FileLocation\";
        ConversionHandler conversionHandler = new ConversionHandler(config);
        var convertedDocumentPath = conversionHandler.Convert("OpNote.pdf", new WordsSaveOptions { });
        convertedDocumentPath.Save(files[i].ToString() + ".rtf");

This is probably a really easy issue to resolve, but my lack of knowledge about the product is hindering progress with my app.

All advice and help is appreciated.

Thanks,
Josh

@JoshWigley,

Thank you for taking interest in GroupDocs.Conversion for .NET and posting your concerns.

If you look into the following code:

var convertedDocumentPath = conversionHandler.Convert("OpNote.pdf", new WordsSaveOptions { });

The Convert method takes two arguments. Convert(String, SaveOptions) file name and save options.
Hence, it would certainly convert a single file or batch of files specified by the guid. In your case, if you manage to get names of the files within a folder that are of PDF format. You can then pass those names one by one to the Convert method in order to do batch conversion.
This is how you can implement your use-case:

  • Specify the storage path and get all PDF files within

       string path = "YOUR_STORAGE_PATH";
       string[] pdfFiles = Directory.GetFiles(path, "*.pdf").Select(Path.GetFileName)                                     .ToArray();
    
  • Set save options as per your needs

      var saveOptions = new WordsSaveOptions
      {
            ConvertFileType = WordsSaveOptions.WordsFileType.Rtf,
      };
    
  • Convert and save files one by one

      foreach (var item in pdfFiles)
      {
            var convertedDocumentPath = conversionHandler.Convert(item, saveOptions);
            convertedDocumentPath.Save(Path.Combine(OUTPUT_PATH, "result-" + Path.GetFileNameWithoutExtension(item) + ".rtf"));
      }
    

Hopefully it will meet your requirements. If you face any further issue, do let us know.