Java program to generate iGigBook index from files

Share your gig experiences using iGigBook
iGigBook User
Posts: 10
Joined: Sat Jun 07, 2014 8:36 pm

Java program to generate iGigBook index from files

Postby bros225 » Thu Mar 24, 2016 5:45 pm

You might find this program useful. It reads a folder of PDF files and generates an iGigBook index from it using the files names for the song names. Enjoy! Note: I wrote it for windows - don't know if it works on Mac. Sorry about lost indentation - I tried to attach it but kept getting errors (invalid file extension).

Code: Select all

/* This Class is a standalone Java application. It reads a folder containing PDF files and generates
 * an iGigBook index from it. Presumably, each file represents one song made up of one or more pages.
 * The file name is assigned to song name in the index (author is set to "Unknown, Unknown"). The
 * files must be PDF files, but a file extension (.PDF) is not required; however, if a file extension
 * exists it will be removed for purposes of assigning the song name.
 * WARNING: The files in the folder are renamed, so you are advised to create a backup copy of the
 * folder before running this application.
 * The files are renamed by giving each file a prefix which identifies the page number for the song
 * in the index. The prefix guarantees that when you combine the PDF files (using a tool of your
 * choice) you will be able to combine them in the same sequence as in the index. The prefix also
 * allows you to keep your songs as separate files and not lose the relationship to the index (which
 * is desirable, since once you combine the files it is difficult to reverse the process).
 * For consistency, the files are sorted (using a TreeMap) by filename before processing (presumably
 * using the ascii collating sequence, but that's up to java).
 * There are 4 required, positional runtime arguments:
 * (1) PDF folder name: The folder that contains the PDF song files. To be certain of the location,
 *     you are advised to fully qualify the name.
 * (2) Starting page number: Since iGigBook doesn't allow you to delete and rebuild the entire index
 *     every time you make a change, this allows you to start where you left off (assuming you are
 *     building a book over time).
 * (3) Output file name: The text file to which the iGigBook index is written. To be certain of the
 *     location, you are advised to fully qualify the name.
 * (4) File rewrite indicator: r = write over the output file if it already exists (case ignored),
 *     Any other value = abend if the output file already exists.

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;

import com.itextpdf.text.pdf.PdfReader;

class GenIgigbookIndex {

   private class ApplException extends Exception {
      private int msgCode;

      private ApplException(int msgCode, String msg) {
         this.msgCode = msgCode;

   private class Args {
      int reqArgs = 4; // required number of arguments
      File pdfFolder; // folder containing PDF files
      int startPageNum; // starting page number
      int minPageNum = 1; // minimum page number
      int maxPageNum = 99999; // maximum page number
      String filePfxMask = "00000"; // for padding the file prefix
      String filePfxSep = "_"; // file prefix separator
      PrintWriter outFile; // output file
      String outFileStr; // output file name
      String fileOverw; // overwrite or new file

   private Args getArgs(String args[]) throws Exception {
       * Validate the runtime arguments and convert each into a useful object
      ApplException e = new ApplException(0, "");
      Args validArgs = new Args();

      if (args.length != validArgs.reqArgs) {
         e = new ApplException(1, "Invalid number of arguments, number=" + args.length);
         throw e;
      validArgs.pdfFolder = new File(args[0]);
      if (!validArgs.pdfFolder.exists()) {
         e = new ApplException(2, "Invalid pdfFolder argument - folder not found, pdfFolder="
               + validArgs.pdfFolder.getAbsolutePath());
         throw e;
      if (!validArgs.pdfFolder.isDirectory()) {
         e = new ApplException(3, "Invalid pdfFolder argument - must be a directory, pdfFolder="
               + validArgs.pdfFolder.getAbsolutePath());
         throw e;
      try {
         validArgs.startPageNum = Integer.parseInt(args[1]);
      } catch (Exception exception) {
         e = new ApplException(4, "Invalid startPageNum argument - must be an integer, startPageNum=" + args[1]);
         throw e;
      if (validArgs.startPageNum < validArgs.minPageNum || validArgs.startPageNum > validArgs.maxPageNum) {
         e = new ApplException(5,
               "Invalid startPageNum argument - out of range, startPageNum=" + validArgs.startPageNum);
         throw e;
      validArgs.outFileStr = args[2];
      validArgs.fileOverw = args[3];
      File outFile = new File(validArgs.outFileStr);
      if (outFile.exists() && !validArgs.fileOverw.equalsIgnoreCase("r")) {
         e = new ApplException(6, "Invalid outFile argument - file already exists, outFile=" + validArgs.outFileStr);
         throw e;
      if (outFile.isDirectory()) {
         e = new ApplException(7, "Invalid outFile argument - file is a directory, outFile=" + validArgs.outFileStr);
         throw e;
      validArgs.outFile = new PrintWriter(new FileWriter(validArgs.outFileStr));
      System.out.println("PDF folder=" + validArgs.pdfFolder.getAbsolutePath());
      System.out.println("Starting page number=" + validArgs.startPageNum);
      System.out.println("Index output file=" + outFile.getAbsolutePath());
      System.out.println("Output file overwrite option=" + validArgs.fileOverw);
      return validArgs;

   private int genIgigIndex(Args validArgs) throws Exception {
       * Generate iGigBook index
      ApplException e = new ApplException(0, "");
      int pageNum = validArgs.startPageNum;
      File listOfFile[] = validArgs.pdfFolder.listFiles();
      TreeMap<String, String> fileNameTreeMap = new TreeMap<String, String>();
      for (int i = 0; i < listOfFile.length; i++) {
         String fileName = listOfFile[i].getName();
         fileNameTreeMap.put(fileName, fileName);
      Collection<String> c = fileNameTreeMap.keySet();
      Iterator<String> itr = c.iterator();
      int fileCount = 0;
      while (itr.hasNext()) {
         if (pageNum > validArgs.maxPageNum) {
            e = new ApplException(8, "Page number exceeded maximum, pageNum=" + pageNum);
            throw e;
         Object key =;
         String fileName = fileNameTreeMap.get(key).toString();
         int indexOfExt = fileName.lastIndexOf('.');
         if (indexOfExt < 1) {
            indexOfExt = fileName.length();
         File pdfFile = new File(validArgs.pdfFolder + "\\" + fileName);
         PdfReader document = new PdfReader(new FileInputStream(pdfFile));
         int noPages = document.getNumberOfPages();
         validArgs.outFile.println('"' + fileName.substring(0, indexOfExt) + '"' + "," + pageNum + "," + noPages
               + "," + "\"Unknown, Unknown\"");
         String filePfx = validArgs.filePfxMask + pageNum;
         String filePfxFixed = filePfx.substring(filePfx.length() - validArgs.filePfxMask.length())
               + validArgs.filePfxSep;
         File destFile = new File(validArgs.pdfFolder + "\\" + filePfxFixed + fileName);
         pageNum = pageNum + noPages;
      return fileCount;

   public static void main(String[] args) {
      GenIgigbookIndex thisMain = new GenIgigbookIndex();
      System.out.println(thisMain.getClass().getName() + " started");
      int fileCount = 0;
      try {
         Args validArgs = thisMain.getArgs(args);
         fileCount = thisMain.genIgigIndex(validArgs);
      } catch (ApplException applException) {
         System.err.println("Application abended due to application exception.");
         System.err.println("(" + applException.msgCode + ") " + applException.toString());
      } catch (Exception exception) {
         System.err.println("Application abended due to unexpected exception.");
      System.out.println(thisMain.getClass().getName() + " ended, files processed=" + fileCount);

User avatar
Phil Smith
iGigBook Specialist
Posts: 2130
Joined: Tue Oct 19, 2010 8:00 pm

Re: Java program to generate iGigBook index from files

Postby Phil Smith » Thu Mar 24, 2016 7:36 pm

I've taken the liberty to add code tags to the Java source.

* (2) Starting page number: Since iGigBook doesn't allow you to delete and rebuild the entire index
* every time you make a change, this allows you to start where you left off (assuming you are
* building a book over time).

If you have uploaded an index and that index has typos you can use the Content Manager to correct those entries without having to delete the index and replace it.
Are you happy with iGigBook?
Tell the world about it by leaving a rating or a review in the App or Google Play Store.
Got Questions? Click Here For The How To Section
iGigBook: The Go-To Gig Tool.

Return to “Your iGigBook Gig Stories”

Who is online

Users browsing this forum: No registered users and 1 guest