ConvertToBase64

Description

Converts the images of specified indices in buffer to a base64 string.

Syntax

.ConvertToBase64(indices, enumImageType, asyncSuccessFunc, asyncFailureFunc);

Parameters

<int[]> indices: specifies the indices of images in buffer. The index is 0-based. If you are converting to BMP/JPG/PNG, make sure you pass only one index. Multiple indices are valid only for TIF/PDF.
EnumDWT_ImageType enumImageType: specifies the format.

Type Numeric Value
IT_BMP 0
IT_JPG 1
IT_TIF 2
IT_PNG 3
IT_PDF 4

<OnSuccess function> asyncSuccessFunc: callback function triggered when the file is converted successfully.
<OnFailure function> asyncFailureFunc: callback function triggered when the file fails to be converted.

Please refer to the function prototype OnSuccess or OnFailure.

Return value

<Base64Result> is returned in the callback asyncSuccessFunc().

Methods available in <Base64Result> object

Data Type Method Name Description
<int> getLength() Returns the length of the Base64 String
<string> getData(offset, length) Returns the string containing the extracted part of the Base64 string. The parameter offset means the position where to start the extraction. The parameter length means the number of characters to extract.
<string> getMD5() Returns the MD5 of the Base64 String.

The result string returned from getData is the pure base64 string with no extra info. For example,
"/9j/4AAQSkZJRgABA...".
To use the string in most circumstances, you need to add extra info like
data:image/png;base64,"/9j/4AAQSkZJRgABA...".

Because this method is async only, the returned value doesn't mean if the convert succeeds or not. When convert fails, check ErrorCode or ErrorString for error information.

Remarks

This method is valid only in HTML5 Edtion

Example

JavaScript:

/*Convert the 1st and 4th images to a base64-encoded PDF*/
DWObject.ConvertToBase64 ([1,3], EnumDWT_ImageType.IT_PDF, asyncSuccessFunc, asyncFailureFunc);
function asyncSuccessFunc (result) {
     var length=result.getLength();
     console.log(result.getData(0,length));
     console.log(result);
}
function asyncFailureFunc (errorCode, errorString) {
     alert("ErrorCode: "+errorCode +"\r"+"ErrorString:"+ errorString);
}