Free Support Forum - groupdocs.cloud

Batch Conversion pdf to doc

Hi,

do you have a code sample for batch conversion? I have a directory containing a number of pdf and would like to convert all of them. Your example code is for one file at the time.

thank you
vittorio

@Vittorio12

Thanks for your inquiry. You can achieve your requirement by processing files in a loop. Please iterate through the directory, upload the PDF documents to storage and convert. Here is a sample code, you can improve/modify it as per your requirements.

// Create necessary API instances
var configuration = new Configuration(MyAppSid, MyAppKey);
var fileApi = new FileApi(configuration);            
var convertApi = new ConvertApi(configuration);

var pathToSourceFiles = @"C:/Temp/input/";            
var format = "docx";
var folder = "Temp";

DirectoryInfo dir = new DirectoryInfo(pathToSourceFiles);
System.IO.FileInfo[] files = dir.GetFiles();
foreach (System.IO.FileInfo file in files)
{
    // upload file to storage
    var requestUpload = new UploadFileRequest(folder+"/"+file.ToString(), System.IO.File.OpenRead(file.FullName), null);
    var responseUpload = fileApi.UploadFile(requestUpload);
                
    // Prepare convert settings
    var settings = new ConvertSettings
    {
        FilePath = folder + "/" + file.ToString(),
        Format = format,
        OutputPath = "converted"
    };

    // Convert to specified format
    var response = convertApi.ConvertDocument(new ConvertDocumentRequest(settings));
}