Skip to content

智能全文解析

输出.TXT格式文件

注意:在学习使用不同函数之前,建议先阅读请求描述,了解基本的PDF处理流程。使用不同函数时,可以在上传文件时设置各自特殊的参数。其他基本步骤一致。

智能文本提取:

java
{
  "enableAiLayout": 1,
  "isContainImg": 1,
  "isContainAnnot": 1,
  "enableOcr": 0,
  "ocrRecognitionLang": "AUTO",
  "pageRanges": "1,2,3-5",
  "txtTableFormat": 1
}

所需参数

enableAiLayout:是否开启AI版面分析(0: 不开启;1: 开启)。默认 1。

isContainImg:转换时是否包含图像(0: 不开启;1: 开启)。默认 1。

isContainAnnot:转换时是否包含注释(0: 不开启;1: 开启)。默认 1。

enableOcr:是否使用OCR(0: 不开启;1: 开启)。默认 0。

ocrRecognitionLang:OCR识别语言,支持的类型和定义:

AUTO: 自动,CHINESE: 中文简体,CHINESE_TRAD: 中文繁体,ENGLISH: 英语,KOREAN: 韩语,JAPANESE: 日语,LATIN: 拉丁语,DEVANAGARI: 梵文,CYRILLIC: 西里尔语,ARABIC: 阿拉伯语,TAMIL: 泰米尔语,TELUGU: 泰卢固语,KANNADA: 卡纳达语,THAI:泰语,GREEK:希腊语,ESLAV:eslav语系;默认 AUTO。

pageRanges:指定页码转换,从1开始。默认 空。

txtTableFormat:pdf转txt时是否格式化表格(0: 不开启;1: 开启)。默认 1。

Java 示例:

您需要将 apiKey 替换为您从控制台获取的 publicKey,将 file 替换为您要转换的文件 ,language 替换为您想要的接口错误提示语言类型。

  1. 授权

    您需要将认证响应中的 替换为从控制台获取的 publicKeysecretKey,并使用 accessToken

    curl
    curl --location --request POST 'https://api-server.compdf.com/server/v2/oauth/token' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "publicKey": "publicKey",
        "secretKey": "secretKey"
    }'
    java
     import 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/v2/oauth/token")
           .method("POST", body)
           .build();
         Response response = client.newCall(request).execute();
       }
     }
  2. 创建任务

    您需要将 替换为上一步获取的 accessToken,将 ***替换为所需的界面和任务错误消息语言类型。请求成功后,您将从响应中收到 taskId

    curl
    curl --location --request GET 'https://api-server.compdf.com/server/v2/task/pdf/txt' \
    --header 'Authorization: Bearer accessToken'
    java
     import 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/v2/task/pdf/txt?language={{language}}")
           .method("GET", body)
           .addHeader("Authorization", "Bearer {{accessToken}}")
           .build();
         Response response = client.newCall(request).execute();
       }
     }
  3. 上传文件

    file 替换为您要转换的文件,将 替换为您上一步获取到的 taskId,将 language 替换为您需要的界面错误信息语言类型,将 替换为您第一步获取到的 accessToken

    curl
    curl --location --request POST 'https://api-server.compdf.com/server/v2/file/upload' \
    --header 'Authorization: Bearer accessToken' \
    --form 'file=@"test.png"' \
    --form 'taskId="taskId"' \
    --form 'password=""' \
    --form 'parameter="{    \"lang\": \"auto\"    }"' \
    java
     import 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","{    \"enableAiLayout\": \"1\"    }")
           .build();
         Request request = new Request.Builder()
           .url("https://api-server.compdf.com/server/v2/file/upload")
           .method("POST", body)
           .addHeader("Authorization", "Bearer {{accessToken}}")
           .build();
         Response response = client.newCall(request).execute();
       }
     }
  4. 执行任务

    替换为 创建任务 步骤中获取的 taskId,将 替换为第一步获取的 access_token,将 language 替换为所需的界面错误信息语言类型。

    curl
    curl --location -g --request GET 'https://api-server.compdf.com/server/v2/execute/start?taskId=taskId' \
    --header 'Authorization: Bearer accessToken'
    java
     import 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/v2/execute/start?taskId={{taskId}}&language={{language}}")
          .method("GET", body)
          .addHeader("Authorization", "Bearer {{accessToken}}")
          .build();
        Response response = client.newCall(request).execute();
      }
     }
  5. 获取任务信息

    替换为 创建任务 步骤中获取的 taskId,将 替换为第一步中获取的 access_token

    curl
    curl --location -g --request GET 'https://api-server.compdf.com/server/v2/task/taskInfo?taskId=taskId' \
    --header 'Authorization: Bearer accessToken'
    java
     import 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/v2/task/taskInfo?taskId={{taskId}}")
           .method("GET", body)
           .addHeader("Authorization", "Bearer {{accessToken}}")
           .build();
         Response response = client.newCall(request).execute();
       }
     }

响应信息:

请求的成功响应返回 HTTP 200 OK 状态代码以及显示订单详细信息的 JSON 响应主体。

响应模式:application/json

响应参数数据类型描述
codeStringHTTP请求状态,"200"代表成功
messageString请求信息
dataObject返回结果
+taskIdString任务ID
+taskFileNumint任务处理文件数量
+taskSuccessNumint任务处理文件成功数量
+taskFailNumint任务处理文件失败数量
+taskStatusString任务状态
+assetTypeIdint使用资产类型ID
+taskCostint任务费用
+taskTimeint任务持续时间
+sourceTypeString原格式
+targetTypeString目标格式
+fileInfoDTOListArray任务文件信息
++fileKeyString文件key
++taskIdString任务ID
++fileNameString原文件名
++downFileNameString下载文件名
++fileUrlString原文件地址
++downloadUrlString处理结果文件下载地址
++sourceTypeString原格式
++targetTypeString目标格式
++fileSizeint文件大小
++convertSizeint处理结果文件大小
++convertTimeint处理消耗时间
++statusString文件处理状态
++failureCodeString文件处理失败错误码
++failureReasonString文件处理失败说明
++fileParameterString处理参数

响应示例:

json
"code": "200",
"msg": "success",
"data": {
    "taskId": "f416dbcf-0c10-4f93-ab9e-a835c1f5dba1",
    "taskFileNum": 1,
    "taskSuccessNum": 1,
    "taskFailNum": 0,
    "taskStatus": "<taskStatus>",
    "assetTypeId": 0,
    "taskCost": 1,
    "taskTime": 1,
    "sourceType": "<sourceType>",
    "targetType": "<targetType>",
    "fileInfoDTOList": [
      {
        "fileKey": "<fileKey>",
        "taskId": "<taskId>",
        "fileName": "<fileName>",
        "downFileName": "<downFileName>",
        "fileUrl": "<fileUrl>",
        "downloadUrl": "<downloadUrl>",
        "sourceType": "<sourceType>",
        "targetType": "<targetType>",
        "fileSize": 24475,
        "convertSize": 6922,
        "convertTime": 8,
        "status": "<status>",
        "failureCode": "",
        "failureReason": "",
        "fileParameter": "<fileParameter>"
      }
    ]
}

结果

文件类型说明
.txt结果文件

异步请求

如果您需要使用文件异步处理流程,请阅读异步请求说明