Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
var job = await _cloudConvert.CreateJobAsync(new JobCreateRequest
{
    Tasks = new
    {
        import_first_file = new ImportUrlCreateRequest
        {
             Url = "https://mypdf1/pdf"
        },
        import_second_file = new ImportUrlCreateRequest
        {
            Url = "https://mypdf2/pdf"
        },
        merge_my_files = new MergeCreateRequest
        {
            Input = strings,
            Output_Format = CloudConvert.API.Models.Enums.MergeOutputFormat.pdf
        },
        export_it = new ExportUrlCreateRequest
        {
            Input = "merge_my_files"
        }
    }
});


Here this Tasks is of type dynamic.
If I have multiple dynamic number of pdf urls . How I can add those as a ImportUrlCreateRequest tasks as we dont have idea how may pdfs are there.

What I have tried:

I am facing difficulty to generate this Tasks and add different types of object inside that.
Posted
Updated 29-Mar-24 1:30am
v2

The easiest way for you to add an unknown number of pdf files would be to use an array or list. Inside iterate over the collection to create the value and assign them to Tasks. It doesn't have to be any more complicated than that.
 
Share this answer
 
To add to what Pete has stated above, you can loop through each URL and create an 'ImportUrlCreateRequest' task for each one, see CloudConvert API[^]

With the below code I am using a list called 'importTasks' to store all the 'ImportUrlCreateRequest tasks' dynamically, then loop through each URL in your list and create an 'ImportUrlCreateRequest' task for each URL. I then use this list to add all 'ImportUrlCreateRequest' tasks dynamically in the Tasks object when creating the job -

C#
//Create your list to store ImportUrlCreateRequest tasks...
List<object> importTasks = new List<object>();

//Your list of URL's...
List<string> pdfUrls = new List<string>
{
    "https://mypdf1/pdf",
    "https://mypdf2/pdf",
//And so on...
};

//Loop through each PDF URL and create ImportUrlCreateRequest tasks... 
foreach (string url in pdfUrls)
{
    importTasks.Add(new ImportUrlCreateRequest
    {
        Url = url
    });
}

//Now create the job with all tasks...
var job = await _cloudConvert.CreateJobAsync(new JobCreateRequest
{
    Tasks = new
    {
        import_tasks = importTasks.ToArray(),
           ImportUrlCreateRequest tasks dynamically
        merge_my_files = new MergeCreateRequest
        {
            Input = "import_tasks",
            Output_Format = CloudConvert.API.Models.Enums.MergeOutputFormat.pdf
        },
        export_it = new ExportUrlCreateRequest
        {
            Input = "merge_my_files"
        }
    }
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900