智能文档解析
输出.Json格式文件
注意:在学习使用不同函数之前,建议先阅读请求描述,了解基本的PDF处理流程。使用不同函数时,可以在上传文件时设置各自特殊的参数。其他基本步骤一致。
智能文本提取:
java
{
"version": "v2"
}
所需参数
version
:PDF 转 JSON 不同版本(v1、v2),选择 v2 时使用智能文档解析。默认 v1。
示例
授权
您需要将认证响应中的 和 替换为从控制台获取的 publicKey 和 secretKey,并使用 accessToken。
curlcurl --location --request POST 'https://api-server.compdf.com/server/v1/oauth/token' \ --header 'Content-Type: application/json' \ --data-raw '{ "publicKey": "publicKey", "secretKey": "secretKey" }'
javaimport java.io.*; import okhttp3.*; public class main { public static void main(String []args) throws IOException{ OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, "{\n \"publicKey\": \"{{public_key}}\",\n \"secretKey\": \"{{secret_key}}\"\n}"); Request request = new Request.Builder() .url("https://api-server.compdf.com/server/v1/oauth/token") .method("POST", body) .build(); Response response = client.newCall(request).execute(); } }
创建任务
您需要将 替换为上一步获取的 accessToken,将 ***替换为所需的界面和任务错误消息语言类型。请求成功后,您将从响应中收到 taskId。
curlcurl --location --request GET 'https://api-server.compdf.com/server/v1/task/pdf/json' \ --header 'Authorization: Bearer accessToken'
javaimport java.io.*; import okhttp3.*; public class main { public static void main(String []args) throws IOException{ OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("https://api-server.compdf.com/server/v1/task/pdf/json?language={{language}}") .method("GET", body) .addHeader("Authorization", "Bearer {{accessToken}}") .build(); Response response = client.newCall(request).execute(); } }
上传文件
将 替换为您要转换的文件,将 替换为您上一步获取到的 taskId,将 替换为您需要的界面错误信息语言类型,将 替换为您第一步获取到的 accessToken。
curlcurl --location --request POST 'https://api-server.compdf.com/server/v1/file/upload' \ --header 'Authorization: Bearer accessToken' \ --form 'file=@"test.pdf"' \ --form 'taskId="taskId"' \ --form 'password=""' \ --form 'parameter="{ \"lang\": \"auto\" }"' \
javaimport java.io.*; import okhttp3.*; public class main { public static void main(String []args) throws IOException{ OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("file","{{file}}", RequestBody.create(MediaType.parse("application/octet-stream"), new File("<file>"))) .addFormDataPart("taskId","{{taskId}}") .addFormDataPart("language","{{language}}") .addFormDataPart("parameter","{ \"lang\": \"auto\" }") .build(); Request request = new Request.Builder() .url("https://api-server.compdf.com/server/v1/file/upload") .method("POST", body) .addHeader("Authorization", "Bearer {{accessToken}}") .build(); Response response = client.newCall(request).execute(); } }
执行任务
将 替换为 创建任务 步骤中获取的 taskId,将 替换为第一步获取的 access_token,将 替换为所需的界面错误信息语言类型。
curlcurl --location -g --request GET 'https://api-server.compdf.com/server/v1/execute/start?taskId=taskId' \ --header 'Authorization: Bearer accessToken'
javaimport java.io.*; import okhttp3.*; public class main { public static void main(String []args) throws IOException{ OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("https://api-server.compdf.com/server/v1/execute/start?taskId={{taskId}}&language={{language}}") .method("GET", body) .addHeader("Authorization", "Bearer {{accessToken}}") .build(); Response response = client.newCall(request).execute(); } }
获取任务信息
将 替换为 创建任务 步骤中获取的 taskId,将 替换为第一步中获取的 access_token。
curlcurl --location -g --request GET 'https://api-server.compdf.com/server/v1/task/taskInfo?taskId=taskId' \ --header 'Authorization: Bearer accessToken'
javaimport java.io.*; import okhttp3.*; public class main { public static void main(String []args) throws IOException{ OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("https://api-server.compdf.com/server/v1/task/taskInfo?taskId={{taskId}}") .method("GET", body) .addHeader("Authorization", "Bearer {{accessToken}}") .build(); Response response = client.newCall(request).execute(); } }
结果
文件类型 | 说明 |
---|---|
.Zip | 该zip文件包含Json结果文件和图片文件夹。 |
内容
参数 | 说明 |
---|---|
rect | 对象在页面中的位置 |
page | 对象所在的页码 |
order_index | 对象在当前页面的阅读顺序位置 |
type | 用于标识对象的类型。目前支持的对象类型有: Text:普通文本类型对象,包含文本内容。 Image:图片类型对象,包含图片的路径。 Table和UnstdTable:表格类型对象,包含表格的内容和结构。 Catalogue:目录类型对象,包含目录的内容 List和UnorderedList:列表类型对象,包含列表的内容 Formula:公式类型对象,包含公式的内容 Header:页眉类型对象,包含页眉的内容 Footer:页脚类型对象,包含页脚的内容 PageNumber:页码类型对象,包含页码的内容 FigureTitle:图形标题类型对象,包含图形标题的内容 FigureCaption:图形标题类型对象,包含图形标题的内容 |
java
{
"version": "1.0.0",
"objects": [
{
"type": "Header",
"rect": [
49.0,
43.5,
171.5,
76.0
],
"text": "Intelligent Document Parsing",
"page": 0,
"order_index": 0
}
]
}