I want to delete the strings file1 and file2 after its merged to a single pdf merge12.
Please suggest your valuable ideas.. Thanks in advance
package com;
import java.io.FileOutputStream;
import java.util.ArrayList;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
/*
* author
* @282532
*/
public class PdfMerger {
public static void main(String args[]){
String file1 = "d:\\PR00002.PDF";
String file2 = "d:\\PR00003.PDF";
String mergedFileLocation = "d:\\merge12.pdf";
String filesTobeMerges[] = new String[] { file1, file2 };
mergeMyFiles(filesTobeMerges, mergedFileLocation);
}
public static void mergeMyFiles(String filesTobeMerged[],String mergedFileLocation){
System.out.println("Starting to merge files...");
System.out.println("total number of files to be merged"+filesTobeMerged.length +"\n");
try{
int pageOffset = 0;
ArrayList masterBookMarkList = new ArrayList();
String outFile = mergedFileLocation;
Document document = null;
PdfCopy writer = null;
PdfReader reader = null;
for (int fileIndex = 0; fileIndex< filesTobeMerged.length; fileIndex++) {
reader = new PdfReader(filesTobeMerged[fileIndex]);
System.out.println("Reading file" +filesTobeMerged[fileIndex]);
/** * Replace all the local named links with the actual destinations.
* *
*/
reader.consolidateNamedDestinations();
int totalPages = reader.getNumberOfPages();
/** * Merging the files to the first file.
* * If we are passing file1, file2 and file3,
* * we will merge file2 and file3 to file1.
* */
if (fileIndex == 0) {
/** * Create the document object from the reader */
document = new Document(reader.getPageSizeWithRotation(1));
/** * Create a pdf write that listens to this document.
* * Any changes to this document will be written the file
* * * outFile is a location where the final merged document * will be written to. */
System.out.println("Creating an empty PDF...");
writer = new PdfCopy(document, new FileOutputStream(outFile));
/** * Open this document */
document.open();
}
/** * Add the conent of the file into this document (writer).
* * Loop through multiple Pages */
System.out.println("Merging File: "+filesTobeMerged[fileIndex]);
PdfImportedPage page;
for (int currentPage = 1; currentPage <= totalPages; currentPage++) {
page = writer.getImportedPage(reader, currentPage);
writer.addPage(page);
}
}
document.close();
System.out.println("File has been merged and written to-"+mergedFileLocation);
}catch(Exception e){
e.printStackTrace();
}
}
}