Getting size of pdf before merging them

How to get an PDF size before merging them. We have a size limitation to work on and to achieve this we we need to check PDF size before and after merging. We are suing PDf 20.6 version and Java.

@venkata.veerubhotla

There is no direct support available in API.You can use following sample code on your end using System.IO namespace to get the file size.

System.IO.FileInfo file = new System.IO.FileInfo("input.pdf");
long FileSize = file.Length;
long Threshold = 5;//MB

// Compare the FileSize with MBs
if ((FileSize / (1024 * 1024)) < Threshold)
{
 //Perform your logic
}

@venkata.veerubhotla

The equivalent Java implementation for the same is:

public static void GetFileSize(String fileName)
{
    Path path = Paths.get(fileName);
    // size of a file (in bytes)
      long FileSize;
   try 
    {
        FileSize = Files.size(path);
      
        long Threshold = 5;//MB

        // Compare the FileSize with MBs
        if ((FileSize / (1024 * 1024)) < Threshold)
        {
         //Perform your logic
        }
      
    } 
    catch (IOException ex) 
    {
        ex.printStackTrace();
    }
}

So each time, we have to save it to a local drive before checking the size?
lets say we have 10 pdf’s to be merged - then each time we cannot save and check the size. Any other easy way?

@venkata.veerubhotla

I am afraid this is the solution and you will anyways be holding PDF file in memory to get the size.