Chapter 18

cpdf -draft [-boxes] [-draft-remove-only <n>] in.pdf [<range>] -o out.pdf

cpdf -remove-all-text in.pdf [<range>] -o out.pdf

cpdf -blacktext in.pdf [<range>] -o out.pdf

cpdf -blacklines in.pdf [<range>] -o out.pdf

cpdf -blackfills in.pdf [<range>] -o out.pdf

cpdf -thinlines <minimum thickness> in.pdf [<range>] -o out.pdf

cpdf -clean in.pdf -o out.pdf

cpdf -set-version <version number> in.pdf -o out.pdf

cpdf -copy-id-from source.pdf in.pdf -o out.pdf

cpdf -remove-id in.pdf -o out.pdf

cpdf -list-spot-colors in.pdf

cpdf -print-dict-entry <key> in.pdf

cpdf -remove-dict-entry <key> [-dict-entry-search <term>]
      in.pdf -o out.pdf

cpdf -replace-dict-entry <key> -replace-dict-entry-value <value>
     [-dict-entry-search <term>] in.pdf -o out.pdf

cpdf -remove-clipping [<range>] in.pdf -o out.pdf

18.1 Draft Documents

The -draft operation removes bitmap (photographic) images from a file, so that it can be printed with less ink. Optionally, the -boxes option can be added, filling the spaces left blank with a crossed box denoting where the image was. This is not guaranteed to be fully visible in all cases (the bitmap may be have been partially covered by vector objects or clipped in the original). For example:

cpdf -draft -boxes in.pdf -o out.pdf

To remove a single image only, specify -draft-remove-only, giving the name of the image obtained by a call to -image-resolution as described in Section 13.2 and giving the appropriate page. For example:

cpdf -draft -boxes -draft-remove-only "/Im1" in.pdf 7 -o out.pdf

To remove text instead of images, use the -remove-all-text operation:

cpdf -remove-all-text in.pdf -o out.pdf

18.2 Blackening Text, Lines and Fills

Sometimes PDF output from an application (for instance, a web browser) has text in colors which would not print well on a grayscale printer. The -blacktext operation blackens all text on the given pages so it will be readable when printed.

This will not work on text which has been converted to outlines, nor on text which is part of a form.

cpdf -blacktext in.pdf -o out.pdf

The -blacklines operation blackens all lines on the given pages.

cpdf -blacklines in.pdf -o out.pdf

The -blackfills operation blackens all fills on the given pages.

cpdf -blackfills in.pdf -o out.pdf

Contrary to their names, all these operations can use another color, if specified with -color.

18.3 Hairline Removal

Quite often, applications will use very thin lines, or even the value of 0, which in PDF means ”The thinnest possible line on the output device”. This might be fine for on-screen work, but when printed on a high resolution device, such as by a commercial printer, they may be too faint, or disappear altogether. The -thinlines operation prevents this by changing all lines thinner than <minimal thickness> to the given thickness. For example:

cpdf -thinlines 0.2mm in.pdf [<range>] -o out.pdf

Thicken all lines less than 0.2mm to that value.

18.4 Garbage Collection

Sometimes incremental updates to a file by an application, or bad applications can leave data in a PDF file which is no longer used. This function removes that unneeded data.

cpdf -clean in.pdf -o out.pdf

18.5 Change PDF Version Number

To change the pdf version number, use the -set-version operation, giving the part of the version number after the decimal point. For example:

cpdf -set-version 4 in.pdf -o out.pdf

Change file to PDF 1.4.

This does not alter any of the actual data in the file — just the supposed version number. For PDF versions starting with 2 add ten to the number. For example, for PDF version 2.0, use -set-version 10.

18.6 Copy ID

The -copy-id-from operation copies the ID from the given file to the input, writing to the output.

cpdf -copy-id-from source.pdf in.pdf -o out.pdf

Copy the id from source.pdf to the contents of in.pdf, writing to out.pdf.

If there is no ID in the source file, the existing ID is retained. You cannot use -recrypt with -copy-id-from.

18.7 Remove ID

The -remove-id operation removes the ID from a document.

cpdf -remove-id in.pdf -o out.pdf

Remove the ID from in.pdf, writing to out.pdf.

You cannot use -recrypt with -remove-id.

18.8 List Spot Colours

This operation lists the name of any “separation” color space in the given PDF file.

cpdf -list-spot-colors in.pdf

List the spot colors, one per line in in.pdf, writing to stdout.

18.9 PDF Dictionary Entries

This is for editing data within the PDF’s internal representation. Use with caution.

To print a dictionary entry:

cpdf -print-dict-entry /URI in.pdf -o out.pdf

Print all URLs in annotation hyperlinks in.pdf.

To remove a dictionary entry:

cpdf -remove-dict-entry /One in.pdf -o out.pdf

Remove the entry for /One in every dictionary in.pdf, writing to out.pdf.

cpdf -remove-dict-entry /One -dict-entry-search "1" in.pdf -o out.pdf

Replace the entry for /One in every dictionary in.pdf if the key’s value is the given value, writing to out.pdf.

To replace a dictionary entry:

cpdf -replace-dict-entry /One -replace-dict-entry-value "2"
     in.pdf -o out.pdf

Remove the entry for /One in every dictionary in.pdf, writing to out.pdf.

cpdf -replace-dict-entry /One -dict-entry-search "1"
     -replace-dict-entry-value "2" in.pdf -o out.pdf

Remove the entry for /One in every dictionary in.pdf if the key’s value is the given value, writing to out.pdf.

18.10 Removing Clipping

The -remove-clipping operation removes any clipping paths on given pages from the file.

cpdf -remove-clipping in.pdf -o out.pdf

Remove clipping paths in in.pdf, writing to out.pdf.

Java Interface

/* CHAPTER 18. Miscellaneous */ 
/** Removes images on the given pages, replacing them with crossed boxes if 
<code>boxes</code> is <code>true</code>. 
@param pdf PDF document 
@param range page range 
@param boxes add crossed boxes */ 
public native void draft(Pdf pdf, Range range, boolean boxes) 
    throws CpdfError; 
/** Removes all text from the given pages in a given document. */ 
public native void removeAllText(Pdf pdf, Range range) throws CpdfError; 
/** Blackens all text on the given pages. */ 
public native void blackText(Pdf pdf, Range range) throws CpdfError; 
/** Blackens all lines on the given pages. */ 
public native void blackLines(Pdf pdf, Range range) throws CpdfError; 
/** Blackens all fills on the given pages. */ 
public native void blackFills(Pdf pdf, Range range) throws CpdfError; 
/** Thickens every line less than <code>min_thickness</code> to 
<code>min_thickness</code>. Thickness given in points. 
@param pdf PDF document 
@param range page range 
@param min_thickness minimum thickness */ 
public native void thinLines(Pdf pdf, Range range, double min_thickness) 
    throws CpdfError; 
/** Copies the <code>/ID</code> from one document to another. 
@param pdf PDF document to copy from 
@param pdf2 PDF document to copy to */ 
public native void copyId(Pdf pdf, Pdf pdf2) throws CpdfError; 
/** Removes a documents <code>/ID</code>. */ 
public native void removeId(Pdf pdf) throws CpdfError; 
/** Sets the minor version number of a document. 
@param pdf PDF document 
@param version minor version number */ 
public native void setVersion(Pdf pdf, int version) throws CpdfError; 
/** Sets the full version number of a document. 
@param pdf PDF document 
@param major major version number 
@param minor minor version nuber */ 
public native void setFullVersion(Pdf pdf, int major, int minor) 
    throws CpdfError; 
/** Removes any dictionary entry with the given key anywhere in the 
@param pdf PDF document 
@param key key to remove */ 
public native void removeDictEntry(Pdf pdf, String key) throws CpdfError; 
/** Removes any dictionary entry with the given key whose value matches the 
given search term. 
@param pdf PDF document 
@param key key to remove 
@param searchterm search term */ 
public native void removeDictEntrySearch(Pdf pdf, String key, 
                                         String searchterm) 
    throws CpdfError; 
/** Replaces the value associated with the given key. 
@param pdf PDF document 
@param key key whose value to replace 
@param newvalue value to replace with */ 
public native void replaceDictEntry(Pdf pdf, String key, String newvalue) 
    throws CpdfError; 
/** Replaces the value associated with the given key if the existing value 
matches the search term. 
@param pdf PDF document 
@param key key whose value to replace 
@param newvalue value to replace with 
@param searchterm search term */ 
public native void replaceDictEntrySearch(Pdf pdf, String key, 
                                          String newvalue, 
                                          String searchterm) 
    throws CpdfError; 
/** Removes all clipping from pages in the given range. */ 
public native void removeClipping(Pdf pdf, Range range) throws CpdfError; 
/** Returns a JSON array containing any and all values associated with 
the given key, and fills in its length. 
@param pdf PDF document 
@param key key to search for */ 
public native byte[] getDictEntries(Pdf pdf, String key) throws CpdfError;