PDFUtils
Utility methods to convert PDF files. For example, to stamp an image or text.
Methods
merge
Description:
| Method | Return values | Description |
|---|---|---|
|
merge(List<InputStream> inputs, OutputStream output) |
void |
Merge several PDFs into a new one. |
|
Inputs: List of input streams to merge. output: Output stream for 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);
}
}
}
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();
}
}
}
fillForm
Description:
| Method | Return values | Description |
|---|---|---|
|
fillForm(InputStream input, Map<String, Object> values, OutputStream output) |
void |
Fill the PDF form with values. |
|
input: The input stream of the PDF file with the form. values: A map of values to be used to fill out the form. output: The generated PDF with the form filled in. |
||
Example using this file FillForm.pdf:
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);
InputStream fis = new FileInputStream("/home/openkm/FillForm.pdf");
OutputStream fos = new FileOutputStream("/home/openkm/FillFormOutput.pdf");
Map<String, Object> values = new HashMap<>();
values.put("TextField_1", "Field #1 value");
values.put("TextField_2", "Field #2 value");
values.put("TextField_3", "Field #3 value");
pdfUtils.fillForm(fis, values, fos);
} catch (Exception e) {
e.printStackTrace();
}
}
}