Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.print.PrinterException;
- import java.awt.print.PrinterJob;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.print.PrintService;
- import org.apache.pdfbox.multipdf.PDFMergerUtility;
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
- import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
- import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
- import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
- import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
- import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
- public class pdf {
- public static String SRC = "C:/Users/ahn/Desktop/pdfs/";
- public static void main(String[] args) throws IOException
- {
- report report1 = new report("TestUser1", "P&T", "operatingActivities", "weeklyTopics","vocationalSchool");
- report report2 = new report("TestUser2", "P&T2", "operatingActivities2", "weeklyTopics2","vocationalSchool2");
- report report3 = new report("TestUser3", "P&T3", "operatingActivities3", "weeklyTopics3","vocationalSchool3");
- report report4 = new report("TestUser4", "P&T4", "operatingActivities4", "weeklyTopics4","vocationalSchool4");
- List<report> reports = new ArrayList<>();
- reports.add(report1);
- reports.add(report2);
- reports.add(report3);
- reports.add(report4);
- fillMultipleReportsInOne(reports);
- //fillReport(SRC + "test.pdf", SRC + "test_filled.pdf", "Alexander", "P&T", "oo","ee", "dd");
- }
- public static void fillReport(String SRC,String DEST,String name, String area, String operatingActivities, String weeklyTopics, String vocationalSchool) throws InvalidPasswordException, IOException {
- try
- {
- PDDocument pdfDocument = PDDocument.load(new File(SRC));
- // get the document catalog
- PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
- // as there might not be an AcroForm entry a null check is necessary
- setFormFields(acroForm, name, area, operatingActivities, vocationalSchool, weeklyTopics);
- // Save and close the filled out form.
- encryptPDF(pdfDocument, DEST);
- System.out.println("PDF erstellt mit Pfad" + DEST);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void fillMultipleReportsInOne(List<report> reports) throws IOException {
- PDFMergerUtility PDFmerger = new PDFMergerUtility();
- PDDocument resultPDF = new PDDocument();
- for (report report : reports) {
- try
- {
- PDDocument pdfDocument = PDDocument.load(new File(SRC + "test.pdf"));
- // get the document catalog
- PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
- // as there might not be an AcroForm entry a null check is necessary
- setFormFields(acroForm, report.getName(), report.getArea(), report.getOperatingActivities(), report.getVocationlaSchool(), report.getWeeklyTopics());
- // Save and close the filled out form.
- PDFmerger.appendDocument(resultPDF, pdfDocument);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- encryptPDF(resultPDF, SRC + "merged.pdf");
- }
- public static void setFormFields(PDAcroForm acroForm, String name, String area, String operatingActivities, String vocationalSchool, String weeklyTopics) throws IOException {
- if (acroForm != null)
- {
- // Retrieve an individual field and set its value.
- PDTextField fieldName = (PDTextField) acroForm.getField( "name" );
- fieldName.setValue(name);
- PDTextField fieldArea = (PDTextField) acroForm.getField( "area" );
- fieldArea.setValue(area);
- PDTextField fieldOperating = (PDTextField) acroForm.getField( "operating" );
- fieldOperating.setValue(operatingActivities);
- PDTextField filedSchool = (PDTextField) acroForm.getField( "school" );
- filedSchool.setValue(vocationalSchool);
- PDTextField fieldWeekly = (PDTextField) acroForm.getField( "topics" );
- fieldWeekly.setValue(weeklyTopics);
- //acroForm.flatten();
- // If a field is nested within the form tree a fully qualified name
- // might be provided to access the field.
- //field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" );
- //field.setValue("Text Entry");
- }
- }
- public static PDDocument encryptPDF(PDDocument pddocumentToEncrypt, String SRC) {
- // Define the length of the encryption key.
- // Possible values are 40 or 128 (256 will be available in PDFBox 2.0).
- int keyLength = 256;
- AccessPermission ap = new AccessPermission();
- // Disable printing, everything else is allowed
- ap.setCanModifyAnnotations(false);
- ap.setCanFillInForm(false);
- ap.setCanModify(false);
- // Owner password (to open the file with all permissions) is "12345"
- // User password (to open the file but with restricted permissions, is empty here)
- StandardProtectionPolicy spp = new StandardProtectionPolicy("12334", "", ap);
- spp.setEncryptionKeyLength(keyLength);
- spp.setPermissions(ap);
- try {
- pddocumentToEncrypt.protect(spp);
- pddocumentToEncrypt.save(SRC);
- pddocumentToEncrypt.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return pddocumentToEncrypt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement