PDFUtils
Utility methods to convert PDF files. For example, to stamp images or text.
Methods
Section titled “Methods”Description:
| Method | Return values | Description |
|---|---|---|
| merge(List |
void | Merge several PDFs into a new one. |
- inputs: List of input streams for merging.
- 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.util.ContextWrapper;import com.openkm.util.FileUtils;import com.openkm.util.PDFUtils;import com.openkm.util.PathUtils;
public class Test {
public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; File tmp = null;
try { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class);
tmp = FileUtils.createTempFile("pdf"); fos = new FileOutputStream(tmp);
List<InputStream> inputs = new ArrayList<>(); inputs.add(okmDocument.getContent(null, "c0984ecf-8d9e-40f8-b5fb-2c83d202bd9d", false)); inputs.add(okmDocument.getContent(null, "b3d0af95-076c-4cc4-9b85-bd3e7eb2baa3", false));
// Merge document pdfUtils.merge(inputs, fos);
// Create document in repository String fldPath = PathUtils.getParent("/okm:root/test3.pdf"); // all documents are in same path String docPath = fldPath + "/docMerge.pdf"; fis = new FileInputStream(tmp); okmDocument.createSimple(null, docPath, fis); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); fileUtils.deleteQuietly(tmp); } }}stampImage
Section titled “stampImage”Description:
| Method | Return values | Description |
|---|---|---|
| stampImage(InputStream input, byte[] image, OutputStream output) | void | Stamp a PDF document with an image watermark. |
- input: The input stream of the file.
- image: The content of the image 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.ContextWrapper;import com.openkm.util.PDFUtils;
public class Test {
public static void main(String[] args) { try { PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class);
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
Section titled “stampText”Description:
| Method | Return values | Description |
|---|---|---|
| stampText(InputStream input, String text, OutputStream output) | void | Stamp a PDF document with a text watermark. |
- input: The input stream of the file.
- text: 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.ContextWrapper;import com.openkm.util.PDFUtils;
public class Test {
public static void main(String[] args) { try { PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class);
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
Section titled “getNumberOfPages”Description:
| Method | Return values | Description |
|---|---|---|
| getNumberOfPages(String token, String docId) | int | Returns the number of pages in a PDF. |
- input: The input stream of the file.
- text: Text for the stamp.
- docId: PDF OpenKM unique identifier.
Example:
package com.openkm;
import com.openkm.util.ContextWrapper;import com.openkm.util.PDFUtils;
public class Test {
public static void main(String[] args) { try { PDFUtils pdfUtils = ContextWrapper.getContext().getBean(PDFUtils.class); String docId = "e6b61e8d-4bf1-4c37-828f-09ecc34261cc"; System.out.println(pdfUtils.getNumberOfPages(null, docId)); } catch (Exception e) { e.printStackTrace(); } }}