PDF был разделен с использованием библиотеки PDF Box, и результирующий PDF имеет почти такой же размер, как исходный файл PDF - PullRequest
0 голосов
/ 06 февраля 2019

Я использую приведенный ниже код, чтобы разделить огромный PDF на два разных PDF.PDF расщепляется правильно.Первый PDF будет создан с первыми 2 страницами PDF, а второй PDF будет создан с остальными страницами PDF.

Проблема в размере, исходный PDF составляет 17 МБ. 2 сгенерированных PDF-файла также имеют размер 15 МБ каждый .Логически это должно быть меньше по размеру, я искал форум, они сказали, что PDFont должен использоваться должным образом.Я не использовал PDFont здесь не уверен, если я делаю это неправильно

    public static void main(String[] args) throws IOException, COSVisitorException {

            File input = new File("sourceFile.pdf");

           // pdPage and pdPage1 will be used to get first and second page of entire PDF
//pdPageMedRec will get the rest of the pages
            PDPage pdPage = null;
            PDPage pdPage1 = null;
            PDPage pdPageMedRec = null;

            PDDocument firstOutputDocument = null;
            PDDocument secondOutputDocument = null;
            PDDocument inputDocument = PDDocument.loadNonSeq(input, null);
            List<PDPage> list = inputDocument.getDocumentCatalog().getAllPages();


       // I wanted two documents to be generated from the big PDF
//firstOutputDocument  is document 1 and it will be having first 2 pages of the big pdf
//secondOutputDocument is document 2 and it will be having the rest of the pages of the PDF
            firstOutputDocument = new PDDocument();
            secondOutputDocument = new PDDocument();

// Taking first page and second page
            pdPage = list.get(0);
            pdPage1 = list.get(1);

// Appending them as one document
            firstOutputDocument.importPage(pdPage);
            firstOutputDocument.importPage(pdPage1);

// Looping the rest of the pages
            for (int page = 3; page <= inputDocument.getNumberOfPages(); ++page) {
                pdPageMedRec = (PDPage) inputDocument.getDocumentCatalog().getAllPages().get(page - 1);
                // append page to current document
                secondOutputDocument.importPage(pdPageMedRec);
            }

// Saving first document
            File f = new File("document1.pdf");
            firstOutputDocument.save(f);
            firstOutputDocument.close();

// Saving second document
            File g = new File("document2.pdf");
            secondOutputDocument.save(g);
            secondOutputDocument.close();

            inputDocument.close();
        }
...