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

namespace GcPdfWeb.Samples
{
    // This sample shows how to add the document list panel to the Viewer,
    // and fill it with a selection with available PDF documents list loaded from
    // the URL specified by documentListUrl option.

    // This and other samples in this section demonstrate the features of GcPdfViewer
    // (a JavaScript PDF viewer control included with GcPdf), 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 GcPdf 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 GcPdf 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 GcPdf 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 ViewerPdfDocumentList
    {
        public void CreatePDF(Stream stream)
        {
            // Note:
            //
            // In the documentListUrl the demo site returns a list that looks like this:
            // [
            //   {"doc_1|base-url/GetPdfFromList?name=doc_1.pdf|Open doc_1"},
            //   {"doc_2|base-url/GetPdfFromList?name=doc_2.pdf|Open doc_2"},
            //   {"doc_3|base-url/GetPdfFromList?name=doc_3.pdf|Open doc_3"}
            // ]
            //
            // Alternatively, the item parts can be specified by name:
            // [
            //   {"name": "doc_1", "path": "base-url/GetPdfFromList?name=doc_1.pdf", "title" : "Open doc_1"},
            //   {"name": "doc_2", "path": "base-url/GetPdfFromList?name=doc_2.pdf", "title" : "Open doc_2"},
            //   {"name": "doc_3", "path": "base-url/GetPdfFromList?name=doc_3.pdf", "title" : "Open doc_3"}
            // ]
            //
            // The GetPdfFromList demo URL generates and returns a PDF that shows the requested name.
            //
            GcPdfDocument doc = new GcPdfDocument();
            GcPdfGraphics g = doc.NewPage().Graphics;
            g.DrawString("Use the Document List panel in the left sidebar to select and open a document.",
                new TextFormat() { Font = StandardFonts.Times, FontSize = 12 },
                new PointF(72, 72));
            doc.Save(stream);
        }

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

        public const string JS_CODE = @"
function findBaseUrl(options) {  
  var s = window.__baseUrl || ((options.supportApi || {}).apiUrl || '');
  return s[s.length - 1] !== '/' ? s + '/' : s; 
}
function createPdfViewer(selector, baseOptions) {
    var options = baseOptions || {};
    options.documentListUrl = findBaseUrl(options) + 'DocumentList';
    options.friendlyFileName = '';
    var viewer = new GcPdfViewer(selector, options);    
    viewer.addDefaultPanels();
    var viewerDefault = viewer.toolbarLayout.viewer.default;
    viewerDefault.splice(viewerDefault.indexOf(""open""), 1);
    var documentListPanelHandle = viewer.addDocumentListPanel();
    viewer.onAfterOpen.register(function() {
        viewer.leftSidebar.menu.panels.open(documentListPanelHandle.id);
    });
    return viewer;
}
";
    }
}