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.
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."
6) After your credentials are created, they are automatically downloaded:
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.
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 clipboard1<Project Sdk="Microsoft.NET.Sdk">23 <PropertyGroup>4 <OutputType>Exe</OutputType>5 <TargetFramework>netcoreapp3.1</TargetFramework>6 </PropertyGroup>78 <ItemGroup>9 <PackageReference Include="log4net" Version="2.0.12" />10 <PackageReference Include="Adobe.PDFServicesSDK" Version="3.4.1" />11 </ItemGroup>1213 <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>2425</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 clipboard1using 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 clipboard1namespace ElectronicSeal2{3 public class Program4 {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 clipboard1// 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();67// Create an ExecutionContext using credentials.8ExecutionContext executionContext = ExecutionContext.Create(credentials);
5) Now let's define our input fields:
Copied to your clipboard1//Get the input document to perform the sealing operation2FileRef sourceFile = FileRef.CreateFromLocalFile(@"sampleInvoice.pdf");34//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 clipboard1//Create AppearanceOptions and add the required signature appearance items2AppearanceOptions 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);89//Set the Seal Field Name to be created in input PDF document.10string sealFieldName = "Signature1";1112//Set the page number in input document for applying seal.13int sealPageNumber = 1;1415//Set if seal should be visible or invisible.16bool sealVisible = true;1718//Create FieldLocation instance and set the coordinates for applying signature19FieldLocation fieldLocation = new FieldLocation(150, 250, 350, 200);2021//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 clipboard1//Set the name of TSP Provider being used.2string providerName = "<PROVIDER_NAME>";34//Set the access token to be used to access TSP provider hosted APIs.5string accessToken = "<ACCESS_TOKEN>";67//Set the credential ID.8string credentialID = "<CREDENTIAL_ID>";910//Set the PIN generated while creating credentials.11string pin = "<PIN>";1213CSCAuthContext cscAuthContext = new CSCAuthContext(accessToken, "Bearer");1415//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 clipboard1//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 clipboard1//Create the PDFElectronicSealOperation instance using the PDFElectronicSealOptions instance2PDFElectronicSealOperation pdfElectronicSealOperation = PDFElectronicSealOperation.CreateNew(sealOptions);34//Set the input source file for PDFElectronicSealOperation instance5pdfElectronicSealOperation.SetInput(sourceFile);67//Set the optional input seal image for PDFElectronicSealOperation instance8pdfElectronicSealOperation.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 clipboard1//Execute the operation2FileRef result = pdfElectronicSealOperation.Execute(executionContext);34//Save the output at specified location5result.SaveAs("output/sealedOutput.pdf");
Here's the complete application (Program.cs
):
Copied to your clipboard1using 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;1314/// <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 ElectronicSeal22{23 public class Program24{25 // Initialize the logger.26 private static readonly ILog log = LogManager.GetLogger(typeof(Program));27 static void Main(string[] args)28{29 //Configure the logging30 ConfigureLogging();3132 try33{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();394041 // Create an ExecutionContext using credentials.42 ExecutionContext executionContext = ExecutionContext.Create(credentials);4344 //Set the input document to perform the sealing operation45 FileRef sourceFile = FileRef.CreateFromLocalFile(@"sampleInvoice.pdf");4647 //Set the background seal image for signature , if required.48 FileRef sealImageFile = FileRef.CreateFromLocalFile(@"sampleSealImage.png");4950 //Create AppearanceOptions and add the required signature appearance items51 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);5758 //Set the Seal Field Name to be created in input PDF document.59 string sealFieldName = "Signature1";6061 //Set the page number in input document for applying seal.62 int sealPageNumber = 1;6364 //Set if seal should be visible or invisible.65 bool sealVisible = true;6667 //Create FieldLocation instance and set the coordinates for applying signature68 FieldLocation fieldLocation = new FieldLocation(150, 250, 350, 200);6970 //Create FieldOptions instance with required details.71 FieldOptions fieldOptions = new FieldOptions.Builder(sealFieldName)72 .SetVisible(sealVisible)73 .SetFieldLocation(fieldLocation)74 .SetPageNumber(sealPageNumber)75 .Build();7677 //Set the name of TSP Provider being used.78 string providerName = "<PROVIDER_NAME>";7980 //Set the access token to be used to access TSP provider hosted APIs.81 string accessToken = "<ACCESS_TOKEN>";8283 //Set the credential ID.84 string credentialID = "<CREDENTIAL_ID>";8586 //Set the PIN generated while creating credentials.87 string pin = "<PIN>";8889 CSCAuthContext cscAuthContext = new CSCAuthContext(accessToken, "Bearer");9091 //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();9899100 //Create SealingOptions instance with all the sealing parameters.101 SealOptions sealOptions = new SealOptions.Builder(certificateCredentials, fieldOptions)102 .WithAppearanceOptions(appearanceOptions).Build();103104 //Create the PDFElectronicSealOperation instance using the SealOptions instance105 PDFElectronicSealOperation pdfElectronicSealOperation = PDFElectronicSealOperation.CreateNew(sealOptions);106107 //Set the input source file for PDFElectronicSealOperation instance108 pdfElectronicSealOperation.SetInput(sourceFile);109110 //Set the optional input seal image for PDFElectronicSealOperation instance111 pdfElectronicSealOperation.SetSealImage(sealImageFile);112113 //Execute the operation114 FileRef result = pdfElectronicSealOperation.Execute(executionContext);115116 //Save the output at specified location117 result.SaveAs("output/sealedOutput.pdf");118119}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.