How to separate pdf based on given intervals

Viewed 259

My main intention is my code should take pdf and intervals as input,I'm taking sample inputs as 2,6, where my program should divide pdf into 3parts i.e 1,2 pages as 1pdf. 3,5,6 as other pdf and remaining pages into one pdf(if there is any extra page).I'm not getting the required output!! this is code I've written

import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PDFSplitter {

    public List<String> split(String fileName, String password, int[] splitIndices) throws IOException {

        //Loading an existing PDF document
        File file = new File(fileName);
        PDDocument document = null;
        if (password != null && !password.trim().equals("")) {
            document = PDDocument.load(file, password);
        } else {
            document = PDDocument.load(file);
        }

        //Instantiating Splitter class

        //splitting the pages of a PDF document

        List<PDDocument> splitDocs = new ArrayList<PDDocument>();
        int startPage = 0;
        for (int splitIdex : splitIndices) {
            Splitter splitter = new Splitter();
            splitter.setStartPage(startPage);
            splitter.setSplitAtPage(splitIdex +startPage);
            splitter.setEndPage(splitIdex+1);
            List<PDDocument> documents = splitter.split(document);
            splitDocs.addAll(documents);
            startPage = splitIdex + 1;
        }
        if(startPage <= document.getNumberOfPages())
        {
            Splitter splitter = new Splitter();
            splitter.setStartPage(startPage);
            splitter.setSplitAtPage(document.getNumberOfPages() - startPage);
            splitter.setEndPage(document.getNumberOfPages());
            List<PDDocument> documents = splitter.split(document);
            splitDocs.addAll(documents);
        }

        List<String> splitFileNames = new ArrayList<String>();
        for (PDDocument splitDoc : splitDocs) {
            String fileName1 = fileName.substring(0, fileName.indexOf(".PDF")) + splitDocs.indexOf(splitDoc) + ".pdf";
            splitDoc.save(fileName1);
            splitFileNames.add(fileName1);
        }
        document.close();
        return splitFileNames;
    }

    public static void main(String[] args) throws IOException {
        PDFSplitter splitter = new PDFSplitter();
        int[] pages = {3,5};
        List<String> splitFileNames = splitter.split("C:\\Users\\RSk\\Desktop\\rsk.pdf","", pages);

        System.out.println("splitFileNames = " + splitFileNames);
    }
}
1 Answers

My main intention is my code should take pdf and intervals as input, I'm taking sample inputs as 2,6, where my program should divide pdf into 3 parts, i.e. 1,2 pages as one pdf. 3,4,5,6 as other pdf and remaining pages into one pdf (if there is any extra page).

This is easiest to do by customizing the Splitter by overriding the splitAtPage method:

public class CustomSplitter extends Splitter {
    public CustomSplitter(int[] splitIndices) {
        this.splitIndices = splitIndices;
    }

    @Override
    protected boolean splitAtPage(int pageNumber) {
        return Arrays.binarySearch(splitIndices, pageNumber) >= 0;
    }

    final int[] splitIndices;
}

(CustomSplitter class)

Now you can split a document at given pages like this:

PDDocument document = PDDocument.load(SOURCE);
Splitter splitter = new CustomSplitter(new int[] {2,6});

List<PDDocument> documents = splitter.split(document);

for (int i=0; i < documents.size(); i++) {
    documents.get(i).save(String.format("result-%d.pdf", i));
}

(TestCustomSplitter test testSplitForSaiKrishna)

Related