6,000+ companies already love Pdftools

Need a no-code PDF tool without implementation? Try Smallpdf.
All your PDF needs met
From conversions to compression, we have everything you need to integrate PDF documents into your product and your customers.
Build faster, build better
Made by developers for developers. Test our versatile PDF SDK and see it for yourself.
1// Create a converter object
2Converter converter = new Converter();
3
4// Add handler for conversion events
5class EventListener implements ConversionEventListener
6{
7 private EventSeverity eventsSeverity = EventSeverity.INFORMATION;
8
9 public EventSeverity getEventsSeverity() {
10 return eventsSeverity;
11 }
12
13 @Override
14 public void conversionEvent(ConversionEvent event) {
15 // Get the event's suggested severity
16 EventSeverity severity = event.getSeverity();
17
18 // Optionally, the suggested severity can be changed according to
19 // the requirements of your conversion process and, for example,
20 // the event's category (e.Category).
21
22 if (severity.ordinal() > eventsSeverity.ordinal())
23 eventsSeverity = severity;
24
25 // Report conversion event
26 System.out.format("- %c %s: %s (%s%s)%n", severity.toString().charAt(0), event.getCategory(), event.getMessage(), event.getContext(), event.getPageNo() > 0 ? " on page " + event.getPageNo() : "");
27 }
28}
29EventListener el = new EventListener();
30
31converter.addConversionEventListener(el);
32
1try (
2 // Create a session to the built-in cryptographic provider
3 Provider session = new Provider();
4
5 // Open certificate file
6 FileStream pfxStr = new FileStream(certificateFile, FileStream.Mode.READ_ONLY))
7{
8 // Create signature configuration from PFX (or P12) file
9 SignatureConfiguration signature = session.createSignatureFromCertificate(pfxStr, password);
10
11 // Embed validation information to enable the long-term validation (LTV) of the signature (default)
12 signature.setValidationInformation(com.pdftools.crypto.ValidationInformation.EMBED_IN_DOCUMENT);
13
14 // Create the Signer object
15 Signer signer = new Signer();
16
17 // (optional) create an event listener to listen for warning events that are raised and write them to console
18 signer.addWarningListener((e) -> { System.out.format("Warning - %s: %s: %s", e.getCategory(), e.getContext(), e.getMessage()); });
19
20 try (
21 // Open input document
22 FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
23 Document inDoc = Document.open(inStr);
24
25 // Create a stream for the output file
26 FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
27
28 // Sign the input document
29 Document outDoc = signer.sign(inDoc, signature, outStr))
30 {
31 }
32}
33
1// Create the Optimizer object.
2Optimizer optimizer = new Optimizer();
3
4// Create the profile that defines the optimization parameters.
5// The Web profile is used to optimize documents for electronic document exchange.
6Web profile = new Web();
7
8// Optionally, the profile's parameters can be changed according to the
9// requirements of your optimization process.
10
11try (
12 // Open input document
13 FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
14 Document inDoc = Document.open(inStr);
15
16 // Create output stream
17 FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
18
19 // Optimize the document
20 Document outDoc = optimizer.optimizeDocument(inDoc, outStr, profile))
21{
22}
23
1// Open an encrypted input document
2// The password parameter is optional, and only required to open an encrypted PDF document
3FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
4com.pdftools.pdf.Document inDoc = com.pdftools.pdf.Document.open(inStr, password)
5
6// Create output stream
7FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
8
9// Create output options and specify encryption parameters
10// In this example, a 'User' is only granted permission to fill forms and print the document
11OutputOptions outOpt = new OutputOptions();
12
13// Set a user password that is required to open the document.
14// Note that this removes PDF/A conformance of input files (see warning category WarningCategory.PDF_A_REMOVED)
15outOpt.setEncryption(new Encryption(strUserPassword, strOwnerPassword, EnumSet.of(Permission.FILL_FORMS, Permission.PRINT)));
16
17// Optimize the input PDF document and save it to a file, applying the specified output encryption
18Document outDoc = new Optimizer().optimizeDocument(inDoc, outStr, profile, outOpt));
19
20// Create output options and remove encryption by setting it to null
21OutputOptions outOpt = new OutputOptions();
22outOpt.setEncryption(null);
23
1// Open the PDF document to validate
2try (FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
3 com.pdftools.pdf.Document inDoc = com.pdftools.pdf.Document.open(inStr))
4{
5 // Create a conformance object that specifies the PDF/A standard and level against which the document is validated.
6 // Here we choose the PDF/A-2 Standard, with conformance Level B.
7 Conformance conformance = new Conformance(new Conformance.PdfAVersion(2, Conformance.PdfAVersion.Level.B));
8
9 // Create a Validator object and attach an Error event handler that simply writes the validation error messages to the console.
10 Validator validator = new Validator();
11 validator.addErrorListener(
12 (Validator.Error error) ->
13 System.out.format("- %s: %s (%s%s)%n", error.getCategory(), error.getMessage(), error.getContext(), error.getPageNo() > 0 ? String.format(" on page %d", error.getPageNo()) : "")
14 );
15
16 // Validate the PDF/A standard and level of the document against the defined Conformance level.
17 ValidationOptions options = new ValidationOptions();
18 options.setConformance(conformance);
19 ValidationResult result = validator.validate(inDoc, options);
20
21 // Write the result of the Validate method to the console.
22 System.out.println("Document conforms to " + result.getConformance().toString() + ": " + result.getIsConforming​());
23}
24
Try Pdftools and see for yourself
Sign up for a free 30-day trial and join the 6,000+ customers already using Pdftools to manage their documents.
Stress less about your PDF workflow
Test drive Pdftools for yourself with a free trial.


