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:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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 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

fillForm

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

decrypt(File input, String ownerPassword)

File

Decrypts a PDF file and returns a new temporary file with the decrypted content.

isEncrypted (InputStream)

Description:

MethodReturn valuesDescription

isEncrypted(InputStream input)

boolean

Returns true if the PDF stream is encrypted.

isEncrypted (File)

Description:

MethodReturn valuesDescription

isEncrypted(File input)

boolean

Returns true if the PDF file is encrypted.

disableEncryption (streams)

Description:

MethodReturn valuesDescription

disableEncryption(InputStream input, OutputStream output)

void

Removes the encryption flag from a PDF without requiring the owner password, allowing subsequent modification.

disableEncryption (file)

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

getNumberOfPages(File pdf)

int

Returns the number of pages in a local PDF file.

getNumberOfPages (InputStream)

Description:

MethodReturn valuesDescription

getNumberOfPages(InputStream stream)

int

Returns the number of pages in a PDF stream.

getNumberOfPages (repository document)

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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 baseName.

split (repository document)

Description:

MethodReturn valuesDescription

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 dstId folder. Returns the list of created documents.

extract (file)

Description:

MethodReturn valuesDescription

extract(File src, File dst, List<Integer> pages)

void

Extracts the specified pages from a PDF file and writes the result to dst.

extract (repository document)

Description:

MethodReturn valuesDescription

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 docName in the dstId folder. Returns the created document.

rotate (file)

Description:

MethodReturn valuesDescription

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 dst.

rotate (repository document)

Description:

MethodReturn valuesDescription

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 dstId folder. Returns the created document.

Optimization and conversion

optimize (file)

Description:

MethodReturn valuesDescription

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)

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

generateSample(int paragraphs, OutputStream os)

void

Generates a sample Lorem Ipsum PDF with the specified number of paragraphs.

createPdfWithText

Description:

MethodReturn valuesDescription

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);