简体   繁体   中英

iText Pdf Page Byte Size

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 < numPages; ){
    ++currentPage;
    //Get page from reader
    page = writer.getImportedPage(reader, currentPage);

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

}   

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM