How to use the command pdfcrop (with examples)
- Linux
- December 25, 2023
PDFCrop is a command-line utility that detects and removes unwanted margins from each page in a PDF file. It provides various options for setting margins and specifying bounding boxes. This article provides examples of how to use the pdfcrop command for different use cases.
Use case 1: Automatically detect and remove the margin for each page in a PDF file
Code:
pdfcrop path/to/input_file.pdf path/to/output_file.pdf
Motivation: This use case is useful when you want to automatically detect and remove the margins from a PDF file without specifying any custom margins or bounding boxes.
Explanation:
pdfcrop
: The command used to crop the PDF file.path/to/input_file.pdf
: The path to the input PDF file.path/to/output_file.pdf
: The path to the output PDF file after cropping.
Example output: The command will automatically detect and remove the margins from each page in the input PDF file and save the cropped version to the specified output file.
Use case 2: Set the margins of each page to a specific value
Code:
pdfcrop path/to/input_file.pdf --margins 'left top right bottom' path/to/output_file.pdf
Motivation: This use case is useful when you want to set custom margins for each page in the PDF file.
Explanation:
--margins 'left top right bottom'
: The option to set custom margins for each page in the PDF file. Replace ’left’, ’top’, ‘right’, and ‘bottom’ with the desired margin values in points.- Other arguments are the same as in the previous use case.
Example output: The command will crop the PDF file by setting the margins to the specified values for each page and save the cropped version to the specified output file.
Use case 3: Set the margins of each page to a specific value, using the same value for left, top, right, and bottom
Code:
pdfcrop path/to/input_file.pdf --margins 300 path/to/output_file.pdf
Motivation: This use case is useful when you want to set equal margins for each side of the pages in the PDF file.
Explanation:
--margins 300
: The option to set equal margins for each side of the pages in the PDF file. Replace ‘300’ with the desired margin value in points.- Other arguments are the same as in the previous use case.
Example output: The command will crop the PDF file by setting equal margins of 300 points for each side of the pages and save the cropped version to the specified output file.
Use case 4: Use a user-defined bounding box for cropping instead of automatically detecting it
Code:
pdfcrop path/to/input_file.pdf --bbox 'left top right bottom' path/to/output_file.pdf
Motivation: This use case is useful when you want to manually specify a bounding box for cropping rather than relying on automatic detection.
Explanation:
--bbox 'left top right bottom'
: The option to set a user-defined bounding box for cropping. Replace ’left’, ’top’, ‘right’, and ‘bottom’ with the coordinates of the bounding box in points.- Other arguments are the same as in the previous use case.
Example output: The command will crop the PDF file using the specified user-defined bounding box and save the cropped version to the specified output file.
Use case 5: Use different user-defined bounding boxes for odd and even pages
Code:
pdfcrop path/to/input_file.pdf --bbox-odd 'left top right bottom' --bbox-even 'left top right bottom' path/to/output_file.pdf
Motivation: This use case is useful when you want to set different user-defined bounding boxes for odd and even pages in the PDF file.
Explanation:
--bbox-odd 'left top right bottom'
: The option to set a user-defined bounding box for odd pages in the PDF file.--bbox-even 'left top right bottom'
: The option to set a user-defined bounding box for even pages in the PDF file.- Other arguments are the same as in the previous use case.
Example output: The command will crop the PDF file using the specified user-defined bounding boxes for odd and even pages and save the cropped version to the specified output file.
Use case 6: Automatically detect margins using a lower resolution for improved performance
Code:
pdfcrop path/to/input_file.pdf --resolution 72 path/to/output_file.pdf
Motivation: This use case is useful when you have a large PDF file and want to improve the performance of margin detection by lowering the resolution.
Explanation:
--resolution 72
: The option to set a lower resolution (in dots per inch) for margin detection. Replace ‘72’ with the desired resolution value.- Other arguments are the same as in the previous use case.
Example output: The command will crop the PDF file by automatically detecting the margins using a lower resolution of 72 dpi and save the cropped version to the specified output file.
Conclusion:
The pdfcrop command is a versatile tool for cropping PDF files by removing unwanted margins. With its various options for setting margins and specifying bounding boxes, it offers flexibility in customizing the cropping process. Whether you want to automatically detect margins, set specific margin values, or use user-defined bounding boxes, pdfcrop provides the functionality to achieve your desired results.