wpf-403483-controls-and-libraries-pdf-viewer-examples-pdf-facade-api-how-to-clear-page-content.md
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:
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;
}
Imports DevExpress.Pdf
Imports System
Imports System.Windows.Input
Private startSelectionPosition As PdfDocumentPosition
Public Sub New()
InitializeComponent()
pdfViewer.OpenDocument("Demo.pdf")
AddHandler pdfViewer.MouseDown, AddressOf PdfViewer_MouseDown
AddHandler pdfViewer.MouseUp, AddressOf PdfViewer_MouseUp
End Sub
Private Sub PdfViewer_MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
' Retrieve the end position of the selection
Dim endSelectionPosition As PdfDocumentPosition =
pdfViewer.ConvertPixelToDocumentPosition(e.GetPosition(pdfViewer))
If endSelectionPosition Is Nothing OrElse startSelectionPosition Is Nothing Then
Return
End If
If endSelectionPosition.Point.Equals(startSelectionPosition.Point) Then
Return
End If
' Define the selection area
Dim left As Double = Math.Min(startSelectionPosition.Point.X, endSelectionPosition.Point.X)
Dim bottom As Double = Math.Min(startSelectionPosition.Point.Y, endSelectionPosition.Point.Y)
Dim right As Double = Math.Max(startSelectionPosition.Point.X, endSelectionPosition.Point.X)
Dim top As Double = Math.Max(startSelectionPosition.Point.Y, endSelectionPosition.Point.Y)
Dim rectangle As New PdfRectangle(left, bottom, right, top)
Dim pageNumber As Integer = startSelectionPosition.PageNumber
' Retrieve the page properties
Dim pageFacade As PdfPageFacade = pdfViewer.GetDocumentFacade().Pages(pageNumber - 1)
' Set what content type to keep
Dim options As New PdfClearContentOptions() With {.ClearAnnotations = False, .ClearGraphics = False, .ClearImages = False}
' Clear the selected area
pageFacade.ClearContent(rectangle, options)
End Sub
Private Sub PdfViewer_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
' Get the start position of the selection
startSelectionPosition = pdfViewer.ConvertPixelToDocumentPosition(e.GetPosition(pdfViewer), True)
If startSelectionPosition Is Nothing Then
Return
End If
End Sub