Linearize PDFs
Optimize PDFs for quick viewing on the web, especially for mobile clients. Linearization allows your end users to view large PDF documents incrementally so that they can view pages much faster in lower bandwidth conditions.
REST API
See our public API Reference for Linearize PDF
Linearize PDF
Linearizing a PDF creates a web-optimized PDF file which supports incremental access in network environments.
Please refer the API usage guide to understand how to use our APIs.
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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/linearizePDFInput.pdf").toPath())) {12 // Initial setup, create credentials instance13 Credentials credentials = new ServicePrincipalCredentials(14 System.getenv("PDF_SERVICES_CLIENT_ID"),15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));1617 // Creates a PDF Services instance18 PDFServices pdfServices = new PDFServices(credentials);1920 // Creates an asset(s) from source file(s) and upload21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());2223 // Creates a new job instance24 LinearizePDFJob linearizePDFJob = new LinearizePDFJob(asset);2526 // Submit the job and gets the job result27 String location = pdfServices.submit(linearizePDFJob);28 PDFServicesResponse<LinearizePDFResult> pdfServicesResponse = pdfServices.getJobResult(location, LinearizePDFResult.class);2930 // Get content from the resulting asset(s)31 Asset resultAsset = pdfServicesResponse.getResult().getAsset();32 StreamAsset streamAsset = pdfServices.getContent(resultAsset);3334 // Creates an output stream and copy stream asset's content to it35 Files.createDirectories(Paths.get("output/"));36 OutputStream outputStream = Files.newOutputStream(new File("output/linearizePDFOutput.pdf").toPath());37 LOGGER.info("Saving asset at output/linearizePDFOutput.pdf");38 IOUtils.copy(streamAsset.getInputStream(), outputStream);39 outputStream.close();40 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {41 LOGGER.error("Exception encountered while executing operation", ex);42 }43 }44 }
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.ServicePrincipalCredentialsBuilder()19 .WithClientId("PDF_SERVICES_CLIENT_ID")20 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")21 .Build();2223 // Create an ExecutionContext using credentials and create a new operation instance.24 ExecutionContext executionContext = ExecutionContext.Create(credentials);25 LinearizePDFOperation linearizePDFOperation = LinearizePDFOperation.CreateNew();2627 // Set operation input from a source file.28 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"linearizePDFInput.pdf");29 linearizePDFOperation.SetInput(sourceFileRef);3031 // Execute the operation.32 FileRef result = linearizePDFOperation.Execute(executionContext);3334 // Save the result to the specified location.35 result.SaveAs(Directory.GetCurrentDirectory() + "/output/linearizePDFOutput.pdf");36 }37 catch (ServiceUsageException ex)38 {39 log.Error("Exception encountered while executing operation", ex);40 }41 // Catch more errors here . . .42 }4344 static void ConfigureLogging()45 {46 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());47 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));48 }49 }50 }
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.js45const {6 ServicePrincipalCredentials,7 PDFServices,8 MimeType,9 LinearizePDFJob,10 LinearizePDFResult,11 SDKError,12 ServiceUsageError,13 ServiceApiError14} = require("@dcloud/pdfservices-node-sdk");15const fs = require("fs");1617(async () => {18 let readStream;19 try {20 // Initial setup, create credentials instance21 const credentials = new ServicePrincipalCredentials({22 clientId: process.env.PDF_SERVICES_CLIENT_ID,23 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET24 });2526 // Creates a PDF Services instance27 const pdfServices = new PDFServices({credentials});2829 // Creates an asset(s) from source file(s) and upload30 readStream = fs.createReadStream("./linearizePDFInput.pdf");31 const inputAsset = await pdfServices.upload({32 readStream,33 mimeType: MimeType.PDF34 });3536 // Creates a new job instance37 const job = new LinearizePDFJob({inputAsset});3839 // Submit the job and get the job result40 const pollingURL = await pdfServices.submit({job});41 const pdfServicesResponse = await pdfServices.getJobResult({42 pollingURL,43 resultType: LinearizePDFResult44 });4546 // Get content from the resulting asset(s)47 const resultAsset = pdfServicesResponse.result.asset;48 const streamAsset = await pdfServices.getContent({asset: resultAsset});4950 // Creates an output stream and copy stream asset's content to it51 const outputFilePath = "./linearizePDFOutput.pdf";52 console.log(`Saving asset at ${outputFilePath}`);5354 const outputStream = fs.createWriteStream(outputFilePath);55 streamAsset.readStream.pipe(outputStream);56 } catch (err) {57 if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {58 console.log("Exception encountered while executing operation", err);59 } else {60 console.log("Exception encountered while executing operation", err);61 }62 } finally {63 readStream?.destroy();64 }65})();
Copied to your clipboard1// Please refer our REST API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Linearize-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/linearizepdf' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718"10}'