View on GitHub

Coherent PDF Command Line Tools and C/C++/Python/.NET/Java/JavaScript API Community Release

Powerful, free tools to manipulate PDF files

[Current version: 2.7.2 (October 2024)]

The Coherent PDF Command Line Tools and C/C++/Python/.NET/Java/JavaScript API allow you to manipulate existing PDF files in a variety of ways. For example:

The tools are available under the AGPL. If you are unable to abide by the terms of the AGPL, or need support, commercial licenses are available from Coherent Graphics Ltd. If you're not sure if you need a license, ask us.

Download binaries now for Windows, Mac, or Linux

Pre-built binaries are available for Windows, Mac OS 10.12 and later (x86 or ARM), Linux Intel.

Download pre-built cpdf command line tool

Download pre-built C/C++/Python/.NET API static and shared libraries

To install the python library, in addition to the DLLs, you will need to run pip install pycpdflib. The .NET library is also on Nuget, but you will need the DLLs above too.

The JavaScript API is available on npm: coherentpdf.js or on GitHub.

Build from source

Look at the source repository for cpdf, or for the C API, or for the Python API or for the .NET API or for the Java API or for the JavaScript API. You'll need to install the OCaml toolchain, and a couple of simple dependencies, but it's simple to build.

User Manual

A comprehensive user manual for the Command Line PDF tools can be found as a PDF document. The manuals for the C API and for the Python API and for the .NET API and for the Java API and for the JavaScript API are also available.

Command line examples

In all cases, on Microsoft Windows, substitute cpdf.exe for cpdf.

Select some pages from a file in.pdf and write to out.pdf:

cpdf in.pdf 1-3,12-end -o out.pdf

Merge some files together:

cpdf one.pdf two.pdf three.pdf -o merged.pdf

Split a file into single-page files page001.pdf, page002.pdf etc:

cpdf -split in.pdf -o page%%%.pdf

Encrypt a file with 128 bit AES encryption with an owner password but blank user password:

cpdf -encrypt AES "pass" "" in.pdf -o out.pdf

Output some information about a file:

cpdf -info file.pdf

Encryption: Not encrypted
Permissions: 
Linearized: true
Version: 1.4
Pages: 8
Title: catalogueproduit-UK.qxd
Author: James Peterson
Subject: 
Keywords: 
Creator: QuarkXPress: pictwpstops filter 1.0
Producer: Acrobat Distiller 6.0 for Macintosh
Created: D:20060926213913+02'00'
Modified: D:20060926213913+02'00'

Output information about each page:

cpdf -page-info file.pdf

Page 1:
MediaBox: 0.000000 0.000000 768.000000 1366.000000
CropBox: 
BleedBox: 0.000000 0.000000 768.000000 1366.000000
TrimBox: 0.000000 0.000000 768.000000 1366.000000
ArtBox: 0.000000 0.000000 768.000000 1366.000000
Rotation: 0
Page 2:
MediaBox: 0.000000 0.000000 768.000000 1366.000000
CropBox: 
BleedBox: 0.000000 0.000000 768.000000 1366.000000
TrimBox: 0.000000 0.000000 768.000000 1366.000000
ArtBox: 0.000000 0.000000 768.000000 1366.000000
Rotation: 0

Scale pages to A4 paper:

cpdf -scale-to-fit a4portrait in.pdf -o out.pdf

Crop a file:

cpdf -crop "20mm 20mm 300mm 300mm" in.pdf -o out.pdf 

Add some text with page numbers:

cpdf -add-text "Page %Page of %EndPage" -top 100pt -font "Times-Roman" -font-size 20 in.pdf -o out.pdf

Stamp one PDF file over each page of another:

cpdf -stamp-on logo.pdf in.pdf -o out.pdf

Attach a file to page 5:

cpdf -attach-file sheet.xls -to-page 5 in.pdf -o out.pdf

Combine several commands together using AND: flip a file vertically and stamp text, then encrypt.

cpdf in.pdf -vflip AND -add-text "Page %Page" AND -encrypt 128bit owner "" -o out.pdf

Compress a file without loss of information:

cpdf -squeeze in.pdf -o out.pdf

C API example

#include <stdbool.h>
#include "cpdflibwrapper.h"

int main (int argc, char ** argv)
{
  /* Initialise cpdf */
  cpdf_startup(argv);

  /* We will take the input hello.pdf and repeat it three times */
  int mergepdf = cpdf_fromFile("hello.pdf", "");

  /* Check the error state */
  if (cpdf_lastError) return 1;

  /* Clear the error state */
  cpdf_clearError();

  /* The array of PDFs to merge */
  int pdfs[] = {mergepdf, mergepdf, mergepdf};

  /* Merge them */
  int merged = cpdf_mergeSimple(pdfs, 3);
  
  if (cpdf_lastError) return 1;

  cpdf_clearError();

  /* Write output */
  cpdf_toFile(merged, "merged.pdf", false, false);

  if (cpdf_lastError) return 1;

  return 0;
}

Python API example

#Merge example
import sys
import pycpdflib

# DLL loading depends on your own platform. These are the author's settings.
if sys.platform.startswith('darwin'):
    pycpdflib.loadDLL("/Users/john/repos/python-libcpdf/libpycpdf.so")
elif sys.platform.startswith('linux'):
    pycpdflib.loadDLL("../libpycpdf.so")
elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
    os.add_dll_directory("C:\\\\OCaml64/home/JohnWhitington/python-libcpdf/")
    pycpdflib.loadDLL("libpycpdf.dll")

#We will take the input hello.pdf and repeat it three times
mergepdf = pycpdflib.fromFile('hello.pdf', '')

#The list of PDFs to merge
pdfs = [mergepdf, mergepdf, mergepdf]

#Merge them
merged = pycpdflib.mergeSimple(pdfs)

#Write output
pycpdflib.toFile(merged, 'merged.pdf', False, False)

.NET API example

//Merge example
using System;
using System.Collections.Generic;
using CoherentGraphics;

// Initialise cpdf
Cpdf.startup();

// We will take the input hello.pdf and repeat it three times
using (Cpdf.Pdf mergepdf = Cpdf.fromFile("hello.pdf", ""))
{
  // The list of PDFs to merge
  List<Cpdf.Pdf> pdfs = new List<Cpdf.Pdf> {mergepdf, mergepdf, mergepdf};

  // Merge them
  Cpdf.Pdf merged = Cpdf.mergeSimple(pdfs);

  // Write output
  Cpdf.toFile(merged, "merged.pdf", false, false);
  
  // Dispose of merged PDF
  merged.Dispose();
}

Java API example

//Merge example
import com.coherentpdf.Jcpdf

public static void main(String[] args)
{
   // Initialise cpdf
   Jcpdf jcpdf = new Jcpdf();
   try
   {
     jcpdf.startup();
   }
   catch (Jcpdf.CpdfError e)
   {
     System.out.println("Error during cpdf startup");
   }
   // We will take the input hello.pdf and repeat it three times
   try (Jcpdf.Pdf mergepdf = jcpdf.fromFile("hello.pdf", ""))
   {
     // The array of PDFs to merge
     Jcpdf.Pdf[] pdfs = {mergepdf, mergepdf, mergepdf};
     // Merge them
     Jcpdf.Pdf merged = jcpdf.mergeSimple(pdfs);
     // Write output
     jcpdf.toFile(merged, "merged.pdf", false, false);
     // Dispose of merged PDF
     merged.close();
   }
   catch (Jcpdf.CpdfError e)
   {
     System.out.println("Error during cpdf operation");
   }
}

JavaScript API example

//Merge example

//Load coherentpdf.js
const coherentpdf = require('./coherentpdf.js');

//Load the file hello.pdf from the current directory
var pdf = coherentpdf.fromFile('hello.pdf', '');

//Merge three copies of it
var merged = coherentpdf.mergeSimple([pdf, pdf, pdf]);

//Write to merged.pdf
coherentpdf.toFile(merged, 'merged.pdf', false, false);

//Clean up the two PDFs
coherentpdf.deletePdf(pdf);
coherentpdf.deletePdf(merged);

(Coherentpdf.js works in the browser too.)

Support

If you discover a bug, you can raise an issue in our issue tracker, or email for support.

Please note, though, that we prioritise support for paying customers.

FAQs

Q. My command works, but fails when I put it in a Windows Batch file.

A. Some cpdf commands use % as a special character. Use %% instead.

Q. I want to express thanks for this free program, but I'm not a commercial user. What can I do?

A. You can buy me a coffee if you like.