Skip to content

转换指南

PDF 转 Word 工具指南

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

PDF 转 Word:

java
{    
  "isContainAnnot": "1",  
  "isContainImg": "1",
  "wordLayoutMode": "1",
  "isAllowOcr": "0",
  "isContainOcrBg": "0",
  "isOnlyAiTable": "0"
}

:::

必需参数

isContainAnnot:是否包含注释(1:是,0:否)默认0。

isContainImg:是否包含图片(1:是,0:否)默认0。

wordLayoutMode:排版方式(1.流式排版模式;2.流式排版支持表格;3.框式排版模式;)默认1。

isAllowOcr:是否允许开启OCR(1:是,0:否)默认0。

isContainOcrBg:是否开启OCR后保留背景图(1:是,0:否)默认0。

isOnlyAiTable:是否开启AI识别表格(1:是,0:否)默认0。

示例

  1. 授权

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

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

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

    curl
     curl --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="{ \"isContainAnnot\": 1 , \"isContainImg\":1,\"wordLayoutMode\":1,\"isAllowOcr\":0,\"isContainOcrBg\":0,\"isOnlyAiTable\":0}"' \
     --form 'language=""'
    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("password","")
          .addFormDataPart("parameter","{  \"isFlowLayout\": \"1\",  \"isContainImg\": \"1\"}")
          .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();
      }
    }
  4. 执行任务

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

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

结果

文件类型说明
.docx完成后的 Word 文件