PDF samples
Basics
Section titled “Basics”Suggested code sample
Section titled “Suggested code sample”First, you must create the webservice object:
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);Then you should log in using the login method. You can access it from the webservice object ws as shown below:
ws.login(user, password);At this point you can use all the PDF methods from the pdf class as shown below:
InputStream is = ws.pdf.getImage("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", 1, null);Methods
Section titled “Methods”getImage
Section titled “getImage”Description:
| Method | Return values | Description |
|---|---|---|
| getImage(String uuid, int page, String size) | InputStream | Returns an image of the specified page. |
The value of uuid is the UUID of the document.
The page parameter is the page number in the document, starting from 1.
The size parameter is optional. When provided, it resizes the page image; otherwise, the default value is used.
Example:
package com.openkm;
import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password);
InputStream is = ws.pdf.getImage("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", 1, null); OutputStream fos = new FileOutputStream("/home/openkm/page-1.png"); IOUtils.copy(is, fos); IOUtils.closeQuietly(is); IOUtils.closeQuietly(fos); } catch (Exception e) { e.printStackTrace(); } }}Description:
| Method | Return values | Description |
|---|---|---|
| split(String uuid, String dstId, String baseName, List |
List |
Returns a list of documents resulting from the split operation. |
The value of uuid is the UUID of the source document.
The value of dstId is the UUID of the destination folder. If not provided, the parent folder of the source document is used.
The value of baseName is the base name used to generate the split documents. For example, if baseName is “test” and pages is [1, 3], the resulting files will be named “test-001.pdf” and “test-003.pdf”.
The value of pages is the list of page numbers at which to split the document.
Example:
package com.openkm;
import java.util.ArrayList;import java.util.List;import com.openkm.sdk4j.bean.*;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password); List<Integer> pages = new ArrayList<>(); pages.add(2); pages.add(3); List<Document> documents = ws.pdf.split("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", "0be057f1-efac-4e09-9c10-e7f27c731442", "split", pages); System.out.println(documents); } catch (Exception e) { e.printStackTrace(); } }}extract
Section titled “extract”Description:
| Method | Return values | Description |
|---|---|---|
| extract(String uuid, String dstId, String name, List |
Document | Returns the new document created with the extracted pages. |
The value of uuid is the UUID of the source document.
The value of dstId is the UUID of the destination folder. If not provided, the parent folder of the source document is used.
The value of name is the name of the new document containing the extracted pages.
The value of pages is the list of page numbers to be extracted.
Example:
package com.openkm;
import java.util.ArrayList;import java.util.List;import com.openkm.sdk4j.bean.*;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;
public class PdfExample {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password); List<Integer> pages = new ArrayList<>(); pages.add(2); pages.add(3); Document document = ws.pdf.extract("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", "0be057f1-efac-4e09-9c10-e7f27c731442", "extract.pdf", pages); System.out.println(document); } catch (Exception e) { e.printStackTrace(); } }}remove
Section titled “remove”Description:
| Method | Return values | Description |
|---|---|---|
| remove(String uuid, String dstId, String name, List |
Document | Returns a new document with the specified pages removed. |
The value of uuid is the UUID of the source document.
The value of dstId is the UUID of the destination folder. If not provided, the parent folder of the source document is used.
The value of name is the name of the new document.
The value of pages is the list of page numbers to be removed.
Example:
package com.openkm;
import java.util.ArrayList;import java.util.List;import com.openkm.sdk4j.bean.*;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;
public class PdfExample {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password); List<Integer> pages = new ArrayList<>(); pages.add(2); pages.add(3); Document document = ws.pdf.remove("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", "0be057f1-efac-4e09-9c10-e7f27c731442", "remove.pdf", pages); System.out.println(document); } catch (Exception e) { e.printStackTrace(); } }}rotate
Section titled “rotate”Description:
| Method | Return values | Description |
|---|---|---|
| rotate(String uuid, String dstId, String name, Integer angle, List |
Document | Returns a new document with the specified pages rotated. |
The value of uuid is the UUID of the source document.
The value of dstId is the UUID of the destination folder. If not provided, the parent folder of the source document is used.
The value of name is the name of the new document.
The value of angle is the rotation angle applied to the specified pages.
The value of pages is the list of page numbers to be rotated.
Example:
package com.openkm;
import java.util.ArrayList;import java.util.List;import com.openkm.sdk4j.bean.*;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;
public class PdfExample {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password); List<Integer> pages = new ArrayList<>(); pages.add(2); pages.add(3); Document document = ws.pdf.rotate("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", "0be057f1-efac-4e09-9c10-e7f27c731442", "rotate.pdf", 180, pages); System.out.println(document); } catch (Exception e) { e.printStackTrace(); } }}insertPages
Section titled “insertPages”Description:
| Method | Return values | Description |
|---|---|---|
| insertPages(String uuid, InsertPagesRequest request) | Document | Returns the new document created with the inserted pages. |
The value of uuid is the UUID of the target document where the pages will be inserted.
The InsertPagesRequest object contains the following fields:
- insertOperations: a list of PageInsertOperation objects, each describing a single insertion step.
- dstId: the UUID of the destination folder for the resulting document. If not provided, the parent folder of the target document is used.
- name: the name of the new document to be generated.
Each PageInsertOperation object contains the following fields:
- srcId: the UUID of the source document from which pages will be taken.
- pages: the list of page numbers to take from the source document.
- insertFromPage: the position in the target document at which the pages will be inserted.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;import com.openkm.sdk4j.bean.Document;import com.openkm.sdk4j.bean.InsertPagesRequest;import com.openkm.sdk4j.bean.PageInsertOperation;
import java.util.Arrays;
public class PdfExample {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password);
// Create the insert pages request InsertPagesRequest request = new InsertPagesRequest(); request.setName("merged_document.pdf"); request.setDstId("e7d6860a-7e0b-4823-bd3d-75f88be1eedf");
// Define a page insertion operation PageInsertOperation operation = new PageInsertOperation(); operation.setSrcId("a573da75-ba1e-4f5b-98d2-fbb7da5f1cf1"); // Source document UUID operation.setPages(Arrays.asList(1, 2, 3)); // Pages to take from source operation.setInsertFromPage(1); // Insert at position 1 in target
request.getInsertOperations().add(operation);
// Insert pages into the target document Document result = ws.pdf.insertPages("6a54d98b-6454-4484-8172-8359a2b4f2f2", request); System.out.println("New document created: " + result.getUuid()); } catch (Exception e) { e.printStackTrace(); } }}optimize
Section titled “optimize”Description:
| Method | Return values | Description |
|---|---|---|
| optimize(String uuid) | boolean | Returns true when the document has been optimized successfully. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.impl.OKMWebservices;
public class Test {
public static void main(String[] args) { String host = "http://localhost:8080/openkm"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try { ws.login(username, password); boolean success = ws.pdf.optimize("cd4f69ed-e517-408b-b7eb-95cfe371ecba"); System.out.println("optimize success: " + success); } catch (Exception e) { e.printStackTrace(); } }}