Duplicate PDF pages

Learn how to clone one or more pages from a PDF and how to position them using the Visual PDF REST API.

Basic page duplication

In your pipeline definition, add a task for the duplicate-pages tool.

The options parameter allows you to specify which pages you want to duplicate from the PDF, based on their indexes.

    
      // Creating the form data for the pipeline
      const form = new FormData();

      form.append(
        'tasks',
        [
          {
            tool: 'duplicate-pages',
            // Duplicate the first and second pages
            options: { pages: [0, 1] },
          },
        ],
      );
    
  

Position of new pages

By default, new pages are inserted following their source page.

However, it is possible to define the position of the new pages

    
      // Duplicate the second page and move it to the first position
      options: { pages: [{ page: 1, position: 0 }] }
    
  

Range duplication

The API allows you to duplicate a range of pages, rather than listing all indexes one-by-one.

    
      // Duplicate the first 5 pages
      options: { pages: [{ range: [0, 4] }] }
    
  

Grouping new pages

By default, when you duplicate a range of pages, the new pages will be grouped after the initial pages.

You can change this behavior by specifying the group parameter.

    
      // Duplicate the first 5 pages, and insert them each following their initial page
      options: { pages: [{ range: [0, 4], group: false }] }
    
  

In that case, you will not be able to use the position parameter.

Multiple duplicates

By default, pages are only duplicated once, but you can define a custom number of duplications.

    
      // Duplicate the first page twice
      options: { pages: [{ page: 0, duplicates: 2 }] }
    
  

Combine duplications

You can define several duplication instructions.

The instructions are executed in the order given in the pages parameter. That way, you can use the previously duplicated pages in the next duplication instruction, or compute the right indexes according to the previous duplications.

    
      options: {
        pages: [
          // Duplicate the first page
          0,
          // Duplicate the second page and move it to the last position
          { page: 1, position: 'last' },
          // Duplicate the 3 last pages twice, insterting new pages next to their source page
          { range: ['last-3', 'last'], group: false, duplicates: 2 },
        ],
      },