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 webservice object "ws" as shown below:
ws.login(user, password);
Once you are logged in with the webservices, the session is kept in the webservice 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:
Method | Return values | Description |
---|---|---|
doc2pdf(FileStream is, String fileName) |
Stream |
Retrieves the uploaded document converted to PDF format. |
The parameter fileName is the document's file name. The application uses this parameter to identify the document MIME type by the file extension. The OpenOffice service must be enabled in 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:
Method | Return values | Description |
---|---|---|
imageConvert(FileStream fs, String fileName, List<String> params, String dstMimeType) |
Stream |
Retrieves the uploaded image after transformation. |
The variable fileName is the document's file name. The application uses this variable to identify the document MIME type by the file extension. The parameter dstMimeType is the expected resulting document MIME type. 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. See ImageMagick convert tool for a complete list of them. The image convert tool must be enabled in 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 a single 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 your 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:
Method | Return values | Description |
---|---|---|
html2pdf(String url) |
Stream |
Retrieves the PDF of a URL. |
The HTML to PDF conversion tool must be enabled in 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:
Method | Return values | Description |
---|---|---|
doc2txt(FileStream fs, String fileName) |
String |
Extracts the text from the uploaded document. |
A Text Extractor must be enabled for the document MIME type in 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:
Method | Return values | Description |
---|---|---|
img2txt(FileStream fs, String fileName) |
String |
Extracts the text from the uploaded image. |
The OCR engine must be enabled in 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:
Method | Return values | Description |
---|---|---|
barcode2txt(FileStream fs, String fileName) |
String |
Extracts the barcode text from the uploaded image. |
The Bar Code tool must be enabled in 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());
}
}
}
}