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.
PDFUtils is a Spring bean ? access it via ContextWrapper.getContext().getBean(PDFUtils.class) or inject it with @Autowired.
Merging
merge (streams)
Description:
| Method | Return values | Description |
|---|---|---|
|
merge(List<InputStream> inputs, OutputStream output) |
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)
Description:
| Method | Return values | Description |
|---|---|---|
|
merge(String token, List<String> docIds, String dstPath) |
String |
Merges a list of OpenKM documents (by path or UUID) into a single PDF and stores the result at |
|
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
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. |
|
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
Description:
| Method | Return values | Description |
|---|---|---|
|
listFormFields(String input) |
List<String> |
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
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 |
|
permissions: ORed iText permissions constants, e.g. |
||
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)
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 |
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)
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)
Description:
| Method | Return values | Description |
|---|---|---|
|
isEncrypted(InputStream input) |
boolean |
Returns |
isEncrypted (File)
Description:
| Method | Return values | Description |
|---|---|---|
|
isEncrypted(File input) |
boolean |
Returns |
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)
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
getNumberOfPages (File)
Description:
| Method | Return values | Description |
|---|---|---|
|
getNumberOfPages(File pdf) |
int |
Returns the number of pages in a local PDF file. |
getNumberOfPages (InputStream)
Description:
| Method | Return values | Description |
|---|---|---|
|
getNumberOfPages(InputStream stream) |
int |
Returns the number of pages in a PDF stream. |
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
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
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
split (file)
Description:
| Method | Return values | Description |
|---|---|---|
|
split(File file, String baseName, List<Integer> pages) |
File |
Splits a PDF file at the given page boundaries and returns a ZIP file containing the resulting parts, each named using |
split (repository document)
Description:
| Method | Return values | Description |
|---|---|---|
|
split(String token, String docId, String dstId, List<Integer> pages, String baseName) |
List<Document> |
Splits an OpenKM PDF document at the given page boundaries and stores the resulting parts in the |
extract (file)
Description:
| Method | Return values | Description |
|---|---|---|
|
extract(File src, File dst, List<Integer> pages) |
void |
Extracts the specified pages from a PDF file and writes the result to |
extract (repository document)
Description:
| Method | Return values | Description |
|---|---|---|
|
extract(String token, String docId, String dstId, List<Integer> pages, String docName) |
Document |
Extracts the specified pages from an OpenKM PDF document and stores the result as a new document named |
rotate (file)
Description:
| Method | Return values | Description |
|---|---|---|
|
rotate(File src, File dst, List<Integer> pages, Integer angle) |
void |
Rotates the specified pages of a PDF file by the given angle (90, 180, or 270 degrees) and writes the result to |
rotate (repository document)
Description:
| Method | Return values | Description |
|---|---|---|
|
rotate(String token, String docId, String dstId, List<Integer> pages, String docName, Integer angle) |
Document |
Rotates the specified pages of an OpenKM PDF document and stores the result as a new document in the |
Optimization and conversion
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 |
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
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
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
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);