Redact PDFs Using Coordinates in C#

PSPDFKit GdPicture.NET Library enables you to specify areas in a document and permanently redact these areas.

To redact areas in a document using coordinates, follow these steps:

  1. Create a GdPicturePDF object.

  2. Select the source PDF file by passing its path to the LoadFromFile method of the GdPicturePDF object.

  3. Specify each area to redact in the following way:

    • Select the page with the area to redact with the SelectPage method of the GdPicturePDF object.

    • Specify the coordinates of the area with the AddRedactionRegion method of the GdPicturePDF object. By default, the origin of the coordinate system is the bottom-left corner. This method takes four parameters:

      • The horizontal coordinate of the bottom-left corner of the rectangular area in points.

      • The vertical coordinate of the bottom-left corner of the rectangular area in points.

      • The width of the rectangular area in points.

      • The height of the rectangular area in points. For more information on changing the default settings, see Setting the Coordinate System Origin and Setting the Measurement Unit.

  4. Run the redaction process with the ApplyRedaction method of the GdPicturePDF object.

  5. Save the output in a PDF document with the SaveToFile method.

The example below loads a PDF document, redacts the specified areas, and then saves the redacted file in a PDF:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
// Load the source document.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Specify the areas to redact.
gdpicturePDF.SelectPage(1);
gdpicturePDF.AddRedactionRegion(120, 300, 140, 20);
gdpicturePDF.SelectPage(2);
gdpicturePDF.AddRedactionRegion(100, 280, 70, 15);
// Run the redaction process.
gdpicturePDF.ApplyRedaction();
// Save the output in a PDF document.
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Load the source document.
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Specify the areas to redact.
    gdpicturePDF.SelectPage(1)
    gdpicturePDF.AddRedactionRegion(120, 300, 140, 20)
    gdpicturePDF.SelectPage(2)
    gdpicturePDF.AddRedactionRegion(100, 280, 70, 15)
    ' Run the redaction process.
    gdpicturePDF.ApplyRedaction()
    ' Save the output in a PDF document.
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics

Setting the Coordinate System Origin

To set the origin of the coordinate system, use the SetOrigin method of the GdPicturePDF object. This method takes a member of the PdfOrigin enumeration as its parameter. For example, to set the origin to the top left, call gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft).

The reference point for the first two parameters of the AddRedactionRegion method is the point of the rectangle that’s closest to the origin. If you define a different origin for the coordinate system, the reference point also changes. For example, if the origin is the top-right corner of the document, then the first two parameters of the AddRedactionRegion method define the coordinates of the top-right corner of the rectangular area that is to be redacted.

Setting the Measurement Unit

Each parameter of the AddRedactionRegion method is expressed in the unit of measurement specified by the SetMeasurementUnit method. The default unit of measurement is points. To change the unit of measurement, use the SetMeasurementUnit method. This method takes a member of the PdfMeasurementUnit enumeration as its parameter. For example, to set the unit of measurement to centimeters, call gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter).