ReplacePagesOperation

ReplacePagesOperation

An operation that allows specific pages in a PDF file to be replaced with pages from multiple PDF files.

For more complex use cases, refer the CombineFilesOperation

Sample Usage:


 const credentials = PDFToolsSdk.Credentials.serviceAccountCredentialsBuilder()
           .fromFile("pdftools-api-credentials.json")
           .build(),
       executionContext = PDFToolsSdk.ExecutionContext.create(credentials),
       ReplacePages = PDFToolsSdk.ReplacePages,
       replacePagesOperation = ReplacePages.Operation.createNew();

 replacePagesOperation.setBaseInput(PDFToolsSdk.FileRef.createFromLocalFile('~/Documents/replacePagesOperationBaseInput.pdf',
 	ReplacePages.SupportedSourceFormat.pdf));
 replacePagesOperation.addPagesForReplace(1, PDFToolsSdk.FileRef.createFromLocalFile('~/Documents/replacePagesOperationFileToBeReplacedWithInput.pdf',
 	ReplacePages.SupportedSourceFormat.pdf));

 replacePagesOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/replacePagesOperationOutput.pdf'))
     .catch(err => console.log(err));

Members

(static, constant) SupportedSourceFormat

Properties:
Name Type Description
pdf string

Represents "application/pdf" media type

Supported source file formats for ReplacePagesOperation.

Methods

(static) createNew() → {ReplacePagesOperation}

Constructs a ReplacePagesOperation instance.

Returns:

A new ReplacePagesOperation instance.

Type
ReplacePagesOperation

setBaseInput(baseSourceFileRefnon-null)

Sets a base input file.

Parameters:
Name Type Description
baseSourceFileRef FileRef

The base input file.

addPagesForReplace(basePageToReplacenon-null, inputFilenon-null, pageRangesopt)

Adds the pages of the input PDF file for replacing the page of the base PDF file.

This method can be invoked multiple times with the same or different input PDF files.

If pageRanges is not provided, the specified page of the base PDF file will be replaced by all the pages of the input PDF file.

Parameters:
Name Type Attributes Description
basePageToReplace Number

Page of base PDF file.

inputFile FileRef

A PDF file to be replaced with.

pageRanges PageRanges <optional>

Page ranges of the input PDF file; defaults to all the pages.

execute(contextnon-null) → {Promise.<FileRef>}

Executes this operation using the supplied context and returns a Promise which resolves to the operation result.

The resulting file may be stored in the system temporary directory (per the os.tempdir(), symlinks are resolved to the actual path). See FileRef for how temporary resources are cleaned up.

Parameters:
Name Type Description
context ExecutionContext

The context in which the operation will be executed.

Throws:
  • if an API call results in an error response.

    Type
    ServiceApiError
  • if service usage limits have been reached or credentials quota has been exhausted.

    Type
    ServiceUsageError
Returns:

A promise which resolves to the operation result.

Type
Promise.<FileRef>