PDFUtils
PDFUtils is a Spring bean providing methods for working with PDF documents: merging, encrypting, decrypting, splitting, extracting pages, rotating, filling forms, and rendering pages as images. Static overloads operate on local streams and files; instance methods operate directly on OpenKM repository documents.
Merging
Section titled “Merging”merge (streams)
Section titled “merge (streams)”Description:
| Method | Return values | Description |
|---|---|---|
| merge(List |
void | Merges multiple PDF input streams into a single PDF written to the output stream. |
Example:
package com.openkm;
import com.openkm.api.OKMDocument;import com.openkm.util.ContextWrapper;import com.openkm.util.FileUtils;import com.openkm.util.PDFUtils;import com.openkm.util.PathUtils;import org.apache.commons.io.IOUtils;
import java.io.*;import java.util.Arrays;
public class Test {
public static void main(String[] args) { File tmp = null; try { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class);
tmp = FileUtils.createTempFile("pdf");
try (OutputStream fos = new FileOutputStream(tmp)) { pdfUtils.merge(Arrays.asList( okmDocument.getContent(null, "c0984ecf-8d9e-40f8-b5fb-2c83d202bd9d", false), okmDocument.getContent(null, "b3d0af95-076c-4cc4-9b85-bd3e7eb2baa3", false) ), fos); }
try (InputStream fis = new FileInputStream(tmp)) { okmDocument.createSimple(null, "/okm:root/docMerge.pdf", fis); } } catch (Exception e) { e.printStackTrace(); } finally { FileUtils.deleteQuietly(tmp); } }}merge (repository documents)
Section titled “merge (repository documents)”Description:
| Method | Return values | Description |
|---|---|---|
| merge(String token, List |
String | Merges a list of OpenKM documents (by path or UUID) into a single PDF and stores the result at dstPath in the repository. Non-PDF documents are automatically converted to PDF before merging. Returns the path of the resulting document. |
- token: Authentication token.
- docIds: List of repository paths or UUIDs to merge.
- dstPath: Repository path where the merged PDF will be created.
Example:
PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class);String result = pdfUtils.merge(null, Arrays.asList("/okm:root/doc1.pdf", "/okm:root/doc2.pdf"), "/okm:root/merged.pdf");System.out.println("Merged to: " + result);Form operations
Section titled “Form operations”fillForm
Section titled “fillForm”Description:
| Method | Return values | Description |
|---|---|---|
| fillForm(InputStream input, Map<String, Object> values, OutputStream output) | void | Fills AcroForm fields in a PDF. Fields whose current value contains template expressions (e.g. ${variable}) are processed through the template engine. Fields are also matched by name from the values map. Supports text, checkbox, and date fields. The form is flattened (made non-editable) for any field that is filled. |
- input: The PDF input stream.
- values: A map of field name → value for substitution.
- output: The output stream for the filled PDF.
Example:
package com.openkm;
import com.openkm.util.PDFUtils;
import java.io.*;import java.util.HashMap;import java.util.Map;
public class Test {
public static void main(String[] args) { try (InputStream is = new FileInputStream("/home/openkm/template.pdf"); OutputStream os = new FileOutputStream("/home/openkm/filled.pdf")) {
Map<String, Object> values = new HashMap<>(); values.put("customerName", "John Smith"); values.put("invoiceDate", new java.util.Date()); values.put("approved", "true"); // checkbox
PDFUtils.fillForm(is, values, os); } catch (Exception e) { e.printStackTrace(); } }}listFormFields
Section titled “listFormFields”Description:
| Method | Return values | Description |
|---|---|---|
| listFormFields(String input) | List |
Returns the names of all AcroForm fields in the given PDF file path. Returns an empty list if the PDF has no form. |
- input: The local file system path to the PDF file.
Example:
List<String> fields = PDFUtils.listFormFields("/home/openkm/template.pdf");System.out.println("Form fields: " + fields);Encryption
Section titled “Encryption”encrypt (streams)
Section titled “encrypt (streams)”Description:
| Method | Return values | Description |
|---|---|---|
| encrypt(InputStream input, String userPassword, String ownerPassword, int permissions, OutputStream output) | void | Encrypts a PDF stream with the given passwords and permissions. Pass null for userPassword to allow opening without a password (restricted by permissions). Pass a non-null userPassword to require a password to open the document. |
- permissions: ORed iText permissions constants, e.g.
PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY.
encrypt (file)
Section titled “encrypt (file)”Description:
| Method | Return values | Description |
|---|---|---|
| encrypt(File input, String userPassword, String ownerPassword, int permissions) | File | Encrypts a PDF file and returns a new temporary file with the encrypted content. |
encrypt (repository document)
Section titled “encrypt (repository document)”Description:
| Method | Return values | Description |
|---|---|---|
| encrypt(String token, String docId, String userPassword, String ownerPassword, int permissions, String dstPath) | void | Encrypts an OpenKM document and stores the result at dstPath. If dstPath already exists the document is checked out and checked in with the new content. |
decrypt (streams)
Section titled “decrypt (streams)”Description:
| Method | Return values | Description |
|---|---|---|
| decrypt(InputStream input, String ownerPassword, OutputStream output) | void | Decrypts a PDF stream using the owner password and writes the result to the output stream. |
decrypt (file)
Section titled “decrypt (file)”Description:
| Method | Return values | Description |
|---|---|---|
| decrypt(File input, String ownerPassword) | File | Decrypts a PDF file and returns a new temporary file with the decrypted content. |
isEncrypted (InputStream)
Section titled “isEncrypted (InputStream)”Description:
| Method | Return values | Description |
|---|---|---|
| isEncrypted(InputStream input) | boolean | Returns true if the PDF stream is encrypted. |
isEncrypted (File)
Section titled “isEncrypted (File)”Description:
| Method | Return values | Description |
|---|---|---|
| isEncrypted(File input) | boolean | Returns true if the PDF file is encrypted. |
disableEncryption (streams)
Section titled “disableEncryption (streams)”Description:
| Method | Return values | Description |
|---|---|---|
| disableEncryption(InputStream input, OutputStream output) | void | Removes the encryption flag from a PDF without requiring the owner password, allowing subsequent modification. |
disableEncryption (file)
Section titled “disableEncryption (file)”Description:
| Method | Return values | Description |
|---|---|---|
| disableEncryption(File input) | File | Removes the encryption flag from a PDF file and returns a new temporary file with the result. |
Page count
Section titled “Page count”getNumberOfPages (File)
Section titled “getNumberOfPages (File)”Description:
| Method | Return values | Description |
|---|---|---|
| getNumberOfPages(File pdf) | int | Returns the number of pages in a local PDF file. |
getNumberOfPages (InputStream)
Section titled “getNumberOfPages (InputStream)”Description:
| Method | Return values | Description |
|---|---|---|
| getNumberOfPages(InputStream stream) | int | Returns the number of pages in a PDF stream. |
getNumberOfPages (repository document)
Section titled “getNumberOfPages (repository document)”Description:
| Method | Return values | Description |
|---|---|---|
| getNumberOfPages(String token, String docId) | int | Returns the number of pages in an OpenKM document. Non-PDF documents are automatically converted to PDF. |
Example:
PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class);int pages = pdfUtils.getNumberOfPages(null, "e6b61e8d-4bf1-4c37-828f-09ecc34261cc");System.out.println("Pages: " + pages);Document rendering
Section titled “Document rendering”getDocumentInPdf
Section titled “getDocumentInPdf”Description:
| Method | Return values | Description |
|---|---|---|
| getDocumentInPdf(String token, String docId) | InputStream | Returns the document content as a PDF stream. If the document is not a PDF it is converted to PDF first (using the conversion cache if available). The caller is responsible for closing the stream. |
getPageAsImage
Section titled “getPageAsImage”Description:
| Method | Return values | Description |
|---|---|---|
| getPageAsImage(String token, String docId, int page, String size) | File | Renders a single page of a PDF (or convertible) document as a PNG image and returns a temporary file. |
- page: The 1-based page number.
- size: The desired image size/resolution descriptor.
Page operations
Section titled “Page operations”split (file)
Section titled “split (file)”Description:
| Method | Return values | Description |
|---|---|---|
| split(File file, String baseName, List |
File | Splits a PDF file at the given page boundaries and returns a ZIP file containing the resulting parts, each named using baseName. |
split (repository document)
Section titled “split (repository document)”Description:
| Method | Return values | Description |
|---|---|---|
| split(String token, String docId, String dstId, List |
List |
Splits an OpenKM PDF document at the given page boundaries and stores the resulting parts in the dstId folder. Returns the list of created documents. |
extract (file)
Section titled “extract (file)”Description:
| Method | Return values | Description |
|---|---|---|
| extract(File src, File dst, List |
void | Extracts the specified pages from a PDF file and writes the result to dst. |
extract (repository document)
Section titled “extract (repository document)”Description:
| Method | Return values | Description |
|---|---|---|
| extract(String token, String docId, String dstId, List |
Document | Extracts the specified pages from an OpenKM PDF document and stores the result as a new document named docName in the dstId folder. Returns the created document. |
rotate (file)
Section titled “rotate (file)”Description:
| Method | Return values | Description |
|---|---|---|
| rotate(File src, File dst, List |
void | Rotates the specified pages of a PDF file by the given angle (90, 180, or 270 degrees) and writes the result to dst. |
rotate (repository document)
Section titled “rotate (repository document)”Description:
| Method | Return values | Description |
|---|---|---|
| rotate(String token, String docId, String dstId, List |
Document | Rotates the specified pages of an OpenKM PDF document and stores the result as a new document in the dstId folder. Returns the created document. |
Optimization and conversion
Section titled “Optimization and conversion”optimize (file)
Section titled “optimize (file)”Description:
| Method | Return values | Description |
|---|---|---|
| optimize(File pdfIn, File pdfOut) | void | Optimizes a PDF file by linearizing it for fast web viewing and writes the result to pdfOut. |
optimize (repository document)
Section titled “optimize (repository document)”Description:
| Method | Return values | Description |
|---|---|---|
| optimize(String token, String docId) | void | Optimizes an OpenKM PDF document in place by checking it out, optimizing, and checking it back in. |
convertToPdfA
Section titled “convertToPdfA”Description:
| Method | Return values | Description |
|---|---|---|
| convertToPdfA(File input) | File | Converts a PDF file to PDF/A format for long-term archiving and returns a new temporary file with the result. |
Utility
Section titled “Utility”generateSample
Section titled “generateSample”Description:
| Method | Return values | Description |
|---|---|---|
| generateSample(int paragraphs, OutputStream os) | void | Generates a sample Lorem Ipsum PDF with the specified number of paragraphs. |
createPdfWithText
Section titled “createPdfWithText”Description:
| Method | Return values | Description |
|---|---|---|
| createPdfWithText(String text, File out) | void | Creates a simple single-page PDF containing the given text and writes it to the specified output file. |
Example:
File out = FileUtils.createTempFile("pdf");PDFUtils.createPdfWithText("Hello, OpenKM!", out);