2
votes

I have a business requirement that requires me to splits pdfs into multiple documents. Lets say I have a 100MB pdf, I need to split that into for simplicity sake, into multiple pdfs no larger than 10MB a piece.

I am using iText.

I am going to get the original pdf, and loop through the pages, but how can I determine the file size of each page without writing it separately to the disk?

Sample code for simplicity


int numPages = reader.getNumberOfPages();

PdfImportedPage page;
for (int currentPage = 0; currentPage &lt numPages; ){
    ++currentPage;
    //Get page from reader
    page = writer.getImportedPage(reader, currentPage);

// I need the size in bytes here of the page

}   

1

1 Answers

1
votes

I think the easiest way is to write it to the disk and delete it afterwards:

Document document = new Document();
File f= new File("C:\\delete.pdf");  //for instance
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
document.open();
document.add(page);
document.close();
long filesize = f.length();   //this is the filesize in byte
f.delete();

I'm not absolutely sure, I admit, but I don't know how it should be possible to figure out the filesize if the file is not existing.