Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Merging PDF Files using PDFBox in Sub/Directories

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Merging PDF Files using PDFBox in Sub/Directories

    Hi,

    I am currently trying to create a program that merges all the files in directories and sub-directories (automatically) and rename them according to the directory/sub0directory they are in. I am trying to acheive this using the PDFBox library.

    As of now I have found a script that reads all the files and folders in a directory and sub-directories.

    My problem is that I don't know how to acheive the merging of PDFs (i.e. merging the files and renaming them according to the folder name).

    I found a tutorial on how to merge files with PDFBox here but I need to apply it to do it automatically for all the files in the directories.

    The code that lists all the files and folders is this:

    import java.io.File;
     
    public class Main {
     
      static int spc_count=-1;
     
      static void Process(File aFile) {
        spc_count++;
        String spcs = "";
        for (int i = 0; i < spc_count; i++)
          spcs += " ";
        if(aFile.isFile())
          System.out.println(spcs + "[FILE] " + aFile.getName());
        else if (aFile.isDirectory()) {
          System.out.println(spcs + "[DIR] " + aFile.getName());
          File[] listOfFiles = aFile.listFiles();
          if(listOfFiles!=null) {
            for (int i = 0; i < listOfFiles.length; i++)
              Process(listOfFiles[i]);
          } else {
            System.out.println(spcs + " [ACCESS DENIED]");
          }
        }
        spc_count--;
      }
     
      public static void main(String[] args) {
        String nam = "C:\\Documents and Settings\\student3\\Desktop\\PDFs";
        File aFile = new File(nam);
        Process(aFile);
      }
     
     
     
    }

    Any help please?

    Thanks & Regards,
    blivori


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    program that merges all the files in directories and sub-directories
    Could you explain what it means to merge files?
    Do you want to copy the contents of all the files into one file?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    In a folder there are various PDF files for example in PDF_Folder1 there are 5 files PDF1, PDF2, PDF3 and PDF4.

    Now what I want to do is Merge all the PDFs (PDF1-4) and automatically rename it according to the folder name, which in this case is PDF_Folder1.

    Example:

    [DIR] PDF FOLDER 1
    [____FILE] PDF1
    [____FILE] PDF2
    [____FILE] PDF3
    [____FILE] PDF4
    [__SUB-DIR] PDF FOLDER 2
    [____FILE] PDF1
    [____FILE] PDF2
    [____FILE] PDF3

    So:
    - All the PDF files in PDF FOLDER 1 will be merged as a single PDF and the output file called PDF FOLDER 1.
    - All the PDF files in Sub-Directory PDF FOLDER 2 will be merged as a single PDF and the output file called PDF FOLDER 2

    Thanks for you reply,
    blivori

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    I'm not sure about how to preserve the contents of a PDF file, but for text files:
    Open output file
    begin loop to work thru a list of files
    copy next file to output file
    end loop
    close output file

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    I was trying to acheive it by using PDFBox.

    It can be done.

    The problem is how to get the files in the directory and sub-directory, add them to some kind of array or db, and link them with the PDFBox Merger Utility

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    Is this a question of reading API doc for the PDFBox Merger utility to understand how to use it?

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    Kind of..

    I just want to know how to save the files in some sort of array or database and the use the PDFBox Merger Utility to rename it according to the folder/sub-folder the original PDF files where stored in.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    save the files in some sort of array
    Your code does this now:
    File[] listOfFiles = aFile.listFiles(); // saves file references in an array

    What else do you need?

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    yes, now I need to rename the merged pdf according to the folder or sub-folder.

    basically you put the software in the main folder and it automatically processes the files in the folders and sub-folders and renames the files according to the directory they are in.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    You can use the File class to rename files.

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Merging PDF Files using PDFBox in Sub/Directories

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/46901-merging-pdf-files-using-pdfbox-sub-directories.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  2. Write to pdf file with pdfbox with greek encoding
    By javment in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 16th, 2011, 08:57 AM
  3. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  4. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 1st, 2009, 05:13 AM
  5. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2009, 12:48 PM

Tags for this Thread