eu.mrbussy.pdfsplitter.Splitter.java Source code

Java tutorial

Introduction

Here is the source code for eu.mrbussy.pdfsplitter.Splitter.java

Source

/**
 * Split a PDF file into multiple files.
 * Copyright (C) 11 nov. 2014 R. Middel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.mrbussy.pdfsplitter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

/**
 * @author rudi
 *
 */
public class Splitter {

    /**
     * Split the given PDF file into multiple files using pages.
     * 
     * @param filename
     *            - Name of the PDF to split
     * @param useSubFolder
     *            - Use a separate folder to place the files in.
     */
    public static void Run(File file, boolean useSubFolder) {
        PdfReader reader = null;
        String format = null;

        if (useSubFolder) {
            format = "%1$s%2$s%4$s"; // Directory format
            try {
                FileUtils.forceMkdir(
                        new File(String.format(format, FilenameUtils.getFullPath(file.getAbsolutePath()),
                                FilenameUtils.getBaseName(file.getAbsolutePath()),
                                FilenameUtils.getExtension(file.getAbsolutePath()), IOUtils.DIR_SEPARATOR)));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            format = "%1$s%2$s%4$s%2$s_%%03d.%3$s"; // Directory format + filename
        } else
            format = "%1$s%2$s_%%03d.%3$s";

        String splitFile = String.format(format, FilenameUtils.getFullPath(file.getAbsolutePath()),
                FilenameUtils.getBaseName(file.getAbsolutePath()),
                FilenameUtils.getExtension(file.getAbsolutePath()), IOUtils.DIR_SEPARATOR);

        try {
            reader = new PdfReader(new FileInputStream(file));

            if (reader.getNumberOfPages() > 0) {
                for (int pageNum = 1; pageNum <= reader.getNumberOfPages(); pageNum++) {
                    System.out.println(String.format(splitFile, pageNum));
                    String filename = String.format(splitFile, pageNum);
                    Document document = new Document(reader.getPageSizeWithRotation(1));
                    PdfCopy writer = new PdfCopy(document, new FileOutputStream(filename));
                    document.open();
                    // Copy the page from the original
                    PdfImportedPage page = writer.getImportedPage(reader, pageNum);
                    writer.addPage(page);
                    document.close();
                    writer.close();
                }
            }
        } catch (Exception ex) {
            // TODO Implement exception handling
            ex.printStackTrace(System.err);
        } finally {
            if (reader != null)
                // Always close the stream
                reader.close();
        }
    }

    /**
     * Split a list of files
     * @param source
     * @param useSubFolder
     */
    public static void Run(List<File> source, boolean useSubFolder) {

        // For each file run the splitter
        for (File file : source) {
            Splitter.Run(file, useSubFolder);
        }

    }

}