How to Rotate and Crop PDF Pages in .NET
Proper page orientation and tidy margins are vital for professional PDFs—whether for print, design, or e-discovery. With Aspose.PDF.Plugin Optimizer for .NET, you can automate the rotation and cropping of pages in any document, targeting single pages, custom ranges, or full batches.
Rotation Scenarios: Adjusting Page Orientation
- Landscape to Portrait (or vice versa): For mixed-content documents or digital/print alignment
- Selective Rotation: Rotate only certain pages (e.g., diagrams, tables, legal exhibits)
using Aspose.Pdf.Plugins;
string input = @"C:\Docs\mixed.pdf";
string output = @"C:\Docs\rotated.pdf";
var optimizer = new Optimizer();
var rotateOptions = new RotateOptions
{
Rotation = Rotation.on90, // Rotate 90 degrees clockwise
Pages = new[] { 2, 4, 6 } // Rotate only even-numbered pages
};
rotateOptions.AddInput(new FileDataSource(input));
rotateOptions.AddOutput(new FileDataSource(output));
optimizer.Process(rotateOptions);Cropping Margins: Focus on Content
- Trim whitespace, borders, or scanning artifacts
- Crop to exact dimensions for print or design layouts
var cropOptions = new CropOptions
{
CropBox = new Rectangle(50, 50, 500, 700), // x, y, width, height
Pages = new[] { 1, 2 } // Crop only specific pages
};
cropOptions.AddInput(new FileDataSource(input));
cropOptions.AddOutput(new FileDataSource(@"C:\Docs\cropped.pdf"));
optimizer.Process(cropOptions);Combined Example: Batch Rotation and Cropping
Process multiple PDFs or run both operations sequentially:
string[] pdfFiles = Directory.GetFiles(@"C:\Docs\ToProcess", "*.pdf");
foreach (var file in pdfFiles)
{
// 1. Rotate selected pages
var rotate = new RotateOptions { Rotation = Rotation.on90, Pages = new[] { 1 } };
rotate.AddInput(new FileDataSource(file));
rotate.AddOutput(new FileDataSource(file.Replace(".pdf", "_rotated.pdf")));
optimizer.Process(rotate);
// 2. Crop first page in rotated output
var crop = new CropOptions { CropBox = new Rectangle(30, 30, 400, 600), Pages = new[] { 1 } };
crop.AddInput(new FileDataSource(file.Replace(".pdf", "_rotated.pdf")));
crop.AddOutput(new FileDataSource(file.Replace(".pdf", "_final.pdf")));
optimizer.Process(crop);
}Use Cases
- Print production: Ensure documents are properly aligned for binding/finishing
- Graphic design: Crop images/diagrams to layout specs
- Document cleanup: Remove margins or rotated scans from bulk imports
Frequently Asked Questions
Q: How do I rotate only certain pages in a document?
A: Use the Pages array in RotateOptions to specify target pages.
Q: Can I crop to exact dimensions or target only some pages?
A: Yes—set CropBox and specify page numbers in CropOptions as shown above.
Q: Can I combine operations? A: Yes—run cropping and rotation sequentially, or batch-process multiple PDFs as needed.
Pro Tip: Always backup originals before batch changes, and preview results with a PDF viewer to verify layout/rotation before production printing or delivery.