Conversion samples

Basic

Suggested code sample

First, you must create the webservice object:

OKMWebservices ws = OKMWebservicesFactory.newInstance(host);

Then you should log in using the method "login". You can access the "login" method from the web service object "ws" as shown below:

ws.login(user, password);

Once you are logged in to the web service, the session is kept in the web service object. Then you can use the other API methods.

At this point you can use all the Conversion methods from the "conversion" class as shown below:

ws.conversion.doc2pdf(is, "test.docx");

Methods

doc2pdf

Description:

MethodReturn valuesDescription

doc2pdf(FileStream is, String fileName)

Stream

Retrieves the uploaded document converted to PDF format.

The parameter fileName is the document file name. The application uses this parameter to identify the document MIME type by the document extension.

The OpenOffice service must be enabled on the OpenKM server for it to run.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.util;
using System.IO;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8080/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);
try {
ws.login(user, password); FileStream inputFile = new FileStream("C:\\Documents\\test.docx", FileMode.Open); Stream tmpFile= ws.conversion.doc2pdf(inputFile, "test.docx"); inputFile.Close(); FileStream outputFile = new FileStream("C:\\Documents\\out.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
tmpFIle.CopyTo(outputFile);
outputFile.Close();
tmpFIle.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

imageConvert

Description:

MethodReturn valuesDescription

imageConvert(FileStream fs, String fileName, List<String> params, String dstMimeType)

Stream

Retrieve the uploaded image with transformations.

The variable fileName is the document file name. The application uses this variable to identify the document MIME type by the document extension.

The parameter dstMimeType is the expected document MIME type of the result.

Using this method, you are actually executing the ImageMagick convert tool on the server side.

You can set many parameters - transformations - in the params variable. Take a look at ImageMagick convert tool to get a complete list of them.

The image convert tool must be enabled on the OpenKM server for it to run.

When the params value is not empty, it must always end with the chain "${fileIn} ${fileOut}".

Ensure there is only one whitespace character as a separator between two parameters.

When building your integrations, we suggest installing ImageMagick software locally and checking your image transformations first from the command line.

Example:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.util;
using System.IO;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8080/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);
try {
ws.login(user, password); FileStream fileStream = new FileStream("C:\\Documents\\test.jpg", FileMode.Open, FileAccess.Read);
List<string> param = new List<string>();
param.Add("-resize 50% ${fileIn} ${fileOut}");
Stream tmpFIle = ws.conversion.imageConvert(fileStream, test.jpg, param, "image/png");
fileStream.Close();
FileStream destFile = new FileStream("C:\\Documents\\test.png", FileMode.OpenOrCreate);
tmpFIle.CopyTo(destFile);
destFile.Close();
tmpFIle.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

html2pdf

Description:

MethodReturn valuesDescription

html2pdf(String url)

Stream

Retrieves the PDF from a URL.

The HTML to PDF conversion tool must be enabled on the OpenKM server for it to run.

Example:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.util;
using System.IO;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8080/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);
try {
ws.login(user, password); Stream tmpFile = ws.conversion.html2pdf("http://www.openkm.com");
FileStream destFile = new FileStream("C:\\Documents\\test.pdf", FileMode.OpenOrCreate);
tmpFile.CopyTo(destFile);
destFile.Close();
tmpFile.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

doc2txt

Description:

MethodReturn valuesDescription

doc2txt(FileStream fs, String fileName)

String

Extracts the text from the uploaded document.

A Text Extractor must be enabled for the document MIME type on the OpenKM server for it to run.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using System.IO;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8080/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);
try {
ws.login(user, password); FileStream fileStream = new FileStream("C:\\Documents\\test.docx", FileMode.Open); System.Console.WriteLine(ws.conversion.doc2txt(fileStream, "test.docx")); fileStream.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

img2txt

Description:

MethodReturn valuesDescription

img2txt(FileStream fs, String fileName)

String

Extracts the text from the uploaded image.

The OCR engine must be enabled on the OpenKM server for it to run.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using System.IO;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8080/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);
try {
ws.login(user, password); FileStream fileStream = new FileStream("C:\\Images\\test.png", FileMode.Open); System.Console.WriteLine(ws.conversion.img2txt(fileStream, "test.png")); fileStream.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

barcode2txt

Description:

MethodReturn valuesDescription

barcode2txt(FileStream fs, String fileName)

String

Extracts the barcode text from the uploaded image.

The Barcode tool must be enabled on the OpenKM server for it to run.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using System.IO;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8080/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);
try {
ws.login(user, password); FileStream fileStream = new FileStream("C:\\Images\\test.png", FileMode.Open); System.Console.WriteLine(ws.conversion.barcode2txt(fileStream, "test.png")); fileStream.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }