Edit in GitHubLog an issue

Quickstart for Adobe PDF Electronic Seal API (.NET)

To get started using PDF Electronic Seal API, let's walk through a simple scenario - Applying an electronic seal on an invoice PDF document. In this guide, we will walk you through the complete process for creating a program that will accomplish this task.

Prerequisites

To complete this guide, you will need:

  • .NET: version 6.0 or above
  • .Net SDK
  • A build tool: Either Visual Studio or .NET Core CLI.
  • An Adobe ID. If you do not have one, the credential setup will walk you through creating one.
  • A way to edit code. No specific editor is required for this guide.

Step One: Getting credentials

1) To begin, open your browser to https://acrobatservices.adobe.com/dc-integration-creation-app-cdn/main.html?api=pdf-services-api. If you are not already logged in to Adobe.com, you will need to sign in or create a new user. Using a personal email account is recommended and not a federated ID.

Sign in

2) After registering or logging in, you will then be asked to name your new credentials. Use the name, "New Project".

3) Change the "Choose language" setting to ".Net".

4) Also note the checkbox by, "Create personalized code sample." This will include a large set of samples along with your credentials. These can be helpful for learning more later.

5) Click the checkbox saying you agree to the developer terms and then click "Create credentials."

Project setup

6) After your credentials are created, they are automatically downloaded:

alt

Step Two: Setting up the project

1) In your Downloads folder, find the ZIP file with your credentials: PDFServicesSDK-.NetSamples.zip. If you unzip that archive, you will find a folder of samples and the pdfservices-api-credentials.json file.

alt

2) Take the pdfservices-api-credentials.json file and place it in a new directory.

3) In your new directory, create a new file, ElectronicSeal.csproj. This file will declare our requirements as well as help define the application we're creating.

Copied to your clipboard
1<Project Sdk="Microsoft.NET.Sdk">
2
3 <PropertyGroup>
4 <OutputType>Exe</OutputType>
5 <TargetFramework>netcoreapp3.1</TargetFramework>
6 </PropertyGroup>
7
8 <ItemGroup>
9 <PackageReference Include="log4net" Version="2.0.12" />
10 <PackageReference Include="Adobe.PDFServicesSDK" Version="3.4.1" />
11 </ItemGroup>
12
13 <ItemGroup>
14 <None Update="log4net.config">
15 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
16 </None>
17 <None Update="sampleInvoice.pdf">
18 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
19 </None>
20 <None Update="sampleSealImage.png">
21 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
22 </None>
23 </ItemGroup>
24
25</Project>

Our application will take an Invoice PDF document, sampleInvoice.pdf (downloadable from here), and will use the sealing options with default appearance options to apply electronic seal over the PDF document by invoking Acrobat Services API and generate an electronically sealed PDF.

4) In your editor, open the directory where you previously copied the credentials and created the csproj file. Create a new file, Program.cs.

Now you're ready to begin coding.

Step Three: Creating the application

1) We'll begin by including our required dependencies:

Copied to your clipboard
1using Adobe.PDFServicesSDK;
2using Adobe.PDFServicesSDK.auth;
3using Adobe.PDFServicesSDK.exception;
4using Adobe.PDFServicesSDK.io;
5using Adobe.PDFServicesSDK.options.electronicseal;
6using Adobe.PDFServicesSDK.pdfops;
7using log4net;
8using log4net.Config;
9using log4net.Repository;
10using System;
11using System.IO;
12using System.Reflection;

2) Now let's define our main class and Main method:

Copied to your clipboard
1namespace ElectronicSeal
2{
3 public class Program
4 {
5 // Initialize the logger.
6 private static readonly ILog log = LogManager.GetLogger(typeof(Program));
7 static void Main() {
8 }
9 }
10}

3) Set the environment variables PDF_SERVICES_CLIENT_ID and PDF_SERVICES_CLIENT_SECRET by running the following commands and replacing placeholders YOUR CLIENT ID and YOUR CLIENT SECRET with the credentials present in pdfservices-api-credentials.json file:

  • Windows:

    • set PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>
    • set PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>
  • MacOS/Linux:

    • export PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>
    • export PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>

4) Let's create credentials for pdf services and use them:

Copied to your clipboard
1// Initial setup, create credentials instance.
2Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
3 .WithClientId(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_ID"))
4 .WithClientSecret(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_SECRET"))
5 .Build();
6
7// Create an ExecutionContext using credentials.
8ExecutionContext executionContext = ExecutionContext.Create(credentials);

5) Now let's define our input fields:

Copied to your clipboard
1//Get the input document to perform the sealing operation
2FileRef sourceFile = FileRef.CreateFromLocalFile(@"sampleInvoice.pdf");
3
4//Get the background seal image for signature , if required.
5FileRef sealImageFile = FileRef.CreateFromLocalFile(@"sampleSealImage.png");

6) Now, we will define seal field options:

Copied to your clipboard
1//Create AppearanceOptions and add the required signature appearance items
2AppearanceOptions appearanceOptions = new AppearanceOptions();
3appearanceOptions.AddItem(AppearanceItem.NAME);
4appearanceOptions.AddItem(AppearanceItem.LABELS);
5appearanceOptions.AddItem(AppearanceItem.DATE);
6appearanceOptions.AddItem(AppearanceItem.SEAL_IMAGE);
7appearanceOptions.AddItem(AppearanceItem.DISTINGUISHED_NAME);
8
9//Set the Seal Field Name to be created in input PDF document.
10string sealFieldName = "Signature1";
11
12//Set the page number in input document for applying seal.
13int sealPageNumber = 1;
14
15//Set if seal should be visible or invisible.
16bool sealVisible = true;
17
18//Create FieldLocation instance and set the coordinates for applying signature
19FieldLocation fieldLocation = new FieldLocation(150, 250, 350, 200);
20
21//Create FieldOptions instance with required details.
22FieldOptions fieldOptions = new FieldOptions.Builder(sealFieldName)
23 .SetVisible(sealVisible)
24 .SetFieldLocation(fieldLocation)
25 .SetPageNumber(sealPageNumber)
26 .Build();
27

7) Next, we create a CSC Certificate Credentials instance:

Copied to your clipboard
1//Set the name of TSP Provider being used.
2string providerName = "<PROVIDER_NAME>";
3
4//Set the access token to be used to access TSP provider hosted APIs.
5string accessToken = "<ACCESS_TOKEN>";
6
7//Set the credential ID.
8string credentialID = "<CREDENTIAL_ID>";
9
10//Set the PIN generated while creating credentials.
11string pin = "<PIN>";
12
13CSCAuthContext cscAuthContext = new CSCAuthContext(accessToken, "Bearer");
14
15//Create CertificateCredentials instance with required certificate details.
16CertificateCredentials certificateCredentials = CertificateCredentials.CSCCredentialBuilder()
17 .WithProviderName(providerName)
18 .WithCredentialID(credentialID)
19 .WithPin(pin)
20 .WithCSCAuthContext(cscAuthContext)
21 .Build();

8) Now, let's create the seal options with certificate credentials and field options:

Copied to your clipboard
1//Create SealingOptions instance with all the sealing parameters.
2SealOptions sealOptions = new SealOptions.Builder(certificateCredentials, fieldOptions)
3 .WithAppearanceOptions(appearanceOptions).Build();

9) Now, let's create the operation:

Copied to your clipboard
1//Create the PDFElectronicSealOperation instance using the PDFElectronicSealOptions instance
2PDFElectronicSealOperation pdfElectronicSealOperation = PDFElectronicSealOperation.CreateNew(sealOptions);
3
4//Set the input source file for PDFElectronicSealOperation instance
5pdfElectronicSealOperation.SetInput(sourceFile);
6
7//Set the optional input seal image for PDFElectronicSealOperation instance
8pdfElectronicSealOperation.SetSealImage(sealImageFile);

This code creates a seal operation using sealOptions, input source file and input seal image.

10) Let's execute this seal operation:

Copied to your clipboard
1//Execute the operation
2FileRef result = pdfElectronicSealOperation.Execute(executionContext);
3
4//Save the output at specified location
5result.SaveAs("output/sealedOutput.pdf");

Here's the complete application (Program.cs):

Copied to your clipboard
1using Adobe.PDFServicesSDK;
2using Adobe.PDFServicesSDK.auth;
3using Adobe.PDFServicesSDK.exception;
4using Adobe.PDFServicesSDK.io;
5using Adobe.PDFServicesSDK.options.electronicseal;
6using Adobe.PDFServicesSDK.pdfops;
7using log4net;
8using log4net.Config;
9using log4net.Repository;
10using System;
11using System.IO;
12using System.Reflection;
13
14/// <summary>
15/// This sample ElectronicSeal illustrates how to apply electronic seal over the PDF document using custom appearance options.
16/// <para>
17/// To know more about PDF Electronic Seal, please see the <a href="https://developer.adobe.com/document-services/docs/overview/pdf-electronic-seal-api/" target="_blank">documentation</a>.
18/// </para>
19/// Refer to README.md for instructions on how to run the samples.
20/// </summary>
21namespace ElectronicSeal
22{
23 public class Program
24{
25 // Initialize the logger.
26 private static readonly ILog log = LogManager.GetLogger(typeof(Program));
27 static void Main(string[] args)
28{
29 //Configure the logging
30 ConfigureLogging();
31
32 try
33{
34 // Initial setup, create credentials instance.
35 Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
36 .WithClientId(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_ID"))
37 .WithClientSecret(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_SECRET"))
38 .Build();
39
40
41 // Create an ExecutionContext using credentials.
42 ExecutionContext executionContext = ExecutionContext.Create(credentials);
43
44 //Set the input document to perform the sealing operation
45 FileRef sourceFile = FileRef.CreateFromLocalFile(@"sampleInvoice.pdf");
46
47 //Set the background seal image for signature , if required.
48 FileRef sealImageFile = FileRef.CreateFromLocalFile(@"sampleSealImage.png");
49
50 //Create AppearanceOptions and add the required signature appearance items
51 AppearanceOptions appearanceOptions = new AppearanceOptions();
52 appearanceOptions.AddItem(AppearanceItem.NAME);
53 appearanceOptions.AddItem(AppearanceItem.LABELS);
54 appearanceOptions.AddItem(AppearanceItem.DATE);
55 appearanceOptions.AddItem(AppearanceItem.SEAL_IMAGE);
56 appearanceOptions.AddItem(AppearanceItem.DISTINGUISHED_NAME);
57
58 //Set the Seal Field Name to be created in input PDF document.
59 string sealFieldName = "Signature1";
60
61 //Set the page number in input document for applying seal.
62 int sealPageNumber = 1;
63
64 //Set if seal should be visible or invisible.
65 bool sealVisible = true;
66
67 //Create FieldLocation instance and set the coordinates for applying signature
68 FieldLocation fieldLocation = new FieldLocation(150, 250, 350, 200);
69
70 //Create FieldOptions instance with required details.
71 FieldOptions fieldOptions = new FieldOptions.Builder(sealFieldName)
72 .SetVisible(sealVisible)
73 .SetFieldLocation(fieldLocation)
74 .SetPageNumber(sealPageNumber)
75 .Build();
76
77 //Set the name of TSP Provider being used.
78 string providerName = "<PROVIDER_NAME>";
79
80 //Set the access token to be used to access TSP provider hosted APIs.
81 string accessToken = "<ACCESS_TOKEN>";
82
83 //Set the credential ID.
84 string credentialID = "<CREDENTIAL_ID>";
85
86 //Set the PIN generated while creating credentials.
87 string pin = "<PIN>";
88
89 CSCAuthContext cscAuthContext = new CSCAuthContext(accessToken, "Bearer");
90
91 //Create CertificateCredentials instance with required certificate details.
92 CertificateCredentials certificateCredentials = CertificateCredentials.CSCCredentialBuilder()
93 .WithProviderName(providerName)
94 .WithCredentialID(credentialID)
95 .WithPin(pin)
96 .WithCSCAuthContext(cscAuthContext)
97 .Build();
98
99
100 //Create SealingOptions instance with all the sealing parameters.
101 SealOptions sealOptions = new SealOptions.Builder(certificateCredentials, fieldOptions)
102 .WithAppearanceOptions(appearanceOptions).Build();
103
104 //Create the PDFElectronicSealOperation instance using the SealOptions instance
105 PDFElectronicSealOperation pdfElectronicSealOperation = PDFElectronicSealOperation.CreateNew(sealOptions);
106
107 //Set the input source file for PDFElectronicSealOperation instance
108 pdfElectronicSealOperation.SetInput(sourceFile);
109
110 //Set the optional input seal image for PDFElectronicSealOperation instance
111 pdfElectronicSealOperation.SetSealImage(sealImageFile);
112
113 //Execute the operation
114 FileRef result = pdfElectronicSealOperation.Execute(executionContext);
115
116 //Save the output at specified location
117 result.SaveAs("output/sealedOutput.pdf");
118
119}
120catch (ServiceUsageException ex)
121 {
122 log.Error("Exception encountered while executing operation", ex);
123 }
124catch (ServiceApiException ex)
125 {
126 log.Error("Exception encountered while executing operation", ex);
127 }
128catch (SDKException ex)
129 {
130 log.Error("Exception encountered while executing operation", ex);
131 }
132catch (IOException ex)
133 {
134 log.Error("Exception encountered while executing operation", ex);
135 }
136catch (Exception ex)
137 {
138 log.Error("Exception encountered while executing operation", ex);
139 }
140}
141 static void ConfigureLogging()
142 {
143 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
144 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
145 }
146}
147}

Next Steps

Now that you've successfully performed your first operation, review the documentation for many other examples and reach out on our forums with any questions. Also remember the samples you downloaded while creating your credentials also have many demos.

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.