Learn how to Crop in PDF File – Try Online & Build with C#, Java, Python
Created
June 2, 2025 13:04
-
-
Save aspose-com-gists/b7cbcc3b764aefc8a4596c8ae0ddae7f to your computer and use it in GitHub Desktop.
Crop in PDF File – Try Online & Build with C#, Java, Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
// Step 1: Load the PDF document | |
Document document = new Document("input.pdf"); | |
// Step 2: Define the crop area (x1, y1, x2, y2) in points | |
// This crops a box from 100,100 to 400,600 | |
Aspose.Pdf.Rectangle cropBox = new Aspose.Pdf.Rectangle(100, 100, 400, 600); | |
// Step 3: Apply the crop to all pages | |
foreach (Page page in document.Pages) | |
{ | |
page.CropBox = cropBox; | |
} | |
// Step 4: Save the cropped PDF | |
document.Save("output_cropped.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.aspose.pdf.*; | |
public class CropPdf { | |
public static void main(String[] args) { | |
// Step 1: Load the PDF document | |
Document document = new Document("input.pdf"); | |
// Step 2: Define crop box using coordinates (x1, y1, x2, y2) | |
Rectangle cropBox = new Rectangle(100, 100, 400, 600); | |
// Step 3: Apply crop box to all pages | |
for (Page page : document.getPages()) { | |
page.setCropBox(cropBox); | |
} | |
// Step 4: Save the cropped PDF | |
document.save("output_cropped.pdf"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pdf as ap | |
# Step 1: Load the PDF document | |
document = ap.Document("input.pdf") | |
# Step 2: Define crop rectangle (x1, y1, x2, y2) | |
crop_box = ap.Rectangle(100, 100, 400, 600) | |
# Step 3: Apply crop box to all pages | |
for page in document.pages: | |
page.crop_box = crop_box | |
# Step 4: Save the cropped PDF | |
document.save("output_cropped.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment