You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过DocuSign API从Java应用上传PDF文件?

通过Java应用向DocuSign API上传PDF文件

实操步骤与代码示例

DocuSign Quickstart里的静态内容仅用示例文本生成文档,要上传本地PDF,核心是把PDF文件转为Base64编码后添加到信封的文档列表中。以下是具体实现:

1. 添加DocuSign Java SDK依赖

如果用Maven,在pom.xml里加入:

<dependency>
    <groupId>com.docusign</groupId>
    <artifactId>docusign-esign-java</artifactId>
    <version>3.27.0</version> <!-- 可替换为最新稳定版 -->
</dependency>

2. 初始化API客户端

配置API端点和授权Token(Token可通过JWT或授权码流程获取,假设已拿到有效Token):

ApiClient apiClient = new ApiClient();
apiClient.setBasePath("https://demo.docusign.net/restapi"); // 测试环境用demo,生产环境替换为www
apiClient.addDefaultHeader("Authorization", "Bearer " + yourAccessToken);

3. 读取本地PDF并转为Base64

将本地PDF文件读入字节数组,再编码成Base64字符串:

// 读取本地PDF文件
File pdfFile = new File("/path/to/your/document.pdf");
byte[] pdfBytes = Files.readAllBytes(pdfFile.toPath());
// 转为Base64编码
String pdfBase64 = Base64.getEncoder().encodeToString(pdfBytes);

4. 构建文档与信封对象

创建Document实例传入Base64内容,再定义信封的收件人和状态:

// 构建文档对象
Document document = new Document();
document.setDocumentId("1"); // 文档唯一标识,后续可用于添加签域
document.setName("MyUploadedDocument.pdf");
document.setDocumentBase64(pdfBase64);

// 定义收件人(以单个收件人为例)
Signer signer = new Signer();
signer.setEmail("recipient@example.com");
signer.setName("Recipient Name");
signer.setRecipientId("1");
signer.setClientUserId("1001"); // 嵌入式签名需要此字段,普通签名可省略

// 组装信封定义
EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition();
envelopeDefinition.setEmailSubject("请签署这份文档");
envelopeDefinition.setDocuments(Collections.singletonList(document));
envelopeDefinition.setRecipients(new Recipients().setSigners(Collections.singletonList(signer)));
envelopeDefinition.setStatus("sent"); // 设置为sent立即发送,设为created则保存为草稿

5. 调用API创建信封

通过EnvelopesApi执行信封创建操作:

EnvelopesApi envelopesApi = new EnvelopesApi(apiClient);
EnvelopeSummary envelopeSummary = envelopesApi.createEnvelope(yourAccountId, envelopeDefinition);

// 输出信封ID,用于后续查询状态或操作
System.out.println("信封已创建,ID:" + envelopeSummary.getEnvelopeId());

关键注意点

  • 确保DocuSign账号有创建信封的权限,Token权限范围需包含signature。
  • 测试环境与生产环境的API端点不同,需对应切换。
  • 若需给PDF添加签域,可在创建信封时给Signer添加Tabs对象,指定签域的位置和类型。

内容的提问来源于stack exchange,提问作者LMi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 22:45:30