import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
/**
*
* @author lpjkr1
*/
public class CopyPDF {
public static void main
(String[] args
) {
// Set up PDF names
//
// Check how many arguments were passed in
if(args.length == 0)
{
System.
out.
println("Proper Usage is: Java -jar CopyPDF.jar pdfName [new_pdfName]"); }
oldName = args[0];
if(args.length == 2) {
newName = args[1];
}
else {
newName = oldName + "_PDFBox.pdf";
}
// Set up time to time how long creating new PDF takes.
//
long startTime
= System.
currentTimeMillis();
System.
getProperty("java.classpath");
PDDocument oldDoc = null;
PDDocument newDoc = null;
System.
out.
println ( "Opening PDF file <" + oldName
+ ">" );
try {
newDoc = new PDDocument();
oldDoc = PDDocument.load (args[0]);
List allPages
= oldDoc.
getDocumentCatalog().
getAllPages();
System.
out.
println ( "Copying pages to new PDF."); for ( int curPageCnt = 0; curPageCnt < allPages.size(); curPageCnt++ )
{
newDoc.addPage( ( PDPage )allPages.get ( curPageCnt ) );
} // end for
newDoc.save( newName );
newDoc.close();
System.
out.
println ( "New PDF file <" + newName
+ ">");
long endTime
= System.
currentTimeMillis(); long time_in_millisconds = endTime - startTime;
long time_in_seconds = (endTime - startTime) / 1000;
System.
out.
println("That took " + time_in_seconds
+ " seconds"); //System.out.println("That took " + time_in_millisconds + " milliseconds");
} // end try
e.printStackTrace();
} // end catch
//System.out.println ( "All Done." );
} // end main
} // end class CopyPDF