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 method "login". You can access the "login" method 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. Then you can use the 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);

Methods

getImage

Description:

MethodReturn valuesDescription

getImage(String uuid, int page, String size)

InputStream

Returns the image of a 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 the uuid is the UUID of the document.

The page is the number of the page in the document starting from 1.

The size value is optional. When set, it is used to resize the image of the page; 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:

MethodReturn valuesDescription
split(String uuid, String dstId, String baseName, List<Integer> pages)

List<Document>

Returns a list of documents from the split pages.

The document must be a PDF or convertible to PDF.

The value of the uuid is the UUID of the document.

The value of baseName is the base name used to create split documents. For example, if the baseName value is "test" and the pages value 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 be split.

Example:

package com.openkm;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.openkm.sdk4j.bean.*;

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);
            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:

MethodReturn valuesDescription

extract(String uuid, String dstId, String name, List<Integer> pages)

boolean

Returns true when the chosen pages of the document have been extracted.

The document must be a PDF or convertible to PDF.

The value of the uuid is the UUID of the document.

The value of the dstId is the UUID of the destination (folder or record).

The value of name is the name of the new document with 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.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);
            //extract
            List<Integer> pages = new ArrayList<>();
            pages.add(2);
            pages.add(3);
            if (ws.pdf.extract("e7d6860a-7e0b-4823-bd3d-75f88be1eedf", "0be057f1-efac-4e09-9c10-e7f27c731442", "extract", pages)) {
                System.out.println("extract done");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

remove

Description:

MethodReturn valuesDescription

remove(String uuid, String dstId, String name, List<Integer> pages)

Document

Returns a new document with the pages removed.

The document must be a PDF or convertible to PDF.

The value of the uuid is the UUID of the document.

The value of the dstId is the UUID of the destination (folder or record).

The value of name is the name of the new document with extracted pages.

The value of pages is the number of pages to be removed.

Example:

package com.openkm;

import java.util.ArrayList;
import java.util.List;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
import com.openkm.sdk4j.bean.*;

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);			
            // remove
            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", "remove", pages);
            System.out.println(document);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

rotate

Description:

MethodReturn valuesDescription

rotate(String uuid, String dstId, String name, String angle, List<Integer> pages)

Document

Returns a new document with the pages rotated.

The document must be a PDF or convertible to PDF.

The value of the uuid is the UUID of the document.

The value of the dstId is the UUID of the destination (folder or record).

The value of name is the name of the new document with extracted pages.

The value of angle is the rotation applied to the chosen pages.

The value of pages is the number of pages to be rotated.

Example:

package com.openkm;

import java.util.ArrayList;
import java.util.List;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
import com.openkm.sdk4j.bean.*;

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);
            // rotate
            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", "180", pages);
            System.out.println("rotation done");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

insertPages

Description:

MethodReturn valuesDescription

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 the uuid is the UUID of the target document where the pages will be inserted.

The value of InsertPagesRequest is an object that has List of PageInsertOperation named insertOperations, an String dstId, that contains the uuid of the destination folder, and a String name for the new generated document.

PageInsertOperation object has a String srcId, the UUID of the source file of the pagesa List of Integer named pages that stores the positions of the pages in the source PDF, and an int insertFromPage, the position where the pages have to be located in the target PDF

Example:

package com.openkm;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;
import com.openkm.sdk4j.bean.Document;
import com.openkm.ws.rest.request.InsertPagesRequest;
import com.openkm.ws.rest.request.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.name = "merged_document.pdf"; // Name for the new document
request.dstId = "e7d6860a-7e0b-4823-bd3d-75f88be1eedf";

// Create a page insertion operation
PageInsertOperation operation = new PageInsertOperation();
operation.srcId = "a573da75-ba1e-4f5b-98d2-fbb7da5f1cf1"; // Source document UUID
operation.pages = Arrays.asList(1, 2, 3); // Pages to insert from source
operation.insertFromPage = 1; // Insert at position 1 in target document

request.insertOperations.add(operation);

// Call insertPages with target document UUID and request
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:

MethodReturn valuesDescription

optimize(String uuid)

boolean

Returns true when optimized successfully.

The UUID of the node must be 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();
        }
    }
}