ViewerCustomValidation.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System.IO;
using GrapeCity.Documents.Pdf;
using System.Drawing;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.Actions;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.AcroForms;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf.Spec;

namespace GcPdfWeb.Samples
{
    // This example demonstrates how to execute custom validation using the validateForm method.
    public class ViewerCustomValidation
    {

        public void CreatePDF(Stream stream)
        {
            GcPdfDocument doc = new GcPdfDocument();
            using (var fs = new FileStream(Path.Combine("Resources", "PDFs", "viewer-custom-validation.pdf"), FileMode.Open, FileAccess.Read))
            {
                doc.Load(fs);
                doc.Save(stream);
            }
        }

        public const string JS_CODE = @"
var viewer;
function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function solveForm() {
  var fldValue1 = viewer.findAnnotation('fldValue1', {findField: 'fieldName'}),
      fldValue2 = viewer.findAnnotation('fldValue2', {findField: 'fieldName'}),
      fldResult = viewer.findAnnotation('fldResult', {findField: 'fieldName'});
  Promise.all([fldValue1, fldValue2, fldResult]).then(function(arr) {
    fldValue1 = arr[0][0].annotation; fldValue2 = arr[1][0].annotation; fldResult = arr[2][0].annotation;
    fldResult.fieldValue = gcd(fldValue1.fieldValue, fldValue2.fieldValue);
    viewer.updateAnnotation(0, fldResult);
  });
}
function validateForm() {
  var a, b, result, expectedAnswer;
  var validationResult = viewer.validateForm(function(fieldValue, field) {
    switch(field.fieldName) {
        case 'fldValue1':
            a = parseFloat(fieldValue);
            break;
        case 'fldValue2':
            b = parseFloat(fieldValue);
            break;
        case 'fldResult':
            result = parseFloat(fieldValue);
            break;
    }
    if(a && b && result) {
        expectedAnswer = gcd(a, b);
        if(parseFloat(fieldValue) !== expectedAnswer) {
           return 'Incorrect answer.';
        }
    }
    return true;
  }, true);
  if(expectedAnswer) {
    if(validationResult !== true) {
        viewer.showMessage(validationResult, 'Correct answer is ' + expectedAnswer, 'error');        
    } else {
        viewer.showMessage('Your answer is correct.', null, 'info');
    }
  } else {
    viewer.showMessage('Please input your answer.', null, 'warn');
  }
  setTimeout(function(){ viewer.repaint([0]); }, 100);  
}
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
        };
    }
    viewer = new GcPdfViewer(selector, options);
	//viewer.addFormEditorPanel();
    return viewer;
}";
    }
}