Back to Devexpress

Sign Documents in DevExpress PDF Viewer for .NET MAUI

maui-404876-pdf-viewer-sign-document.md

latest5.3 KB
Original Source

Sign Documents in DevExpress PDF Viewer for .NET MAUI

  • Jul 17, 2024
  • 3 minutes to read

The DevExpress PDF viewer for .NET MAUI allows you to add handwritten signatures to PDF documents. Signatures are embedded to the document as images.

Add Signature in UI

Follow the steps below to add a signature.

  1. Tap the Sign icon in the edit toolbar.

  2. In the invoked Signatures bottom sheet, select a signature from the list to insert it or add a new signature.

  3. The selected signature appears in the center of the page which occupies the most screen space. You can specify the signature’s color, resize it, or move it within the page.

  4. To create a new signature, tap Add in the Signatures bottom sheet and free-hand draw the signature in the invoked page.

Add Signature via API

The PDF viewer stores all signatures in the PdfViewer.Signatures collection.

Call the AddSignature method and pass collections of points that compose signature polylines to the method parameters.

xaml
<dx:PdfViewer SignatureColor="Black">
    <!--...-->
</dx:PdfViewer>

<dx:DXButton Clicked="DXButton_Clicked" .../>
csharp
private void DXButton_Clicked(object sender, EventArgs e) {
    // Add signature points.
    // An origin point (0,0) is the page bottom-left corner; 
    // coordinates are in document units.
    PdfPoint point1 = new PdfPoint(100, 100);
    PdfPoint point2 = new PdfPoint(110, 110);
    PdfPoint point3 = new PdfPoint(120, 100);
    PdfPoint point4 = new PdfPoint(130, 110);
    PdfPoint point5 = new PdfPoint(140, 100);
    PdfPoint point6 = new PdfPoint(150, 150);
    PdfPoint[] points = new[] { point1, point2, point3, point4, point5, point6 };

    List<IList<PdfPoint>> inks = new List<IList<PdfPoint>> { points };

    pdfViewer.AddSignature(1, inks);
}

Call the DeleteSignature method to remove a signature:

csharp
pdfViewer.DeleteSignature(pdfViewer.Signatures.First());

You can also specify the list of predefined signatures available in the bottom sheet. Populate the PdfViewer.SignatureStorage.Signatures collection. The following PdfHandwrittenSignatureStorage class methods allow you to load and save signatures:

Signatures should be in JSON format. Review an example:

json
[[[{5, 50}, {100, 150}], [{35,16}, {24,12}, {115,110}]], ...]

Save Signatures to the Document

The PDF Viewer does not automatically save newly added signatures to the document. To invoke the system Save File dialog to allow users to save the current document to the file system, call the PdfViewer.ShowSaveFileDialogAsync method or the PdfViewerCommands.ShowSaveFileDialog command. Users can also save documents in the Share UI. To invoke it, call the PdfViewer.ShareDocumentAsync method or the PdfViewerCommands.ShareDocument command. Call the PdfViewer.SaveDocumentAsync method to save the current document to a stream.

After the document is saved, users cannot edit signatures.

Respond to User Actions

SignatureCreating | SignatureCreatedOccur before/after a newly signature object is added on the page.SignatureChangedOccurs if a signature object property value is changed. Signature properties cannot be changed after the document is saved since the signature is embedded to the document as a plain image.SignatureDeleting | SignatureDeletedOccur before/after a signature is deleted.SignatureSelectionChangedOccur a signature is selected/deselected. Users can select signatures before the document is saved.

Prevent Adding Signatures

To prevent signing documents, you can disable the AllowAddSignature property. This disables the Sign icon in the Edit toolbar and the AddSignature method functionality.