ViewerLinkExecuteCustomJsAction.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS 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;

namespace DsPdfWeb.Demos
{
    // This sample demonstrates link annotation that will execute custom JS action script.
    // As for example, we created link annotation that will call the GcPdfViewer specific method 
    // to open the Graphic Signature dialog box.
    // You can use the annotation editing features of GcPdfViewer to inspect or edit existing link annotation, or add new ones.

    // 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 ViewerLinkExecuteCustomJsAction
    {
        public void CreatePDF(Stream stream)
        {
            // This sample demonstrates link annotation that will execute custom JS action script.
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;

            // Draw some text that will represent the link:
            var t2 = g.CreateTextLayout();
            var tf2 = new TextFormat() { Font = StandardFonts.Times, FontSize = 14, FontBold = true, ForeColor = Color.Blue, Underline = true };
            t2.MarginAll = 72;            
            t2.Append("Click here to add signature", tf2);
            t2.PerformLayout(true);
            g.DrawTextLayout(t2, PointF.Empty);

            // Add a link with custom JavaScript action:
            page.Annotations.Add(new LinkAnnotation(t2.ContentRectangle, new ActionJavaScript(@"if(app.viewerType == 'GcPdfViewer') { 
  this.showSignTool({dialogLocation: 'Center', location: 'TopRight'});
        }")));

            // Draw description text:
            var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
            var tl = g.CreateTextLayout();
            tl.MarginAll = 72;
            tl.Append("This example demonstrates how to create a link annotation\n" +
                "that will call the DsPdfViewer specific method to open\n" +
                "the Graphic Signature dialog box.\n" +
                "\n" +
                "Here is the code which is used for JavaScript action:\n" +
                "\n" +
                "if(app.viewerType == 'DsPdfViewer') {\n" +
                "    this.showSignTool({dialogLocation: 'Center',\n" +
                "                                       location: 'TopRight'});\n" +
                "}\n", tf);
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, new PointF(0, 52));

            // Done:
            doc.Save(stream);
        }

        // Used by SupportApiDemo to initialize GcPdfViewer.
        public static GcPdfViewerSupportApiDemo.Models.PdfViewerOptions PdfViewerOptions
        {
            get => new GcPdfViewerSupportApiDemo.Models.PdfViewerOptions(
                GcPdfViewerSupportApiDemo.Models.PdfViewerOptions.Options.AnnotationEditorPanel,
                viewerTools: new string[] { "save", "$navigation", "$split", "text-selection", "pan", "$zoom", "$fullscreen", "download", "print", "rotate", "view-mode", "hide-annotations", "doc-properties", "about" },
                annotationEditorTools: new string[] { "edit-select", "$split", "edit-link", "edit-stamp", "$split", "edit-undo", "edit-redo", "save" });
        }
    }
}