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

namespace GcPdfWeb.Samples
{
    // This sample shows how to replace the standard GcPdfViewer's context menu
    // with a custom one.
    //
    // Note that adding a custom context menu does not require SupportApi to be enabled,
    // this is a purely client side feature.
    //
    // The JavaScript function that is used in this sample to customize the menu
    // checks whether anything is selected in the viewer, and if there is a selection
    // replaces the default menu with one that allows to search for the selected text
    // using Google or Bing. Its source is below under '#if VIEWER_CUSTOMIZATION'.
    // 
    public class ViewerContextMenu
    {
        public void CreatePDF(Stream stream)
        {
            GcPdfDocument doc = new GcPdfDocument();
            using (var fs = new FileStream(Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"), FileMode.Open, FileAccess.Read))
            {
                doc.Load(fs);
                doc.Save(stream);
            }
        }

        // Used by SupportApiDemo to initialize GcPdfViewer.
        public static GcPdfViewerSupportApiDemo.Models.PdfViewerOptions PdfViewerOptions
        {
            get => new GcPdfViewerSupportApiDemo.Models.PdfViewerOptions(GcPdfViewerSupportApiDemo.Models.PdfViewerOptions.Options.None);
        }

#if VIEWER_CUSTOMIZATION
    //VIEWER: GcPdfViewerWeb/SupportApiDemo/Pages/PdfEditor.cshtml#// Viewer opened.{{
    modifyContextMenuSample(viewer);
    function modifyContextMenuSample(viewer) {
        viewer.options.onBeforeOpenContextMenu = function (items, mousePosition, viewer) {
            var selectedText = viewer.getSelectedText();
            if (selectedText) {
                // Remove existent items:
                items.splice(0, items.length);
                // Add own menu items:
                items.push({
                    type: 'button',
                    text: 'Search using Google',
                    onClick: function () {
                        window.open('http://www.google.com/search?q=' + encodeURI(selectedText), '_blank');
                    }
                });
                items.push({
                    type: 'button',
                    text: 'Search using Bing',
                    onClick: function () {
                        window.open('https://www.bing.com/search?q=' + encodeURI(selectedText), '_blank');
                    }
                });
            }
            return true;
        };
    }
    //}}

    //VIEWER: GcPdfViewerWeb/SupportApiDemo-WebForms/Default.aspx#// Viewer opened.{{
    modifyContextMenuSample(viewer);
    function modifyContextMenuSample(viewer) {
        viewer.options.onBeforeOpenContextMenu = function (items, mousePosition, viewer) {
            var selectedText = viewer.getSelectedText();
            if (selectedText) {
                // Remove existent items:
                items.splice(0, items.length);
                // Add own menu items:
                items.push({
                    type: 'button',
                    text: 'Search using Google',
                    onClick: function () {
                        window.open('http://www.google.com/search?q=' + encodeURI(selectedText), '_blank');
                    }
                });
                items.push({
                    type: 'button',
                    text: 'Search using Bing',
                    onClick: function () {
                        window.open('https://www.bing.com/search?q=' + encodeURI(selectedText), '_blank');
                    }
                });
            }
            return true;
        };
    }
    //}}
#endif
    }
}