PDFUtils

Utility methods to convert PDF files. For example, to stamp image or text. 

Methods

merge

Description:

MethodReturn valuesDescription

merge(List<InputStream> inputs, OutputStream output)

void

Merge several PDFs into a new one.

inputs: List of inputs stream for merge.

output: Output stream of the file.

Example:

package com.openkm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;

import com.openkm.api.OKMDocument;
import com.openkm.bean.Document;
import com.openkm.util.PDFUtils;
import com.openkm.util.PathUtils;
import com.openkm.util.FileUtils;

public class Test {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        File tmp = null;
        try {
            List<InputStream> inputs = new ArrayList<InputStream>();	
            tmp = FileUtils.createTempFile("pdf");
            fos = new FileOutputStream(tmp);

            inputs.add(OKMDocument.getInstance().getContent(null, "/home/openkm/test1.pdf", false));
            inputs.add(OKMDocument.getInstance().getContent(null, "/home/openkm/test2.pdf", false));

            // Merge document
            PDFUtils.merge(inputs, fos);

            // Create document in repository
            String fldPath = PathUtils.getParent("/home/openkm/test1.pdf"); // all documents are in same path
            String docPath = fldPath + "/docMerge";
            fis = new FileInputStream(tmp);
            OKMDocument.getInstance().createSimple(null, docPath, fis);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis);
            IOUtils.closeQuietly(fos);
            FileUtils.deleteQuietly(tmp);
        }
    }
}

stampImage

Description:

MethodReturn valuesDescription

stampImage(InputStream input, byte[] image, OutputStream output)

void

Stamp PDF document with image watermark.

input: The input stream of the file.

image: The content of the file.

output: Output stream of the file.

Example:

package com.openkm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.openkm.util.PDFUtils;

public class Test {
    public static void main(String[] args) {
        try {
            String path = "/home/openkm/test.pdf";
            File file = new File(path);
            InputStream is = new FileInputStream(file);
            OutputStream out = new FileOutputStream(file);
            byte[] data = Files.readAllBytes(Paths.get("/home/luis/test.png"));
            PDFUtils.stampImage(is, data, out);
            is.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

stampText

Description:

MethodReturn valuesDescription

stampText(InputStream input, String text, OutputStream output)

void

Stamp PDF document with text watermark.

input: The input stream of the file.

text: A text for the stamp.

output: Output stream of the file.

Example:

package com.openkm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.openkm.util.PDFUtils;

public class Test {
    public static void main(String[] args) {
        try {
            String path = "/home/openkm/test.pdf";
            File file = new File(path);
            InputStream is = new FileInputStream(file);
            OutputStream out = new FileOutputStream(file);
            PDFUtils.stampText(is, "This is the text", out);
            is.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

getNumberOfPages

Description:

MethodReturn valuesDescription

getNumberOfPages(String token, String docId)

int

Return the number of pages for a PDF.

input: The input stream of the file.

text: A text for the stamp.

docId: PDF OpenKM unique identifier.

Example:

package com.openkm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.openkm.util.PDFUtils;

public class Test {
    public static void main(String[] args) {
        try {
            String docId = "e6b61e8d-4bf1-4c37-828f-09ecc34261cc";
            System.out.prinln(PDFUtils.getNumberOfPages(null, docId));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}