eu.mrbussy.pdfsplitter.Application.java Source code

Java tutorial

Introduction

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

Source

/**
 * Split a PDF file into multiple files.
 * Copyright (C) 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.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
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;

import eu.mrbussy.pdfsplitter.ui.MainWindow;

/**
 * @author Rudi
 * 
 */
public class Application {

    private final static String CONFIGURATION_FILE = "application.properties";
    public final static String NAME = "PDFSplitter";
    public final static String VERSION = "0.1";
    public final static String[] AUTHORS = { "R. Middel" };

    /**
     * Arguments that are parsed
     */
    private static CommandLine arguments;
    /**
     * The options that are available as arguments
     */
    private static Options options;

    public static PropertiesConfiguration Configuration;

    /**
     * Start the main program.
     * 
     * @param args
     *            - Arguments passed on to the program
     */
    public static void main(String[] args) {

        // Read configurations
        try {
            String configDirname = FilenameUtils.concat(System.getProperty("user.home"),
                    String.format(".%1$s%2$s", NAME, IOUtils.DIR_SEPARATOR));
            String filename = FilenameUtils.concat(configDirname, CONFIGURATION_FILE);

            // Check to see if the directory exists and the file can be created/opened
            File configDir = new File(configDirname);
            if (!configDir.exists())
                configDir.mkdir();

            // Check to see if the file exists. If not create it
            File file = new File(filename);
            if (!file.exists()) {
                file.createNewFile();
            }
            Configuration = new PropertiesConfiguration(file);
            // Automatically store the settings that change
            Configuration.setAutoSave(true);

        } catch (ConfigurationException | IOException ex) {
            // Unable to read the file. Probably because it does not exist --> create it.
            ex.printStackTrace();
        }

        // Set locale to a configured language
        Locale.setDefault(
                new Locale(Configuration.getString("language", "nl"), Configuration.getString("country", "NL")));

        // Start by parsing the command line
        ParseCommandline(args);

        // Display the help if required and leave the app
        if (arguments.hasOption("h")) {
            showHelp();
        }

        // Display the app version and leave the app.
        if (arguments.hasOption("v")) {
            showVersion();
        }

        // Not command line so start the app GUI
        if (!arguments.hasOption("c")) {
            try {
                // Change the look and feel
                UIManager.setLookAndFeel(
                        Configuration.getString("LookAndFeel", "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"));
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        (new MainWindow()).setVisible(true);
                    }
                });
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                // Something terrible happened so show the  help
                showHelp();
            }

        }

    }

    /**
     * Display the Help window (console)
     */
    private static void showHelp() {
        // automatically generate the help statement
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(Application.NAME + " [Option]... [FILE]",
                "Split the given PDF file into single files\n", options,
                "\n--\n" + Application.NAME + "\n\n" + getShortLicenseText(), false);
        System.exit(0);
    }

    /**
     * Retrieve the license file from the resource stream.
     * @return a string containing the license file text
     */
    private static String getShortLicenseText() {
        String licenseText = null;
        BufferedInputStream inputStream = (BufferedInputStream) Application.class
                .getResourceAsStream("/files/license_short.txt"); //$NON-NLS-1$
        try {
            licenseText = IOUtils.toString(inputStream);
            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return licenseText;
    }

    /**
     * Show specific version information (console)
     */
    private static void showVersion() {
        System.out.println(String.format("%1s version %2s\n", Application.NAME, Application.VERSION));

        System.out.println(getShortLicenseText());
        System.out.println("\nWritten by " + String.join(", ", Application.AUTHORS));

        System.exit(0);
    }

    /**
     * Parse the given command line options. In case invalid arguments are given
     * the help will be shown.<br/>
     * Possible options are:
     * <ul>
     * <li>-v, version - Show the version
     * <li>-h, help - Show the help page
     * <li>-c - Console mode
     * </ul>
     * 
     * @param args
     *            - the arguments given to the program
     */
    private static void ParseCommandline(String[] args) {
        // create the command line parser
        CommandLineParser parser = new GnuParser();

        options = new Options();
        options.addOption("h", "help", false, "Show this help.");
        options.addOption("v", "version", false, "Show the application version.");
        options.addOption("c", "console", true, "Start in console mode");
        // options.addOption("l", "list", true, "Use names from a list");

        try {
            // parse the command line arguments
            arguments = parser.parse(options, args);

        } catch (ParseException exp) {
            showHelp();
            System.exit(-1);
        }

    }

    /**
     * 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 SplitFile(File file, boolean useSubFolder) {
        PdfReader reader = null;
        String format = null;

        if (useSubFolder)
            format = "%1$s%2$s%4$s%2$s_%%03d.%3$s";
        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();
        }
    }
}