Skip to main content
All docs
V25.1
  • 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:

    cleared page content

    using DevExpress.Pdf;
    using System;
    using System.Windows.Input;
    
    PdfDocumentPosition startSelectionPosition;
    
    public MainWindow()
    {
        InitializeComponent();
        pdfViewer.OpenDocument("Demo.pdf");
        pdfViewer.MouseDown += PdfViewer_MouseDown;
        pdfViewer.MouseUp += PdfViewer_MouseUp;
    }
    
    private void PdfViewer_MouseUp(object sender, MouseButtonEventArgs e)
    {
        // Retrieve the end position of the selection
        PdfDocumentPosition endSelectionPosition =
             pdfViewer.ConvertPixelToDocumentPosition(e.GetPosition(pdfViewer));
        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);
    }
    
    private void PdfViewer_MouseDown(object sender, MouseButtonEventArgs e)
    {
        // Get the start position of the selection
        startSelectionPosition = pdfViewer.ConvertPixelToDocumentPosition(e.GetPosition(pdfViewer), true);
        if (startSelectionPosition == null) return;
    }