C#: Crop Pages in PDF

2024-09-18 01:10:05 Written by  support iceblue
Rate this item
(0 votes)

PDF page cropping is particularly useful in scenarios where the original document has excessive margins or borders that are not necessary for the intended use. By cropping pages, you can preserve the designated area for specific use, making the document more efficient for sharing, printing, or digital presentations. In this article, you will learn how to crop pages in PDF in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Crop a PDF Page in C#

Spire.PDF for .NET allows you specify a rectangular area, and then use the PdfPageBase.CropBox property to crop page to the specified area. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Crop the page to the specified area using PdfPageBase.CropBox property.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
using System.Drawing;
using Spire.Pdf;

namespace CropPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file from disk
            pdf.LoadFromFile("Sample1.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Crop the page by the specified area
            page.CropBox = new RectangleF(0, 300, 600, 260);

            //Save the result file
            pdf.SaveToFile("CropPDF.pdf");
            pdf.Close();
        }
    }
}

C#: Crop Pages in PDF

Crop a PDF Page and Export as an Image in C#

To accomplish this task, you can use the PdfDocument.SaveAsImage(int pageIndex, PdfImageType type) method to convert a cropped PDF page to an image. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Crop the page to the specified area using PdfPageBase.CropBox property.
  • Convert the copped page to an image using PdfDocument.SaveAsImage() method.
  • Save the image as a PNG, JPG or BMP file using Image.Save(string filename, ImageFormat format) method.
  • C#
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace CropPDFPageToImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file from disk
            pdf.LoadFromFile("Sample1.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Crop the page by the specified area
            page.CropBox = new RectangleF(0, 300, 600, 260);

            //Convert the page to an image
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap);

            //Save the image as a PNG file
            image.Save("CropPDFSaveAsImage.png", ImageFormat.Png);

            //Save the image as a JPG file
            //image.Save("ToJPG.jpg", ImageFormat.Jpeg);

            //Save the image as a BMP file
            //image.Save("ToBMP.bmp", ImageFormat.Bmp);
        }
    }
}

C#: Crop Pages in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.