PDFUtils
Utility methods to convert PDF files. For example, to stamp 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 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.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
Description:
Method | Return values | Description |
---|---|---|
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.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
Description:
Method | Return values | Description |
---|---|---|
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.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
Description:
Method | Return values | Description |
---|---|---|
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 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();
}
}
}