Skip to main content
All docs
V24.2

How to: Use PDF Facade API to Clear Page Content

  • 3 minutes to read

The following article describes how to use PDF Facade API to clear PDF page content.

Important

The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. Refer to the DevExpress Subscription page for pricing information.

The PdfViewerExtensions.GetDocumentFacade method retrieves a PdfDocumentFacade class object that allows you to change the PDF document without access to its inner structure.

The PdfDocumentFacade.Pages property returns a list of PdfPageFacade objects that contain PDF page properties. Call the ClearContent method to remove content in one or multiple page areas. You can specify what content type to keep in the areas that are to be cleared. To do that, pass the PdfClearContentOptions class object as the method parameter.

The code sample below removes only text in selected page area:

clear page content

using DevExpress.Pdf;
using System;

public Form1()
{
    InitializeComponent();
    pdfViewer.LoadDocument("Document.pdf");
    pdfViewer.MouseDown += pdfViewer_MouseDown;
    pdfViewer.MouseUp += pdfViewer_MouseUp;
}

PdfDocumentPosition startSelectionPosition;

void pdfViewer_MouseDown(object sender, MouseEventArgs e)
{
    // Get the start position of the selection
    startSelectionPosition = pdfViewer.GetDocumentPosition(e.Location, true);
    if (startSelectionPosition == null) return;
}

void pdfViewer_MouseUp(object sender, MouseEventArgs e)
{
    // Retrieve the end position of the selection
    PdfDocumentPosition endSelectionPosition = pdfViewer.GetDocumentPosition(e.Location);
    if (endSelectionPosition == null || startSelectionPosition == null) return;
    if (endSelectionPosition.Point.Equals(startSelectionPosition.Point)) return;

    // Define the selection area
    double left = Math.Min(startSelectionPosition.Point.X, endSelectionPosition.Point.X);
    double bottom = Math.Min(startSelectionPosition.Point.Y, endSelectionPosition.Point.Y);
    double right = Math.Max(startSelectionPosition.Point.X, endSelectionPosition.Point.X);
    double top = Math.Max(startSelectionPosition.Point.Y, endSelectionPosition.Point.Y);
    PdfRectangle rectangle = new PdfRectangle(left, bottom, right, top );
    int pageNumber = startSelectionPosition.PageNumber;

    // Retrieve the page properties
    PdfPageFacade pageFacade = pdfViewer.GetDocumentFacade().Pages[pageNumber - 1];

    // Set what content type to keep
    PdfClearContentOptions options = new PdfClearContentOptions()
    {
        ClearAnnotations = false,
        ClearGraphics = false,
        ClearImages = false
    };

    // Clear the selected area
    pageFacade.ClearContent(rectangle, options);
}