C# SDK
Prerequisites
Requires .NET Framework 4.5 or later, or .NET Core 2.0 or later.
Install the OpenAPI SDK core library.
Option 1: Install using the .NET CLI. To install a specific version, add the --version flag; otherwise, the latest version is installed.
dotnet add package AlibabaCloud.OpenApiClientOption 2: Add the package reference to your .csproj file, and then run dotnet build to install the dependency.
<PackageReference Include="AlibabaCloud.OpenApiClient" Version="0.1.4" />
dotnet buildInstall the docmind-api SDK package and its dependencies.
.NET CLI:
dotnet add package AlibabaCloud.SDK.Docmind-api20220711 --version 1.4.14Package Manager:
Install-Package AlibabaCloud.SDK.Docmind-api20220711 -Version 1.4.14PackageReference:
<PackageReference Include="AlibabaCloud.SDK.Docmind-api20220711" Version="1.4.14" />Paket CLI:
paket add AlibabaCloud.SDK.Docmind-api20220711 --version 1.4.14F# Interactive:
#r "nuget: AlibabaCloud.SDK.Docmind-api20220711, 1.4.14"
Configure authentication
Create an AccessKey.
For more information, see Create an AccessKey.
ImportantAn Alibaba Cloud account AccessKey grants full access to all APIs. For enhanced security, we recommend using a RAM user for API calls and daily operations. To do this, grant the RAM user the necessary permissions to access docmind-api, and then use the RAM user's AccessKey to make API calls. For more information, see Control permissions on docmind-api by using RAM users.
Do not hardcode your AccessKey ID and AccessKey Secret in your project's source code. Doing so can lead to credential leaks and compromise the security of all resources under your account.
If you are concerned about exposing a RAM user's AccessKey, consider creating a RAM role and using a temporary STS credential to call the service. For details, see Call a service by using a temporary account authorized by STS.
Add a dependency on the Alibaba Cloud SDK Credentials package.
Install the package by using the NuGet Package Manager:
In the
Solution Explorerpane, right-click your project and selectManage NuGet Packages. In theManage NuGet Packageswindow that opens, click theBrowsetab and enterAliyun.Credentials. From the search results, select the package where theAuthorsfield isAlibaba Cloud, and then click Install.Configure your credential.
This guide uses a configuration file as an example. For detailed instructions, see Configuration methods.
Submit a document processing task
You can call the docmind-api asynchronous task API in two ways: by uploading a local file or by providing a document URL.
For large files that may require a long processing time, configure the following properties on the config object.
// Connection timeout
config.ConnectTimeout = 60000;
// Read timeout
config.ReadTimeout = 60000;For a complete list of APIs provided by docmind-api, see the API overview.
Submit a task with a local file
The APIs for converting images to Word, Excel, or PDF formats do not support file uploads.
The following code example shows how to call an asynchronous submission API by uploading a local file.
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
public static void Main(string[] args)
{
// When you call an API, the program directly accesses the credential,
// reads your AccessKey, and automatically performs authentication.
// Before you run this example, complete the "Configure authentication" step.
// This example uses a default configuration file to create a default access credential.
// Initialize the Credentials Client with the default credential.
var akCredential = new Aliyun.Credentials.Client(null);
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// Get the AccessKey ID from the configuration.
AccessKeyId = akCredential.GetAccessKeyId(),
// Get the AccessKey Secret from the configuration.
AccessKeySecret = akCredential.GetAccessKeySecret(),
};
// The domain name for the service endpoint.
config.Endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
AlibabaCloud.SDK.Docmind_api20220711.Client client = new AlibabaCloud.SDK.Docmind_api20220711.Client(config);
// An additional dependency is required: AlibabaCloud.DarabonbaStream.
Stream bodySyream = AlibabaCloud.DarabonbaStream.StreamUtil.ReadFromFilePath("<YOUR-FILE-PATH>");
AlibabaCloud.SDK.Docmind_api20220711.Models.SubmitDocStructureJobAdvanceRequest request = new AlibabaCloud.SDK.Docmind_api20220711.Models.SubmitDocStructureJobAdvanceRequest
{
FileUrlObject = bodySyream,
FileNameExtension = "pdf"
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
// If you copy this code, print the API return value yourself.
client.SubmitDocStructureJobAdvance(request, runtime);
}
catch (TeaException error)
{
// If necessary, print the error.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// If necessary, print the error.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
}
}
}The following response is returned.
{
"requestId": "B2969F25-1A0B-5F08-9A16-EE694AD0****",
"data": {
"id": "docmind-20220922-b15f****"
}
}Submit a task with a document URL
The document URL must be a publicly accessible URL without cross-domain restrictions or special escaped characters.
The following code example shows how to call an asynchronous submission API by providing a document URL.
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
public static void Main(string[] args)
{
// When you call an API, the program directly accesses the credential,
// reads your AccessKey, and automatically performs authentication.
// Before you run this example, complete the "Configure authentication" step.
// This example uses a default configuration file to create a default access credential.
// Initialize the Credentials Client with the default credential.
var akCredential = new Aliyun.Credentials.Client(null);
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// Get the AccessKey ID from the configuration.
AccessKeyId = akCredential.GetAccessKeyId(),
// Get the AccessKey Secret from the configuration.
AccessKeySecret = akCredential.GetAccessKeySecret(),
};
// The domain name for the service endpoint.
config.Endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
AlibabaCloud.SDK.Docmind_api20220711.Client client = new AlibabaCloud.SDK.Docmind_api20220711.Client(config);
AlibabaCloud.SDK.Docmind_api20220711.Models.SubmitDocStructureJobRequest request = new AlibabaCloud.SDK.Docmind_api20220711.Models.SubmitDocStructureJobRequest
{
FileUrl = "https://example.pdf",
FileNameExtension = "pdf"
};
try
{
// If you copy this code, print the API return value yourself.
client.SubmitDocStructureJob(request);
}
catch (TeaException error)
{
// If necessary, print the error.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// If necessary, print the error.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
}
}
}
The following response is returned.
{
"requestId": "43A29C77-405E-4CC0-BC55-EE694AD0****",
"data": {
"id": "docmind-20220712-b15f****"
}
}Query the task result
A task's status can be processing, successful, or failed.
The following code example shows how to call a result query API.
var akCredential = new Aliyun.Credentials.Client(null);using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
public static void Main(string[] args)
{
// When you call an API, the program directly accesses the credential,
// reads your AccessKey, and automatically performs authentication.
// Before you run this example, complete the "Configure authentication" step.
// This example uses a default configuration file to create a default access credential.
// Initialize the Credentials Client with the default credential.
var akCredential = new Aliyun.Credentials.Client(null);
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// Get the AccessKey ID from the configuration.
AccessKeyId = akCredential.GetAccessKeyId(),
// Get the AccessKey Secret from the configuration.
AccessKeySecret = akCredential.GetAccessKeySecret(),
};
// The domain name for the service endpoint.
config.Endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
AlibabaCloud.SDK.Docmind_api20220711.Client client = new AlibabaCloud.SDK.Docmind_api20220711.Client(config);
AlibabaCloud.SDK.Docmind_api20220711.Models.GetDocStructureResultRequest request = new AlibabaCloud.SDK.Docmind_api20220711.Models.GetDocStructureResultRequest
{
Id = "docmind-20220902-824b****"
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
// If you copy this code, print the API return value yourself.
client.GetDocStructureResult(request);
}
catch (TeaException error)
{
// If necessary, print the error.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// If necessary, print the error.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
}
}
}
The following response indicates that the task is still processing. A false value for the completed field means the task is not finished. Continue polling until completed is true or you exceed the maximum polling time.
{
"requestId": "2AABD2C2-D24F-12F7-875D-683A27C3****",
"completed": false,
"code": "DocProcessing",
"message": "Document processing",
"hostId": "ocr-api.cn-hangzhou.aliyuncs.com",
"recommend": "https://next.api.aliyun.com/troubleshoot?q=DocProcessing&product=docmind-api"
}The following response indicates a successful task. The Completed field is true and the Status field is Success. Detailed results are in the Data node.
{
"Status": "Success",
"RequestId": "73134E1A-E281-1B2C-A105-D0EF****",
"Completed": true,
"Data": {
"styles": [{
"styleId": 0,
"underline": false,
"deleteLine": false,
"bold": true,
"italic": false,
"fontSize": 15,
"fontName": "黑体",
"color": "000000",
"charScale": 0.95
},
{
"styleId": 1,
"underline": false,
"deleteLine": false,
"bold": false,
"italic": false,
"fontSize": 12,
"fontName": "微软雅黑",
"color": "000000",
"charScale": 1
}
],
"layouts": [{
"text": "测试标题",
"index": 0,
"uniqueId": "xxxx9816e77caea338df554b80ab95c7",
"alignment": "center",
"pageNum": [
0
],
"pos": [{
"x": 405,
"y": 192
},
{
"x": 860,
"y": 191
},
{
"x": 860,
"y": 236
},
{
"x": 406,
"y": 237
}
],
"type": "title",
"subType":"doc_title"
}, {
"text": "本段为测试内容",
"index": 1,
"uniqueId": "xxxx8606c213c01c12d70f98dcfb2525",
"alignment": "left",
"pageNum": [
0
],
"pos": [{
"x": 187,
"y": 311
},
{
"x": 1075,
"y": 311
},
{
"x": 1076,
"y": 373
},
{
"x": 187,
"y": 373
}
],
"type": "text",
"subType":"para",
"lineHeight": 7,
"firstLinesChars": 30,
"blocks": [{
"text": "本段",
"pos": null,
"styleId": 0
},
{
"text": "为测试内容",
"pos": null,
"styleId": 1
}
]
}],
"logics": {
"docTree": [{
"uniqueId": "xxxx9816e77caea338df554b80ab95c7",
"level": 0,
"link": {
"下级": [
],
"包含": [
]
},
"backlink": {
"上级": [
"ROOT"
]
}
}],
"paragraphKVs": null,
"tableKVs": null
},
"docInfo": {
"docType": "pdf",
"orignalDocName": "1.pdf",
"pages": [{
"imageType": "JPEG",
"imageUrl": "http://test.moshi.aliyuncs.com/docMind/image/xxxx3cccbfec45b48d3a8081c9c9659e/0",
"angle": null,
"imageWidth": 1273,
"imageHeight": 1801,
"pageIdCurDoc": 1,
"pageIdAllDocs": 1
}]
}
}
}The following response indicates a failed task. Although the completed field is true, the status is Fail. The response includes an error code and a detailed message. For more information about error codes, see Error codes.
{
"requestId": "A8EF3A36-1380-1116-A39E-B377BE27****",
"completed": true,
"status": "Fail"
"code": "UrlNotLegal",
"message": "Failed to process the document. The document url you provided is not legal.",
"hostId": "docmind-api.cn-hangzhou.aliyuncs.com",
"recommend": "https://next.api.aliyun.com/troubleshoot?q=IDP.UrlNotLegal&product=docmind-api"
}