使用PDF Box库对PDF进行了拆分,生成的PDF的大小与源PDF文件的大小几乎相同

时间:2019-02-06 10:08:30

标签: java pdf pdfbox

我正在使用以下代码将巨大的PDF分为两个不同的PDF。 PDF正确拆分。第一个PDF将由前两页PDF生成,第二个PDF将与其余PDF页一起生成。

问题在于大小,源PDF为17 MB。 所生成的2个PDF也各有15MB 。从逻辑上讲,它的大小应该较小,我在论坛上搜索,他们说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();
        }

0 个答案:

没有答案