Join/Split PDF based on Size

Hi I have PDF files in a folder that I want to join first based on the criteria on (10MB each).
What I am trying to achieve is if the file size reaches 10mb then it should create another file with the same name but add a counter to it? Please can anyone help? or if you have a sample workflow that would work for me too.

Eg: of filename -
1.) abc.pdf
2.)abc_v1.pdf
Note: Some of my pdf files have attachments so I can not use merge pdf activity.

1 Like

Hi
First let’s get the filepath of all files in a folder
—now use a ASSIGN activity like this
arr_filepath = Directory.GetFiles(“yourfolderpath”)

Where arr_filepath is a variable of type array of string

—now use a FOR EACH activity and pass the above variable as input and change the type argument as string

—inside the loop use a IF condition like this
New System.IO.FilePath(item.ToStrings).Length > 10000000

If true it will go to THEN part where we can use Copy File activity and mention the filepath with the name we want in destination property

Cheers @Anshuman_Malik

1 Like

Hi @Palaniyappan,

Thank you for that, however my task is a bit different. See screenshot attached. Based on the file size I trying to join all the pdf’s together which are less than 10mb (i.e file number 1, 2 and 3) and then have another file (file 4 in my case) since it is almost 10mb.

I have to upload these files to a portal in batches and the limitation is 10mb per file.

@Palaniyappan

@ClaytonM

I am actually doing the same thing today, with a limit of 50MB.

I can get back to you how to do this when I have time.

Regards.

THanks @ClaytonM, I am stuck at this loop and I have to get this done by Tuesday if you get a chance please attach your workfile.

but do you want to put the PDFs together in like a zip file or really join to have only one PDF with the content of others until you have groups of 10MB?

I’m assuming he just needs to loop the upload process in groups. Like there is probably an add button to select file(s). He would just need to make an array of the files grouped by file size, then run that with the ForEach to call the upload part inside the loop.

But, if he wants to merge the groups to a pdf, then he would need to do the same thing, but would just need to either merge or zip the groups of files.

i guess i dont have so much creativity to understand some topics :slight_smile: but that would be kind of easy to do creating a string list and adding file by file checking total size until reachs around 10MB…

@ClaytonM: you are absolutely correct. This is exactly what I want but instead of uploading I want to join those files.

The portion I am stuck with is building that logic/loop to identify the group of files.

@bcorrea: You are also correct it your logic. Fact being I am stuck on that logic building part.

Hey @Anshuman_Malik

Maybe you can make use of the C# code from this post - invoke in UiPath or by using the corresponding activities:

It will output the combinations from the array that sum to a predefined target. The array may store pdf file sizes, in your case, and the target - 10mb.

Then loop by decreasing the target - not very likely to reach exactly 10 mb.

thank you @alin.c.mihalea but this makes combinations of files but I need unique files. So does not help me much. Also, I am not well versed with coding.

Another idea:

  1. Store file sizes in an array

  2. Implement a function to compute every possible sum of combinations from that array

For example:
{1,2,3} => sums: 1, 2, 3, 1+2, 1+3, 2+3, 1+2+3

  1. Check which sum is closest to 10mb and join those files. (Compute difference: 10mb - sum)

  2. Recreate the array without the files from Step 3.

  3. Step 2 again.

And so on.

THank you let me see if this works for me or not.

@ClaytonM @Palaniyappanany any luck?

@aksh1yadav please can you help?

Can you be more specific in what do you need to do when you find files that sum 10MB? is it to zip them together or what?

get all the file names in a string which I can then pass to Join PDF activity. I am not able to figure out this part