Advertisement
Guest User

PDFBox Merge code

a guest
Feb 25th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1.  
  2. import java.awt.print.PrinterException;
  3. import java.awt.print.PrinterJob;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import javax.print.PrintService;
  10.  
  11. import org.apache.pdfbox.multipdf.PDFMergerUtility;
  12. import org.apache.pdfbox.pdmodel.PDDocument;
  13. import org.apache.pdfbox.pdmodel.PDPage;
  14. import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
  15. import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
  16. import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
  17. import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
  18. import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
  19.  
  20. public class pdf {
  21.    
  22.     public static String SRC = "C:/Users/ahn/Desktop/pdfs/";
  23.    
  24.        
  25.  
  26.         public static void main(String[] args) throws IOException
  27.         {
  28.             report report1 = new report("TestUser1", "P&T", "operatingActivities", "weeklyTopics","vocationalSchool");
  29.             report report2 = new report("TestUser2", "P&T2", "operatingActivities2", "weeklyTopics2","vocationalSchool2");
  30.             report report3 = new report("TestUser3", "P&T3", "operatingActivities3", "weeklyTopics3","vocationalSchool3");
  31.             report report4 = new report("TestUser4", "P&T4", "operatingActivities4", "weeklyTopics4","vocationalSchool4");
  32.            
  33.             List<report> reports = new ArrayList<>();
  34.             reports.add(report1);
  35.             reports.add(report2);
  36.             reports.add(report3);
  37.             reports.add(report4);
  38.            
  39.             fillMultipleReportsInOne(reports);
  40.            
  41.             //fillReport(SRC + "test.pdf", SRC + "test_filled.pdf", "Alexander", "P&T", "oo","ee", "dd");
  42.        
  43.            
  44.            
  45.            
  46.          
  47.         }
  48.        
  49.        
  50.         public static void fillReport(String SRC,String DEST,String name, String area, String operatingActivities, String weeklyTopics, String vocationalSchool) throws InvalidPasswordException, IOException {
  51.             try
  52.             {
  53.                 PDDocument pdfDocument = PDDocument.load(new File(SRC));
  54.                 // get the document catalog
  55.                 PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
  56.                
  57.                 // as there might not be an AcroForm entry a null check is necessary
  58.                 setFormFields(acroForm, name, area, operatingActivities, vocationalSchool, weeklyTopics);
  59.                
  60.                 // Save and close the filled out form.
  61.                 encryptPDF(pdfDocument, DEST);
  62.                 System.out.println("PDF erstellt mit Pfad" + DEST);
  63.             } catch(Exception e) {
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.        
  68.        
  69.        
  70.         public static void fillMultipleReportsInOne(List<report> reports) throws IOException {
  71.            
  72.             PDFMergerUtility PDFmerger = new PDFMergerUtility();
  73.             PDDocument resultPDF = new PDDocument();
  74.            
  75.             for (report report : reports) {
  76.                
  77.                 try
  78.                 {
  79.                     PDDocument pdfDocument = PDDocument.load(new File(SRC + "test.pdf"));
  80.                     // get the document catalog
  81.                     PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
  82.                    
  83.                     // as there might not be an AcroForm entry a null check is necessary
  84.                     setFormFields(acroForm, report.getName(), report.getArea(), report.getOperatingActivities(), report.getVocationlaSchool(), report.getWeeklyTopics());
  85.                     // Save and close the filled out form.
  86.                     PDFmerger.appendDocument(resultPDF, pdfDocument);
  87.                    
  88.                    
  89.                    
  90.             } catch (Exception e) {
  91.                 e.printStackTrace();
  92.             }
  93.                
  94.         }
  95.        
  96.        
  97.         encryptPDF(resultPDF, SRC + "merged.pdf");
  98.        
  99.     }
  100.        
  101.         public static void setFormFields(PDAcroForm acroForm, String name, String area, String operatingActivities, String vocationalSchool, String weeklyTopics) throws IOException {
  102.             if (acroForm != null)
  103.             {
  104.                 // Retrieve an individual field and set its value.
  105.                 PDTextField fieldName = (PDTextField) acroForm.getField( "name" );
  106.                 fieldName.setValue(name);
  107.                
  108.                 PDTextField fieldArea = (PDTextField) acroForm.getField( "area" );
  109.                 fieldArea.setValue(area);
  110.                
  111.                 PDTextField fieldOperating = (PDTextField) acroForm.getField( "operating" );
  112.                 fieldOperating.setValue(operatingActivities);
  113.                
  114.                 PDTextField filedSchool = (PDTextField) acroForm.getField( "school" );
  115.                 filedSchool.setValue(vocationalSchool);
  116.                
  117.                 PDTextField fieldWeekly = (PDTextField) acroForm.getField( "topics" );
  118.                 fieldWeekly.setValue(weeklyTopics);
  119.                
  120.                 //acroForm.flatten();
  121.                
  122.                 // If a field is nested within the form tree a fully qualified name
  123.                 // might be provided to access the field.
  124.                 //field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" );
  125.                 //field.setValue("Text Entry");
  126.             }
  127.            
  128.         }
  129.        
  130.        
  131.         public static PDDocument encryptPDF(PDDocument pddocumentToEncrypt, String SRC) {
  132.  
  133.             // Define the length of the encryption key.
  134.             // Possible values are 40 or 128 (256 will be available in PDFBox 2.0).
  135.             int keyLength = 256;
  136.  
  137.             AccessPermission ap = new AccessPermission();
  138.  
  139.             // Disable printing, everything else is allowed
  140.             ap.setCanModifyAnnotations(false);
  141.             ap.setCanFillInForm(false);
  142.             ap.setCanModify(false);
  143.            
  144.  
  145.             // Owner password (to open the file with all permissions) is "12345"
  146.             // User password (to open the file but with restricted permissions, is empty here)
  147.             StandardProtectionPolicy spp = new StandardProtectionPolicy("12334", "", ap);
  148.             spp.setEncryptionKeyLength(keyLength);
  149.             spp.setPermissions(ap);
  150.             try {
  151.                 pddocumentToEncrypt.protect(spp);
  152.                 pddocumentToEncrypt.save(SRC);
  153.                 pddocumentToEncrypt.close();
  154.             } catch (IOException e) {
  155.                 // TODO Auto-generated catch block
  156.                 e.printStackTrace();
  157.             }
  158.             return pddocumentToEncrypt;
  159.         }
  160.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement