Sample code for 30+ languages & platforms
Delphi ActiveX

DocuSign Download Envelope Document (PDF)

See more DocuSign Examples

Retrieves the specified document from the envelope. The response body of this method is the PDF file as a byte stream. You can get the file name and document ID from the response's Content-Disposition header.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
jsonToken: TChilkatJsonObject;
url: WideString;
bd: TChilkatBinData;
respStatusCode: Integer;
mime: TChilkatMime;
filename: WideString;
sbPath: TChilkatStringBuilder;

begin
success := 0;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

http := TChilkatHttp.Create(Self);

// Implements the following HTTP request:
// GET /restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents/1

// Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
jsonToken := TChilkatJsonObject.Create(Self);
// Load a previously obtained OAuth2 access token.
success := jsonToken.LoadFile('qa_data/tokens/docusign.json');
if (success = 0) then
  begin
    Memo1.Lines.Add(jsonToken.LastErrorText);
    Exit;
  end;

http.AuthToken := jsonToken.StringOf('access_token');

// Use your account ID and a valid envelopeId here:
http.SetUrlVar('accountId','7f3f65ed-5e87-418d-94c1-92499ddc8252');
http.SetUrlVar('envelopeId','90d7e40a-b4bd-4ccd-bf38-c80e37954a13');

url := 'https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1';
bd := TChilkatBinData.Create(Self);

success := http.DownloadBd(url,bd.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

respStatusCode := http.LastStatus;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode <> 200) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(http.LastResponseHeader);
    // The response body contains an error message.
    Memo1.Lines.Add(bd.GetString('utf-8'));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

// The response indicated success.
// Get the filename from the Content-Disposition header and save to a file.
mime := TChilkatMime.Create(Self);
mime.LoadMime(http.LastResponseHeader);

filename := mime.GetHeaderFieldAttribute('Content-Disposition','filename');
Memo1.Lines.Add('filename = ' + filename);

sbPath := TChilkatStringBuilder.Create(Self);
sbPath.Append('C:/aaworkarea/');
sbPath.Append(filename);
success := bd.WriteFile(sbPath.GetAsString());
if (success = 0) then
  begin
    Memo1.Lines.Add('Failed to save to output file.');
  end
else
  begin
    Memo1.Lines.Add('Wrote ' + sbPath.GetAsString());
  end;
end;