简体   繁体   中英

Splitting Pdf to multiple smaller Pdf - limit with the file-size

The below code does the splitting into smaller pdf files. However, the last pdf file it create gets corrupted. If anyone is able to help with this, where I am doing wrong?

PdfReader Split_PDF_By_Size = new PdfReader("/tutorial.pdf"); Document document = new Document();

        PdfCopy copy = new PdfCopy(document, new FileOutputStream("/File1.pdf"));
        document.open();

        int number_of_pages = Split_PDF_By_Size.getNumberOfPages();
        int pageNumber = 1; /* To generate file name dynamically */
        // int Find_PDF_Size; /* To get PDF size in bytes */
        float combinedSize = 0; /* To convert this to Kilobytes and estimate new PDF size */

        for (int i = 1; i < number_of_pages; i++ ) {
            float Find_PDF_Size;

            if (combinedSize == 0 && i != 1) {
                document = new Document();
                pageNumber++;
                String FileName = "File" + pageNumber + ".pdf";
                copy = new PdfCopy(document, new FileOutputStream(FileName));
                document.open();
            }

            copy.addPage(copy.getImportedPage(Split_PDF_By_Size, i));

            Find_PDF_Size = copy.getCurrentDocumentSize();

            combinedSize = (float)Find_PDF_Size / 1024;

            if (combinedSize < 250 ||  i == number_of_pages) {
                document.close();
                combinedSize = 0;
            } 
        }

        System.out.println("PDF Split By Size Completed. Number of Documents Created:" + pageNumber);
    }
    catch (Exception i)
    {
        System.out.println(i);
    }

This code works fine - with splitting large PDF into multiple smaller size pdf [Using file size limitation]

PdfReader Split_PDF_By_Size = new PdfReader("Test_page.pdf"); Document document = new Document();

        PdfCopy copy = new PdfCopy(document, new FileOutputStream("File1.pdf"));
        document.open();

        int number_of_pages = Split_PDF_By_Size.getNumberOfPages();
        int pageNumber = 1; /* To generate file name dynamically */
        // int Find_PDF_Size; /* To get PDF size in bytes */
        /* To convert this to Kilobytes and estimate new PDF size */
        float combinedSize = 0;

        for (int i = 1; i <= number_of_pages; i++ ) {
            float Find_PDF_Size;

            if (combinedSize == 0 && i != 1) {
                document = new Document();
                pageNumber++;
                String FileName = "File" + pageNumber + ".pdf";
                copy = new PdfCopy(document, new FileOutputStream(FileName));
                document.open();
            }

            copy.addPage(copy.getImportedPage(Split_PDF_By_Size, i));

            Find_PDF_Size = copy.getCurrentDocumentSize();

            combinedSize = (float)Find_PDF_Size / 1024;

            if (combinedSize > 300) {
                document.close();
                combinedSize = 0;
            }
        }

        document.close();
        System.out.println("Number of Documents Created:" + pageNumber);
    }
    catch (Exception i)
    {
        System.out.println(i);
    }

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