Linearize PDFs
Linearize PDF
Linearizing a PDF creates a web-optimized PDF file which supports incremental access in network environments.
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.linearizepdf.LinearizePDF45 public class LinearizePDF {6 // Initialize the logger.7 private static final Logger LOGGER = LoggerFactory.getLogger(LinearizePDF.class);89 public static void main(String[] args) {1011 try {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()14 .fromFile("pdfservices-api-credentials.json")15 .build();1617 // Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 LinearizePDFOperation linearizePDFOperation = LinearizePDFOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/linearizePDFInput.pdf");23 linearizePDFOperation.setInput(source);2425 // Execute the operation26 FileRef result = linearizePDFOperation.execute(executionContext);2728 // Save the result at the specified location29 result.saveAs("output/linearizePDFOutput.pdf");3031 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {32 LOGGER.error("Exception encountered while executing operation", ex);33 }34 }35 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd LinearizePDF/4// dotnet run LinearizePDF.csproj56 namespace LinearizePDF7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials and create a new operation instance.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);24 LinearizePDFOperation linearizePDFOperation = LinearizePDFOperation.CreateNew();2526 // Set operation input from a source file.27 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"linearizePDFInput.pdf");28 linearizePDFOperation.SetInput(sourceFileRef);2930 // Execute the operation.31 FileRef result = linearizePDFOperation.Execute(executionContext);3233 // Save the result to the specified location.34 result.SaveAs(Directory.GetCurrentDirectory() + "/output/linearizePDFOutput.pdf");35 }36 catch (ServiceUsageException ex)37 {38 log.Error("Exception encountered while executing operation", ex);39 }40 // Catch more errors here . . .41 }4243 static void ConfigureLogging()44 {45 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());46 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));47 }48 }49 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/linearizepdf/linearize-pdf.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .serviceAccountCredentialsBuilder()11 .fromFile("pdfservices-api-credentials.json")12 .build();1314 // Create an ExecutionContext using credentials and create a new operation instance.15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),16 linearizePDF = PDFServicesSdk.LinearizePDF,17 linearizePDFOperation = linearizePDF.Operation.createNew();1819 // Set operation input from a source file.20 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/linearizePDFInput.pdf');21 linearizePDFOperation.setInput(input);2223 // Execute the operation and Save the result to the specified location.24 linearizePDFOperation.execute(executionContext)25 .then(result => result.saveAsFile('output/linearizePDFOutput.pdf'))26 .catch(err => {27 if(err instanceof PDFServicesSdk.Error.ServiceApiError28 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {29 console.log('Exception encountered while executing operation', err);30 } else {31 console.log('Exception encountered while executing operation', err);32 }33 });34 } catch (err) {35 console.log('Exception encountered while executing operation', err);36 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-linearizePDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"documentIn\": {12 \"cpf:location\": \"InputFile0\",13 \"dc:format\": \"application/pdf\"14 }15 },16 \"cpf:engine\": {17 \"repo:assetId\": \"urn:aaid:cpf:Service-e4d5f0b75e5d43ea9eaa187860772d27\"18 },19 \"cpf:outputs\": {20 \"documentOut\": {21 \"cpf:location\": \"multipartLabelOut\",22 \"dc:format\": \"application/pdf\"23 }24 }25}"' \26--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'