PDF samples
Basics
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);
Once you are logged in to the web services, the session is kept in the webservice object. You can then use all other API methods.
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
getImage
Description:
| Method | Return values | Description |
|---|---|---|
|
getImage(String uuid, int page, String size) |
InputStream |
Returns the image of the specified page. |
|
The document must be a PDF or convertible to PDF. The default value of the size parameter is "150". The value is in pixels. The value of uuid is the UUID of the document. The page is the page number in the document, starting from 1. The size parameter is optional. When set, 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 user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, 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();
}
}
}
split
Description:
| Method | Return values | Description |
|---|---|---|
| split(String uuid, String dstId, String baseName, List<Integer> pages) |
List<Document> |
Returns a list of documents resulting from the split operation. |
|
The document must be a PDF or convertible to PDF. The value of uuid is the UUID of the 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 create the split documents. For example, if baseName is "test" and pages is [1, 3], the files created will be "test-001.pdf" and "test-003.pdf". The value of pages is the list of page numbers to split. |
||
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
Description:
| Method | Return values | Description |
|---|---|---|
|
extract(String uuid, String dstId, String name, List<Integer> pages) |
Document |
Returns the new document created with the extracted pages. |
|
The document must be a PDF or convertible to PDF. 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
Description:
| Method | Return values | Description |
|---|---|---|
|
remove(String uuid, String dstId, String name, List<Integer> pages) |
Document |
Returns a new document with the specified pages removed. |
|
The document must be a PDF or convertible to PDF. 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
Description:
| Method | Return values | Description |
|---|---|---|
|
rotate(String uuid, String dstId, String name, Integer angle, List<Integer> pages) |
Document |
Returns a new document with the specified pages rotated. |
|
The document must be a PDF or convertible to PDF. 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
Description:
| Method | Return values | Description |
|---|---|---|
|
insertPages(String uuid, InsertPagesRequest request) |
Document |
Returns the new document created with the inserted pages. |
|
The document must be a PDF or convertible to PDF. The value of uuid is the UUID of the target document where the pages will be inserted. The InsertPagesRequest object contains the following fields:
Each PageInsertOperation object contains the following fields:
|
||
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");
// Create 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
Description:
| Method | Return values | Description |
|---|---|---|
|
optimize(String uuid) |
boolean |
Returns true when the document has been optimized successfully. |
|
The UUID must correspond to a PDF document. |
||
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 user = "okmAdmin";
String password = "admin";
OKMWebservices ws = OKMWebservicesFactory.getInstance(host);
try {
ws.login(user, password);
boolean success = ws.pdf.optimize("cd4f69ed-e517-408b-b7eb-95cfe371ecba");
System.out.println("optimize success: " + success);
} catch (Exception e) {
e.printStackTrace();
}
}
}