ViewerAddSignatureUsingLink.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos
{
    // In this sample, we demonstrate how to open Signature Tool from Link Annotation.

    // This and other samples in this section demonstrate the features of GcPdfViewer
    // (a JavaScript PDF viewer control included with DsPdf), mainly the ability
    // to change PDF files (add or edit annotations and AcroForm fields, rotate pages etc.)
    // when the JS viewer on the client is supported by DsPdf running on the server.
    //
    // To enable the editing features of the viewer, its supportApi property must be set
    // to a URL on the server that implements all or some of the edit supporting APIs
    // that are known to/expected by the viewer. This DsPdf demo site provides those APIs,
    // which makes it possible to demonstrate the editing when you open the PDF viewer
    // in this sample. When you download this sample, in addition to the .NET Core
    // console app project that generates the sample PDF, an ASP.NET Core project is
    // also included in the download zip (located in the GcPdfViewerWeb sub-folder of the
    // downloaded zip), which also provides the necessary APIs. In particular, it includes
    // a project that implements the APIs and provides them via a special controller.
    // It is actually the same controller that is used by this DsPdf demo site, and which
    // can be used in any ASP.NET Core site to enable the viewer editing features.
    //
    // Look at the following files in the sample download zip for more info:
    // - GcPdfViewerWeb\SupportApiDemo: the sample ASP.NET Core web site.
    // - GcPdfViewerWeb\SupportApiDemo.sln: solution to build/run the sample web site.
    // - GcPdfViewerWeb\SupportApi: support API implementation (can be used in any site).
    // - GcPdfViewerWeb\SupportApi\Controllers\GcPdfViewerController.cs: support API controller.
    // 
    // Please note that this and other samples in this section are only available in C# at this time.
    //

    
    public class ViewerAddSignatureUsingLink
    {
        public void CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Simple-Lease-Agreement-Links.pdf"));
            doc.Load(fs);
            doc.Save(stream);
        }

        public const string JS_CODE = @"
var viewer;
var tenantAdded = false, landlordAdded = false;
function applyNormalViewerLayout() {
    viewer.toolbarLayout.viewer = {
        default: ['doc-title', '$split', 'about'],
        mobile: ['doc-title', '$split', 'about'],
        fullscreen: ['$fullscreen', 'doc-title', '$split', 'about']
    };
    viewer.applyToolbarLayout();
}

function applySignedBothViewerLayout() {
    viewer.toolbarLayout.viewer = {
        default: ['save', 'doc-title', '$split', 'about'],
        mobile: ['save', 'doc-title', '$split', 'about'],
        fullscreen: ['$fullscreen', 'save', 'doc-title', '$split', 'about']
    };
    viewer.applyToolbarLayout();
}

function onSignatureAdded(resultDetails) {
    viewer.scrollAnnotationIntoView(resultDetails.pageIndex, resultDetails.annotation);
    if(resultDetails.annotation.subject === 'Tenant') {
        tenantAdded = true;
        viewer.removeAnnotation(0, '13R');
        viewer.removeAnnotation(0, '15R');
    } else if(resultDetails.annotation.subject === 'Landlord') {
        landlordAdded = true;
        viewer.removeAnnotation(0, '16R');
        viewer.removeAnnotation(0, '14R');
    }
    if(tenantAdded && landlordAdded) {
      applySignedBothViewerLayout();
    }
    console.log('Added new stamp, subject is ' + resultDetails.annotation.subject);
}

function createPdfViewer(selector, baseOptions) {
    var options = baseOptions || {};
    if (!options.supportApi) {
        options.supportApi = {
            apiUrl: (window.__baseUrl || '') + 'support-api/gc-pdf-viewer',
            token: window.afToken || '',
            webSocketUrl: false, suppressInfoMessages: true, suppressErrorMessages: true
        };
    }

    options.signTool = {
        selectedTab: 'Draw',
        saveSignature: false,
        penColor: '#000000',
        penWidth: 2,
        canvasSize: { width: 320, height: 112 },
        autoResizeCanvas: false,
        dialogLocation: 'Bottom',
        hideToolbar: true,
        hideTabs: true,      
        hideFooter: true,
        hideSaveSignature: true,
        destinationScale: 0.5,
        beforeShow: function (dialog) {
            if(dialog.subject === 'Landlord')
            {
                dialog.title = 'Landlord/Lessor';
                dialog.location = { x: 87, y: 116 };
            }
            else if(dialog.subject === 'Tenant')
            {
                dialog.title = 'Tenant/Lessee';
                dialog.location = { x: 345, y: 116 } ;
            }
            return true;
        },
        afterAdd: function (result) {
            onSignatureAdded(result);
            return true;
        }
    };
    options.restoreViewStateOnLoad = false;
    viewer = new GcPdfViewer(selector, options);
    viewer.mouseMode = 1;
    viewer.addDefaultPanels();
    viewer.addAnnotationEditorPanel();
    viewer.zoom = 70;
    applyNormalViewerLayout();
    return viewer;
}
";

    }
}