fork(2) download
  1. import java.io.IOException;
  2. import java.util.List;
  3.  
  4. import org.apache.pdfbox.pdmodel.PDDocument;
  5. import org.apache.pdfbox.pdmodel.PDPage;
  6. import org.apache.pdfbox.pdmodel.common.PDStream;
  7. import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
  8.  
  9. /**
  10.  *
  11.  * @author lpjkr1
  12.  */
  13. public class CopyPDF {
  14. public static void main(String[] args) {
  15.  
  16. // Set up PDF names
  17. //
  18. String oldName = null;
  19. String newName = null;
  20.  
  21. // Check how many arguments were passed in
  22. if(args.length == 0)
  23. {
  24. System.out.println("Proper Usage is: Java -jar CopyPDF.jar pdfName [new_pdfName]");
  25. System.exit(0);
  26. }
  27.  
  28. oldName = args[0];
  29.  
  30. if(args.length == 2) {
  31. newName = args[1];
  32. }
  33. else {
  34. newName = oldName + "_PDFBox.pdf";
  35. }
  36.  
  37. // Set up time to time how long creating new PDF takes.
  38. //
  39. long startTime = System.currentTimeMillis();
  40.  
  41.  
  42.  
  43. System.getProperty("java.classpath");
  44.  
  45. PDDocument oldDoc = null;
  46. PDDocument newDoc = null;
  47.  
  48. System.out.println ( "Opening PDF file <" + oldName + ">" );
  49.  
  50. try {
  51. newDoc = new PDDocument();
  52.  
  53. oldDoc = PDDocument.load (args[0]);
  54.  
  55. List allPages = oldDoc.getDocumentCatalog().getAllPages();
  56.  
  57. System.out.println ( "Copying pages to new PDF.");
  58. for ( int curPageCnt = 0; curPageCnt < allPages.size(); curPageCnt++ )
  59. {
  60. newDoc.addPage( ( PDPage )allPages.get ( curPageCnt ) );
  61. } // end for
  62.  
  63. newDoc.save( newName );
  64. newDoc.close();
  65. System.out.println ( "New PDF file <" + newName + ">");
  66.  
  67. long endTime = System.currentTimeMillis();
  68. long time_in_millisconds = endTime - startTime;
  69. long time_in_seconds = (endTime - startTime) / 1000;
  70. System.out.println("That took " + time_in_seconds + " seconds");
  71. //System.out.println("That took " + time_in_millisconds + " milliseconds");
  72.  
  73. } // end try
  74. catch ( Exception e ) {
  75. e.printStackTrace();
  76. } // end catch
  77. //System.out.println ( "All Done." );
  78. } // end main
  79. } // end class CopyPDF
  80.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: class CopyPDF is public, should be declared in a file named CopyPDF.java
public class CopyPDF {
       ^
Main.java:4: package org.apache.pdfbox.pdmodel does not exist
import org.apache.pdfbox.pdmodel.PDDocument;
                                ^
Main.java:5: package org.apache.pdfbox.pdmodel does not exist
import org.apache.pdfbox.pdmodel.PDPage;
                                ^
Main.java:6: package org.apache.pdfbox.pdmodel.common does not exist
import org.apache.pdfbox.pdmodel.common.PDStream;
                                       ^
Main.java:7: package org.apache.pdfbox.pdmodel.edit does not exist
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
                                     ^
Main.java:45: cannot find symbol
symbol  : class PDDocument
location: class CopyPDF
        PDDocument oldDoc = null;
        ^
Main.java:46: cannot find symbol
symbol  : class PDDocument
location: class CopyPDF
        PDDocument newDoc = null;
        ^
Main.java:51: cannot find symbol
symbol  : class PDDocument
location: class CopyPDF
            newDoc = new PDDocument();
                         ^
Main.java:53: cannot find symbol
symbol  : variable PDDocument
location: class CopyPDF
            oldDoc = PDDocument.load (args[0]);
                     ^
Main.java:60: cannot find symbol
symbol  : class PDPage
location: class CopyPDF
                newDoc.addPage( ( PDPage )allPages.get ( curPageCnt ) );
                                  ^
10 errors
stdout
Standard output is empty