How Tos

The samples and documentation here should get you quickly up and running with the DC Services SDK. These code examples illustrate how to perform PDF actions using the SDK, including:

  • Creating a PDF from multiple formats, including HTML, Microsoft Office documents, and text files

  • Exporting a PDF to other formats or an image

  • Combining entire PDFs or specified page ranges

  • Using OCR to make a PDF file searchable and editable with a custom locale

Runtime in-memory authentication

The SDK supports providing the authentication credentials at runtime. Doing so allows fetching the credentials from a secret server during runtime instead of storing them in a file. Please refer the following samples for details.

Custom timeout configuration

The APIs use inferred timeout properties and provide defaults. However, the SDK supports custom timeouts for the API calls. You can tailor the timeout settings for your environment and network speed. In addition to the details below, you can refer to working code samples:

Java timeout configuration

Available properties:

  • connectTimeout: Default: 2000. The maximum allowed time in milliseconds for creating an initial HTTPS connection.

  • socketTimeout: Default: 10000. The maximum allowed time in milliseconds between two successive HTTP response packets.

Override the timeout properties via a custom ClientConfig class:

ClientConfig clientConfig = ClientConfig.builder()
   .withConnectTimeout(10000)
   .withSocketTimeout(40000)
   .build();

.NET timeout configuration

Available properties:

  • timeout: Default: 2000. The maximum allowed time in milliseconds for establishing a connection, sending a request, and getting a response.

  • readWriteTimeout: Default: 10000. The maximum allowed time in milliseconds to read or write data after connection is established.

Override the timeout properties via a custom ClientConfig class:

ClientConfig clientConfig = ClientConfig.ConfigBuilder()
  .WithTimeout(40000)
  .WithReadWriteTimeout(10000)
  .Build();

Node.js timeout configuration

Available properties:

  • connectTimeout: Default: 2000. The maximum allowed time in milliseconds for creating an initial HTTPS connection.

  • readTimeout: Default: 10000. The maximum allowed time in milliseconds between two successive HTTP response packets.

  • uploadReadTimeout: Default: 60000. The read timeout in milliseconds for file upload requests.

Override the timeout properties via a custom ClientConfig class:

const clientConfig = DCServicesSdk.ClientConfig
  .clientConfigBuilder()
  .withConnectTimeout("100000")
  .withReadTimeout("40000")
  .build();

Create a PDF

Use the sample below to create PDFs from Microsoft Office documents (Word, Excel and PowerPoint) and other supported file formats. While the example shows .docx file conversion, the SDK supports the following formats:

  • Microsoft Word (DOC, DOCX)

  • Microsoft PowerPoint (PPT, PPTX)

  • Microsoft Excel (XLS, XLSX)

  • Text (TXT, RTF)

  • Image (BMP, JPEG, GIF, TIFF, PNG)

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.createpdf.CreatePDFFromDOCX
Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
  package com.adobe.platform.operation.samples.createpdf;

 import java.io.IOException;

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

 import com.adobe.platform.operation.ExecutionContext;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.CreatePDFOperation;

 public class CreatePDFFromDOCX {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDOCX.class);

   public static void main(String[] args) {

     try {

       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       CreatePDFOperation createPdfOperation = CreatePDFOperation.createNew();

       // Set operation input from a source file.
       FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFInput.docx");
       createPdfOperation.setInput(source);

       // Execute the operation.
       FileRef result = createPdfOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/createPDFFromDOCX.pdf");

     } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
       LOGGER.error("Exception encountered while executing operation", ex);
     }
   }
 }
     
Run the sample:
cd CreatePDFFromDocx/
dotnet run CreatePDFFromDocx.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using System;
 using System.IO;
 using log4net.Repository;
 using log4net.Config;
 using log4net;
 using System.Reflection;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.exception;

 namespace CreatePDFFromDocx
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         CreatePDFOperation createPdfOperation = CreatePDFOperation.CreateNew();

         // Set operation input from a source file.
         FileRef source = FileRef.CreateFromLocalFile(@"createPdfInput.docx");
         createPdfOperation.SetInput(source);

         // Execute the operation.
         FileRef result = createPdfOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPdfOutput.pdf");
       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // Catch more errors here. . .
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
    
Run the sample:
node src/createpdf/create-pdf-from-docx.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   // Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     createPdfOperation = DCServicesSdk.CreatePDF.Operation.createNew();

   // Set operation input from a source file.
   const input = DCServicesSdk.FileRef.createFromLocalFile('resources/createPDFInput.docx');
   createPdfOperation.setInput(input);

   // Execute the operation and Save the result to the specified location.
   createPdfOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/createPDFFromDOCX.pdf'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Create a PDF from static HTML

The sample below creates a PDF file from a static HTML file. The file must be local. Since HTML/web pages typically contain external assets, the input file must be a zip file containing an index.html at the top level of the archive as well as any dependencies such as images, css files, and so on.

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.createpdf.CreatePDFFromStaticHTML
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.createpdf;

 import java.io.IOException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.adobe.platform.operation.ExecutionContext;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.CreatePDFOperation;
 import com.adobe.platform.operation.pdfops.options.createpdf.CreatePDFOptions;
 import com.adobe.platform.operation.pdfops.options.createpdf.PageLayout;

 public class CreatePDFFromStaticHTML {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromStaticHTML.class);

   public static void main(String[] args) {

     try {

       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.createNew();

       // Set operation input from a source file.
       FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFFromStaticHtmlInput.zip");
       htmlToPDFOperation.setInput(source);

       // Provide any custom configuration options for the operation.
       setCustomOptions(htmlToPDFOperation);

       // Execute the operation.
       FileRef result = htmlToPDFOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/createPDFFromStaticHtmlOutput.pdf");

     } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
       LOGGER.error("Exception encountered while executing operation", ex);
     }
   }

   private static void setCustomOptions(CreatePDFOperation htmlToPDFOperation) {
     // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
     PageLayout pageLayout = new PageLayout();
     pageLayout.setPageSize(8, 11.5);

     // Set the desired HTML-to-PDF conversion options.
     CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.htmlOptionsBuilder()
         .includeHeaderFooter(true)
         .withPageLayout(pageLayout)
         .build();
     htmlToPDFOperation.setOptions(htmlToPdfOptions);
   }
 }
     
Run the sample:
cd CreatePDFFromHtml/
dotnet run CreatePDFFromStaticHtml.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using System;
 using System.IO;
 using log4net.Repository;
 using log4net;
 using log4net.Config;
 using System.Reflection;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.exception;
 using Adobe.DocumentCloud.Services.options.createpdf;

 namespace CreatePDFFromStaticHtml
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.CreateNew();

         // Set operation input from a source file.
         FileRef source = FileRef.CreateFromLocalFile(@"createPDFFromStaticHtmlInput.zip");
         htmlToPDFOperation.SetInput(source);

         // Provide any custom configuration options for the operation.
         SetCustomOptions(htmlToPDFOperation);

         // Execute the operation.
         FileRef result = htmlToPDFOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPdfFromStaticHtmlOutput.pdf");
       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
        // Catch more errors here. . .
     }

     private static void SetCustomOptions(CreatePDFOperation htmlToPDFOperation)
     {
       // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
       PageLayout pageLayout = new PageLayout();
       pageLayout.SetPageSize(8, 11.5);

       // Set the desired HTML-to-PDF conversion options.
       CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.HtmlOptionsBuilder()
           .IncludeHeaderFooter(true)
           .WithPageLayout(pageLayout)
           . Build();
       htmlToPDFOperation.SetOptions(htmlToPdfOptions);
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
  
Run the sample:
node src/createpdf/create-pdf-from-static-html.js
const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 const setCustomOptions = (htmlToPDFOperation) => {
   // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
   const pageLayout = new DCServicesSdk.CreatePDF.options.PageLayout();
   pageLayout.setPageSize(8, 11.5);

   // Set the desired HTML-to-PDF conversion options.
   const htmlToPdfOptions = new DCServicesSdk.CreatePDF.options.html.CreatePDFFromHtmlOptions.Builder()
     .includesHeaderFooter(true)
     .withPageLayout(pageLayout)
     .build();
   htmlToPDFOperation.setOptions(htmlToPdfOptions);
 };


 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   // Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     htmlToPDFOperation = DCServicesSdk.CreatePDF.Operation.createNew();

   // Set operation input from a source file.
   const input = DCServicesSdk.FileRef.createFromLocalFile('resources/createPDFFromStaticHtmlInput.zip');
   htmlToPDFOperation.setInput(input);

   // Provide any custom configuration options for the operation.
   setCustomOptions(htmlToPDFOperation);

   // Execute the operation and Save the result to the specified location.
   htmlToPDFOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/createPdfFromStaticHtmlOutput.pdf'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Create a PDF from dynamic HTML

To support workflows with dynamic data, CreatePDFFromDynamicHTML creates PDFs from dynamic HTML. It’s a common scenario for enterprise to provide end users with an HTML template with form fields. This API allows you to capture the users unique data entries and then save it as PDF. Collected data is stored in a JSON file, and the source HTML file must include <script src='./json.js' type='text/javascript'></script>. Refer to the API docs for usage.

The sample CreatePDFFromDynamicHTML converts a zip file, containing the input HTML file and its resources, along with the input data to a PDF file. The input data is used by the JavaScript in the HTML file to manipulate the HTML DOM, thus effectively updating the source HTML file. This mechanism can be used to provide data to the template HTML dynamically prior to PDF conversion.

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.createpdf.CreatePDFFromDynamicHTML
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.createpdf;

 import java.io.IOException;

 import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

 import com.adobe.platform.operation.ExecutionContext;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.CreatePDFOperation;
 import com.adobe.platform.operation.pdfops.options.createpdf.CreatePDFOptions;
 import com.adobe.platform.operation.pdfops.options.createpdf.PageLayout;

 public class CreatePDFFromDynamicHTML {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(CreatePDFFromDynamicHTML.class);

   public static void main(String[] args) {

     try {

       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.createNew();

       // Set operation input from a source file.
       FileRef source = FileRef.createFromLocalFile("src/main/resources/createPDFFromDynamicHtmlInput.zip");
       htmlToPDFOperation.setInput(source);

       // Provide any custom configuration options for the operation.
       setCustomOptions(htmlToPDFOperation);

       // Execute the operation.
       FileRef result = htmlToPDFOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/createPDFFromDynamicHtmlOutput.pdf");

     } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
       LOGGER.error("Exception encountered while executing operation", ex);
     }
   }

   private static void setCustomOptions(CreatePDFOperation htmlToPDFOperation) {
     // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
     PageLayout pageLayout = new PageLayout();
     pageLayout.setPageSize(8, 11.5);

     //Set the dataToMerge field that needs to be populated in the HTML before its conversion
     JSONObject dataToMerge = new JSONObject();
     dataToMerge.put("title","Create, Convert PDFs and More!");
     dataToMerge.put("sub_title","Easily integrate PDF actions within your document workflows.");

     // Set the desired HTML-to-PDF conversion options.
     CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.htmlOptionsBuilder()
         .includeHeaderFooter(true)
         .withPageLayout(pageLayout)
         .withDataToMerge(dataToMerge)
         .build();
     htmlToPDFOperation.setOptions(htmlToPdfOptions);
   }
 }
Run the sample:
cd CreatePDFFromDynamicHtml/
dotnet run CreatePDFFromDynamicHtml.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using System;
 using System.IO;
 using log4net.Repository;
 using log4net;
 using log4net.Config;
 using System.Reflection;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.exception;
 using Adobe.DocumentCloud.Services.options.createpdf;
 using Newtonsoft.Json.Linq;

 namespace CreatePDFFromDynamicHtml
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.CreateNew();

         // Set operation input from a source file.
         FileRef source = FileRef.CreateFromLocalFile(@"createPDFFromDynamicHtmlInput.zip");
         htmlToPDFOperation.SetInput(source);

         // Provide any custom configuration options for the operation.
         SetCustomOptions(htmlToPDFOperation);

         // Execute the operation.
         FileRef result = htmlToPDFOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPdfFromDynamicHtmlOutput.pdf");
       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // errors continued. . .
     }

     private static void SetCustomOptions(CreatePDFOperation htmlToPDFOperation)
     {
       // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
       PageLayout pageLayout = new PageLayout();
       pageLayout.SetPageSize(8, 11.5);

       //Set the dataToMerge field that needs to be populated in the HTML before its conversion
       JObject dataToMerge = new JObject
       {
         { "title", "Create, Convert PDFs and More!" },
         { "sub_title", "Easily integrate PDF actions within your document workflows." }
       };

       // Set the desired HTML-to-PDF conversion options.
       CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.HtmlOptionsBuilder()
           .IncludeHeaderFooter(true)
           .WithPageLayout(pageLayout)
           .WithDataToMerge(dataToMerge)
           .Build();
       htmlToPDFOperation.SetOptions(htmlToPdfOptions);
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }

    
Run the sample:
node src/createpdf/create-pdf-from-dynamic-html.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 const setCustomOptions = (htmlToPDFOperation) => {
   // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation).
   const pageLayout = new DCServicesSdk.CreatePDF.options.PageLayout();
   pageLayout.setPageSize(8, 11.5);
   //Set the dataToMerge field that needs to be populated in the generated PDF.
   const dataToMerge = {
     "title":"Create, Convert PDFs and More!",
     "sub_title": "Easily integrate PDF actions within your document workflows."
   };
   // Set the desired HTML-to-PDF conversion options.
   const htmlToPdfOptions = new DCServicesSdk.CreatePDF.options.html.CreatePDFFromHtmlOptions.Builder()
     .includesHeaderFooter(true)
     .withPageLayout(pageLayout)
     .withDataToMerge(dataToMerge)
     .build();
   htmlToPDFOperation.setOptions(htmlToPdfOptions);
 };


 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   // Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     htmlToPDFOperation = DCServicesSdk.CreatePDF.Operation.createNew();

   // Set operation input from a source file.
   const input = DCServicesSdk.FileRef.createFromLocalFile('resources/createPDFFromDynamicHtmlInput.zip');
   htmlToPDFOperation.setInput(input);

   // Provide any custom configuration options for the operation.
   setCustomOptions(htmlToPDFOperation);

   // Execute the operation and Save the result to the specified location.
   htmlToPDFOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/createPdfFromDynamicHtmlOutput.pdf'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Export a PDF

The sample below converts a PDF file into a number of supported formats such as:

  • Microsoft Office file formats

  • Text files

  • Images

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.exportpdf.ExportPDFToDOCX
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.exportpdf;

 import java.io.IOException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.adobe.platform.operation.ExecutionContext;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.ExportPDFOperation;
 import com.adobe.platform.operation.pdfops.options.exportpdf.ExportPDFTargetFormat;

 public class ExportPDFToDOCX {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(ExportPDFToDOCX.class);

   public static void main(String[] args) {

     try {
       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();
       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       ExportPDFOperation exportPdfOperation = ExportPDFOperation.createNew(ExportPDFTargetFormat.DOCX);

       // Set operation input from a local PDF file
       FileRef sourceFileRef = FileRef.createFromLocalFile("src/main/resources/exportPDFInput.pdf");
       exportPdfOperation.setInput(sourceFileRef);

       // Execute the operation.
       FileRef result = exportPdfOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/exportPdfOutput.docx");

     } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
       LOGGER.error("Exception encountered while executing operation", ex);
     }
   }
 }
  
Run the sample:
cd ExportPDFToDocx/
dotnet run ExportPDFToDocx.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.exception;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.options.exportpdf;
 using Adobe.DocumentCloud.Services.pdfops;
 using log4net;
 using log4net.Config;
 using log4net.Repository;
 using System;
 using System.IO;
 using System.Reflection;

 namespace ExportPDFToDocx
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         ExportPDFOperation exportPdfOperation = ExportPDFOperation.CreateNew(ExportPDFTargetFormat.DOCX);

         // Set operation input from a local PDF file
         FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"exportPdfInput.pdf");
         exportPdfOperation.SetInput(sourceFileRef);

         // Execute the operation.
         FileRef result = exportPdfOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/exportPdfOutput.docx");
       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // Catch more errors here. . .
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
  
Run the sample:
node src/exportpdf/export-pdf-to-docx.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   //Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     exportPDF = DCServicesSdk.ExportPDF,
     exportPdfOperation = exportPDF.Operation.createNew(exportPDF.SupportedTargetFormats.DOCX);

   // Set operation input from a source file
   const input = DCServicesSdk.FileRef.createFromLocalFile('resources/exportPDFInput.pdf');
   exportPdfOperation.setInput(input);

   // Execute the operation and Save the result to the specified location.
   exportPdfOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/exportPdfOutput.docx'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Export a PDF to images

The sample below converts a PDF file to one or more jpeg or png images. Exporting to an image produces a zip archive containing one image per page. Each image file name ends with “_<unpadded_page_index_number>”. For example, a PDF file with 15 pages will generate 15 image files. The first file’s name ends with “_1” and the last file’s name ends with “_15”.

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.exportpdf.ExportPDFToJPEG
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.exportpdf;

 import java.io.IOException;
 import com.adobe.platform.operation.ExecutionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.ExportPDFOperation;
 import com.adobe.platform.operation.pdfops.options.exportpdf.ExportPDFTargetFormat;

 public class ExportPDFToJPEG {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(ExportPDFToJPEG.class);

   public static void main(String[] args) {
     try {

       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       ExportPDFOperation exportPdfOperation = ExportPDFOperation.createNew(ExportPDFTargetFormat.JPEG);

       // Set operation input from a source file.
       FileRef sourceFileRef = FileRef.createFromLocalFile("src/main/resources/exportPDFToImageInput.pdf");
       exportPdfOperation.setInput(sourceFileRef);

       // Execute the operation.
       FileRef result = exportPdfOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/exportPDFToJPEG.zip");

     } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
       LOGGER.error("Exception encountered while executing operation", ex);
     }
   }
 }
     
Run the sample:
cd ExportPDFToImage/
dotnet run ExportPDFToImage.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
using System.IO;
 using System;
 using log4net.Repository;
 using log4net.Config;
 using log4net;
 using System.Reflection;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.options.exportpdf;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.exception;

 namespace ExportPDFToImage
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         ExportPDFOperation exportPdfOperation = ExportPDFOperation.CreateNew(ExportPDFTargetFormat.JPEG);

         // Set operation input from a source file.
         FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"exportPdfToImageInput.pdf");
         exportPdfOperation.SetInput(sourceFileRef);

         // Execute the operation.
         FileRef result = exportPdfOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/exportPdfToImageOutput.zip");
       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // Catch more errors here. . .
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
    
Run the sample:
node src/exportpdf/export-pdf-to-jpeg.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   //Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     exportPDF = DCServicesSdk.ExportPDF,
     exportPdfOperation = exportPDF.Operation.createNew(exportPDF.SupportedTargetFormats.JPEG);

   // Set operation input from a source file
   const input = DCServicesSdk.FileRef.createFromLocalFile('resources/exportPDFToImageInput.pdf');
   exportPdfOperation.setInput(input);

   // Execute the operation and Save the result to the specified location.
   exportPdfOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/exportPDFToJPEG.zip'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Combine files

This sample combines up to 12 PDF files into a single PDF file.

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.combine.CombinePDF
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.combine;

 import java.io.IOException;

 import com.adobe.platform.operation.ExecutionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.CombineFilesOperation;
 import com.adobe.platform.operation.samples.exportpdf.ExportPDFToJPEG;

 public class CombinePDF {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(ExportPDFToJPEG.class);

   public static void main(String[] args) {
     try {
       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();

       // Add operation input from source files.
       FileRef combineSource1 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput1.pdf");
       FileRef combineSource2 = FileRef.createFromLocalFile("src/main/resources/combineFilesInput2.pdf");
       combineFilesOperation.addInput(combineSource1);
       combineFilesOperation.addInput(combineSource2);

       // Execute the operation.
       FileRef result = combineFilesOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/combineFilesOutput.pdf");

     } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
       LOGGER.error("Exception encountered while executing operation", e);
     }
   }
 }
     
Run the sample:
cd CombinePDF/
dotnet run CombinePDF.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using System;
 using System.IO;
 using log4net;
 using log4net.Config;
 using System.Reflection;
 using log4net.Repository;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.exception;

 namespace CombinePDF
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         CombineFilesOperation combineFilesOperation = CombineFilesOperation.CreateNew();

         // Add operation input from source files.
         FileRef combineSource1 = FileRef.CreateFromLocalFile(@"combineFilesInput1.pdf");
         FileRef combineSource2 = FileRef.CreateFromLocalFile(@"combineFilesInput2.pdf");
         combineFilesOperation.AddInput(combineSource1);
         combineFilesOperation.AddInput(combineSource2);

         // Execute the operation.
         FileRef result = combineFilesOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.pdf");

       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // Catch more errors here. . .
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
    
Run the sample:
node src/combine/combine-pdf.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 try {
   // Initial setup, create credentials instance.
   const credentials = DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   // Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     combineFilesOperation = DCServicesSdk.CombineFiles.Operation.createNew();

   // Set operation input from a source file.
   const combineSource1 = DCServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput1.pdf'),
     combineSource2 = DCServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput2.pdf');
   combineFilesOperation.addInput(combineSource1);
   combineFilesOperation.addInput(combineSource2);

   // Execute the operation and Save the result to the specified location.
   combineFilesOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/combineFilesOutput.pdf'))
     .catch(err => {
       if (err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Combine pages from multiple files

This combine sample combines specific pages from up to 12 different PDF files into a single PDF file. Optional arguments allow specifying page ranges for each file to combine in the output file.

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.combine.CombinePDFWithPageRanges
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.combine;

 import java.io.IOException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.adobe.platform.operation.ExecutionContext;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.CombineFilesOperation;
 import com.adobe.platform.operation.pdfops.options.PageRanges;

 public class CombinePDFWithPageRanges {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(CombinePDFWithPageRanges.class);

   public static void main(String[] args) {

     try {

       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
           .fromFile("dc-services-sdk-credentials.json")
           .build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.create(credentials);
       CombineFilesOperation combineFilesOperation = CombineFilesOperation.createNew();

       // Create a FileRef instance from a local file.
       FileRef firstFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput1.pdf");
       PageRanges pageRangesForFirstFile = getPageRangeForFirstFile();
       // Add the first file as input to the operation, along with its page range.
       combineFilesOperation.addInput(firstFileToCombine, pageRangesForFirstFile);

       // Create a second FileRef instance using a local file.
       FileRef secondFileToCombine = FileRef.createFromLocalFile("src/main/resources/combineFileWithPageRangeInput2.pdf");
       PageRanges pageRangesForSecondFile = getPageRangeForSecondFile();
       // Add the second file as input to the operation, along with its page range.
       combineFilesOperation.addInput(secondFileToCombine, pageRangesForSecondFile);

       // Execute the operation.
       FileRef result = combineFilesOperation.execute(executionContext);

       // Save the result to the specified location.
       result.saveAs("output/combineFilesWithPageOptionsOutput.pdf");

     } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
       LOGGER.error("Exception encountered while executing operation", ex);
     }
   }

   private static PageRanges getPageRangeForSecondFile() {
     // Specify which pages of the second file are to be included in the combined file.
     PageRanges pageRangesForSecondFile = new PageRanges();
     // Add all pages including and after page 3.
     pageRangesForSecondFile.addAllFrom(3);
     return pageRangesForSecondFile;
   }

   private static PageRanges getPageRangeForFirstFile() {
     // Specify which pages of the first file are to be included in the combined file.
     PageRanges pageRangesForFirstFile = new PageRanges();
     // Add page 1.
     pageRangesForFirstFile.addSinglePage(1);
     // Add page 2.
     pageRangesForFirstFile.addSinglePage(2);
     // Add pages 3 to 4.
     pageRangesForFirstFile.addRange(3, 4);
     return pageRangesForFirstFile;
   }
 }
     
Run the sample:
cd CombinePDFWithPageRanges/
dotnet run CombinePDFWithPageRanges.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using System;
 using System.IO;
 using log4net.Repository;
 using log4net;
 using log4net.Config;
 using System.Reflection;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.options;
 using Adobe.DocumentCloud.Services.exception;

 namespace CombinePDFWithPageRanges
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {

         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         CombineFilesOperation combineFilesOperation = CombineFilesOperation.CreateNew();

         // Create a FileRef instance from a local file.
         FileRef firstFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput1.pdf");
         PageRanges pageRangesForFirstFile = GetPageRangeForFirstFile();
         // Add the first file as input to the operation, along with its page range.
         combineFilesOperation.AddInput(firstFileToCombine, pageRangesForFirstFile);

         // Create a second FileRef instance using a local file.
         FileRef secondFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput2.pdf");
         PageRanges pageRangesForSecondFile = GetPageRangeForSecondFile();
         // Add the second file as input to the operation, along with its page range.
         combineFilesOperation.AddInput(secondFileToCombine, pageRangesForSecondFile);

         // Execute the operation.
         FileRef result = combineFilesOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.pdf");

       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // Catch more errors here. . .
     }

     private static PageRanges GetPageRangeForSecondFile()
     {
       // Specify which pages of the second file are to be included in the combined file.
       PageRanges pageRangesForSecondFile = new PageRanges();
       // Add all pages including and after page 5.
       pageRangesForSecondFile.AddAllFrom(5);
       return pageRangesForSecondFile;
     }

     private static PageRanges GetPageRangeForFirstFile()
     {
       // Specify which pages of the first file are to be included in the combined file.
       PageRanges pageRangesForFirstFile = new PageRanges();
       // Add page 2.
       pageRangesForFirstFile.AddSinglePage(2);
       // Add page 3.
       pageRangesForFirstFile.AddSinglePage(3);
       // Add pages 5 to 7.
       pageRangesForFirstFile.AddRange(5, 7);
       return pageRangesForFirstFile;
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
    
Run the sample:
node src/combine/combine-pdf-with-page-ranges.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 const getPageRangesForFirstFile = () => {
   // Specify which pages of the first file are to be included in the combined file.
   const pageRangesForFirstFile = new DCServicesSdk.PageRanges();
   // Add page 1.
   pageRangesForFirstFile.addSinglePage(1);
   // Add page 2.
   pageRangesForFirstFile.addSinglePage(2);
   // Add pages 3 to 4.
   pageRangesForFirstFile.addPageRange(3, 4);
   return pageRangesForFirstFile;
 };

 const getPageRangesForSecondFile = () => {
   // Specify which pages of the second file are to be included in the combined file.
   const pageRangesForSecondFile = new DCServicesSdk.PageRanges();
   // Add all pages including and after page 3.
   pageRangesForSecondFile.addAllFrom(3);
   return pageRangesForSecondFile;
 };

 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   // Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     combineFilesOperation = DCServicesSdk.CombineFiles.Operation.createNew();

   // Create a FileRef instance from a local file.
   const combineSource1 = DCServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput1.pdf'),
     pageRangesForFirstFile = getPageRangesForFirstFile();
   // Add the first file as input to the operation, along with its page range.
   combineFilesOperation.addInput(combineSource1, pageRangesForFirstFile);

   // Create a second FileRef instance using a local file.
   const combineSource2 = DCServicesSdk.FileRef.createFromLocalFile('resources/combineFilesInput2.pdf'),
     pageRangesForSecondFile = getPageRangesForSecondFile();
   // Add the second file as input to the operation, along with its page range.
   combineFilesOperation.addInput(combineSource2, pageRangesForSecondFile);

   // Execute the operation and Save the result to the specified location.
   combineFilesOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/combineFilesWithPageRangesOutput.pdf'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

Text recognition (OCR)

Optical character recognition (OCR) converts images to text so that you and your users can fully interact with the PDF file. After performing OCR, the PDF may be fully editable and searchable. The input format must be application/pdf.

This sample defaults to the en-us locale. For other languages, see OCR with explicit language.

Run the sample:
mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.platform.operation.samples.ocr.OcrPDF
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
package com.adobe.platform.operation.samples.ocr;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.platform.operation.ExecutionContext;
import com.adobe.platform.operation.auth.Credentials;
import com.adobe.platform.operation.exception.SdkException;
import com.adobe.platform.operation.exception.ServiceApiException;
import com.adobe.platform.operation.exception.ServiceUsageException;
import com.adobe.platform.operation.io.FileRef;
import com.adobe.platform.operation.pdfops.OCROperation;

public class OcrPDF {

 // Initialize the logger.
 private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDF.class);

 public static void main(String[] args) {

  try {

   // Initial setup, create credentials instance.
   Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   //Create an ExecutionContext using credentials and create a new operation instance.
   ExecutionContext executionContext = ExecutionContext.create(credentials);
   OCROperation ocrOperation = OCROperation.createNew();

   // Set operation input from a source file.
   FileRef source = FileRef.createFromLocalFile("src/main/resources/ocrInput.pdf");
   ocrOperation.setInput(source);

   // Execute the operation
   FileRef result = ocrOperation.execute(executionContext);

   // Save the result at the specified location
   result.saveAs("output/ocrOutput.pdf");

  } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
   LOGGER.error("Exception encountered while executing operation", ex);
  }
 }
}

     
Run the sample:
cd OcrPDF/
dotnet run OcrPDF.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
 using System.IO;
 using System;
 using log4net.Repository;
 using log4net.Config;
 using log4net;
 using System.Reflection;
 using Adobe.DocumentCloud.Services;
 using Adobe.DocumentCloud.Services.auth;
 using Adobe.DocumentCloud.Services.pdfops;
 using Adobe.DocumentCloud.Services.io;
 using Adobe.DocumentCloud.Services.exception;

 namespace OcrPDF
 {
   class Program
   {
     private static readonly ILog log = LogManager.GetLogger(typeof(Program));
     static void Main()
     {
       //Configure the logging
       ConfigureLogging();
       try
       {
         // Initial setup, create credentials instance.
         Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
                 .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
                 .Build();

         //Create an ExecutionContext using credentials and create a new operation instance.
         ExecutionContext executionContext = ExecutionContext.Create(credentials);
         OCROperation ocrOperation = OCROperation.CreateNew();

         // Set operation input from a source file.
         FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"ocrInput.pdf");
         ocrOperation.SetInput(sourceFileRef);

         // Execute the operation.
         FileRef result = ocrOperation.Execute(executionContext);

         // Save the result to the specified location.
         result.SaveAs(Directory.GetCurrentDirectory() + "/output/ocrOperationOutput.pdf");
       }
       catch (ServiceUsageException ex)
       {
         log.Error("Exception encountered while executing operation", ex);
       }
       // Catch more errors here. . .
     }

     static void ConfigureLogging()
     {
       ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
       XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
     }
   }
 }
    
Run the sample:
node src/ocr/ocr-pdf.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
     .serviceAccountCredentialsBuilder()
     .fromFile("dc-services-sdk-credentials.json")
     .build();

   // Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
     ocrOperation = DCServicesSdk.OCR.Operation.createNew();

   // Set operation input from a source file.
   const input = DCServicesSdk.FileRef.createFromLocalFile(
       'resources/ocrInput.pdf',
       DCServicesSdk.OCR.SupportedMediaTypes.pdf
     );
   ocrOperation.setInput(input);

   // Execute the operation and Save the result to the specified location.
   ocrOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/ocrOutput.pdf'))
     .catch(err => {
       if(err instanceof DCServicesSdk.Error.ServiceApiError
         || err instanceof DCServicesSdk.Error.ServiceUsageError) {
         console.log('Exception encountered while executing operation', err);
       } else {
         console.log('Exception encountered while executing operation', err);
       }
     });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }

OCR with explicit language

You can perform OCR on files in other languages, including German, French, Danish, and other languages. Refer to OCRSupportedLocale and OCRSupportedType in the API docs for a list of supported OCR locales and OCR types.

As shown in the OcrPDFWithOptions sample, when you make a PDF file searchable, you specify both the locale (language) and the type. There are two types which produce a different result:

  • One type ensures that text is searchable and selectable, but modifies the original image during the cleanup process (for example, deskews it) before placing an invisible text layer over it. This type removes unwanted artifacts and may result in a more readable document in some scenarios.

  • The second (EXACT) type, also overlays a searchable text layer over the original image, but in this case, the original image is unchanged. This type produces maximum fidelity to the original image.

Run the sample:
mvn -f pom.xml exec:java Dexec.mainClass=com.adobe.platform.operation.samples.ocr.OcrPDFWithOptions
// Get the samples from https://www.adobe.com/go/dcservicessdk_java_samples
 package com.adobe.platform.operation.samples.ocr;

 import java.io.IOException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import com.adobe.platform.operation.ExecutionContext;
 import com.adobe.platform.operation.auth.Credentials;
 import com.adobe.platform.operation.exception.SdkException;
 import com.adobe.platform.operation.exception.ServiceApiException;
 import com.adobe.platform.operation.exception.ServiceUsageException;
 import com.adobe.platform.operation.io.FileRef;
 import com.adobe.platform.operation.pdfops.OCROperation;
 import com.adobe.platform.operation.pdfops.options.ocr.OCROptions;
 import com.adobe.platform.operation.pdfops.options.ocr.OCRSupportedLocale;
 import com.adobe.platform.operation.pdfops.options.ocr.OCRSupportedType;

 public class OcrPDFWithOptions {

   // Initialize the logger.
   private static final Logger LOGGER = LoggerFactory.getLogger(OcrPDFWithOptions.class);

   public static void main(String[] args) {

       try {

           // Initial setup, create credentials instance.
           Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
                   .fromFile("dc-services-sdk-credentials.json")
                   .build();

           //Create an ExecutionContext using credentials and create a new operation instance.
           ExecutionContext executionContext = ExecutionContext.create(credentials);
           OCROperation ocrOperation = OCROperation.createNew();

           // Set operation input from a source file.
           FileRef source = FileRef.createFromLocalFile("src/main/resources/ocrInput.pdf");
           ocrOperation.setInput(source);

           // Build OCR options from supported locales and OCR-types and set them into the operation
           OCROptions ocrOptions = OCROptions.ocrOptionsBuilder()
                   .withOCRLocale(OCRSupportedLocale.EN_US)
                   .withOCRType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
                   .build();
           ocrOperation.setOptions(ocrOptions);

           // Execute the operation
           FileRef result = ocrOperation.execute(executionContext);

           // Save the result at the specified location
           result.saveAs("output/ocrWithOptionsOutput.pdf");

       } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
           LOGGER.error("Exception encountered while executing operation", ex);
       }
   }
 }
     
Run the sample:
cd OcrPDFWithOptions
dotnet run OcrPDFWithOptions.csproj
// Get the samples from https://www.adobe.com/go/dcservicessdk_net_samples
using System.IO;
using System;
using log4net.Repository;
using log4net.Config;
using log4net;
using System.Reflection;
using Adobe.DocumentCloud.Services;
using Adobe.DocumentCloud.Services.auth;
using Adobe.DocumentCloud.Services.options.ocr;
using Adobe.DocumentCloud.Services.pdfops;
using Adobe.DocumentCloud.Services.io;
using Adobe.DocumentCloud.Services.exception;

namespace OcrPDFWithOptions
{
 class Program
 {
   private static readonly ILog log = LogManager.GetLogger(typeof(Program));
   static void Main()
   {
     //Configure the logging
     ConfigureLogging();
     try
     {
       // Initial setup, create credentials instance.
       Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()
               .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json")
               .Build();

       //Create an ExecutionContext using credentials and create a new operation instance.
       ExecutionContext executionContext = ExecutionContext.Create(credentials);
       OCROperation ocrOperation = OCROperation.CreateNew();

       // Set operation input from a source file.
       FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"ocrWithOptionsInput.pdf");
       ocrOperation.SetInput(sourceFileRef);
       // Build OCR options from supported locales and OCR-types and set them into the operation
       OCROptions ocrOptions = OCROptions.OCROptionsBuilder()
           .WithOcrLocale(OCRSupportedLocale.EN_US)
           .WithOcrType(OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
           .Build();
       ocrOperation.SetOptions(ocrOptions);

       // Execute the operation.
       FileRef result = ocrOperation.Execute(executionContext);

       // Save the result to the specified location.
       result.SaveAs(Directory.GetCurrentDirectory() + "/output/ocrOperationWithOptionsOutput.pdf");
     }
     catch (ServiceUsageException ex)
     {
       log.Error("Exception encountered while executing operation", ex);
     }
     // Catch more errors here . . .
   }

   static void ConfigureLogging()
   {
     ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
     XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
   }
 }
}
Run the sample:
node src/ocr/ocr-pdf-with-ocr-options.js
 const DCServicesSdk = require('@dcloud/dc-services-node-sdk');

 try {
   // Initial setup, create credentials instance.
   const credentials =  DCServicesSdk.Credentials
       .serviceAccountCredentialsBuilder()
       .fromFile("dc-services-sdk-credentials.json")
       .build();

   //Create an ExecutionContext using credentials and create a new operation instance.
   const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
       ocrOperation = DCServicesSdk.OCR.Operation.createNew();

   // Set operation input from a source file.
   const input = DCServicesSdk.FileRef.createFromLocalFile(
           'resources/ocrInput.pdf',
           DCServicesSdk.OCR.SupportedMediaTypes.pdf
       );
   ocrOperation.setInput(input);

   // Provide any custom configuration options for the operation.
   const options = new DCServicesSdk.OCR.options.OCROptions.Builder()
       .withOcrType(DCServicesSdk.OCR.options.OCRSupportedType.SEARCHABLE_IMAGE_EXACT)
       .withOcrLang(DCServicesSdk.OCR.options.OCRSupportedLocale.EN_US)
       .build();
   ocrOperation.setOptions(options);

   // Execute the operation and Save the result to the specified location.
   ocrOperation.execute(executionContext)
       .then(result => result.saveAsFile('output/ocrWithOptionsOutput.pdf'))
       .catch(err => {
           if(err instanceof DCServicesSdk.Error.ServiceApiError
               || err instanceof DCServicesSdk.Error.ServiceUsageError) {
               console.log('Exception encountered while executing operation', err);
           } else {
               console.log('Exception encountered while executing operation', err);
           }
       });
 } catch (err) {
   console.log('Exception encountered while executing operation', err);
 }