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:
Code :
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
Re: Merging PDF Files using PDFBox in Sub/Directories
Quote:
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?
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
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
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
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?
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.
Re: Merging PDF Files using PDFBox in Sub/Directories
Quote:
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?
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.
Re: Merging PDF Files using PDFBox in Sub/Directories
You can use the File class to rename files.
Re: Merging PDF Files using PDFBox in Sub/Directories