//
// 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
{
// This sample demonstrates ability to customize defaults for editing features of GcPdfViewer with support API
// provided by the server running DsPdf.
// In this sample, we change defaults for drawing annotations using the editorDefaults option.
// 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 ViewerEditorDefaults
{
public void CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
Common.Util.AddNote("This sample demonstrates the ability to customize defaults for the DsPdfViewer editing functions.", page);
// Save the PDF:
doc.Save(stream);
}
public const string JS_CODE = @"
function createPdfViewer(selector, baseOptions) {
var options = baseOptions || {};
if(!options.supportApi) {
options.supportApi = {
apiUrl: (window.__baseUrl || '') + 'support-api/gc-pdf-viewer',
token: window.afToken || '',
webSocketUrl: (window.__baseUrl || '') + 'signalr',
suppressInfoMessages: true, suppressErrorMessages: true
};
}
options.editorDefaults = {
inkAnnotation: {
borderStyle: { width: 3, style: 1 },
color: '#ff9800'
},
squareAnnotation: {
borderStyle: { width: 5, style: 2, dashArray: [8, 4] },
color: '#000000',
interiorColor: '#f44336'
},
circleAnnotation: {
borderStyle: { width: 2, style: 1 },
color: '#2196f3',
interiorColor: '#fff9c8'
},
lineAnnotation: {
borderStyle: { width: 2, style: 1 },
color: '#4caf50',
interiorColor: '#2196f3',
lineStart: 'Circle',
lineEnd: 'Circle'
},
polyLineAnnotation: {
borderStyle: { width: 2, style: 1 },
color: '#000000',
interiorColor: '#f44336',
lineStart: 'Diamond',
lineEnd: 'ClosedArrow'
},
polygonAnnotation: {
borderStyle: { width: 5, style: 1 },
color: '#ff0000',
interiorColor: '#faa19b'
},
};
var viewer = new GcPdfViewer(selector, options);
viewer.addDefaultPanels();
viewer.addAnnotationEditorPanel();
viewer.layoutMode = 1;
// Configure toolbar buttons:
viewer.toolbarLayout.viewer = {
default: ['open', 'save', '$navigation', '$split', 'text-selection', 'pan', '$zoom', '$fullscreen', 'download', 'print', 'rotate', 'view-mode', 'hide-annotations', 'doc-properties', 'about'],
mobile: ['open', 'save', '$navigation', '$split', 'text-selection', 'pan', '$zoom', '$fullscreen', 'download', 'print', 'rotate', 'view-mode', 'hide-annotations', 'doc-properties', 'about'],
fullscreen: ['$fullscreen', 'open', 'save', '$navigation', '$split', 'text-selection', 'pan', '$zoom', '$fullscreen', 'download', 'print', 'rotate', 'view-mode', 'hide-annotations', 'doc-properties', 'about']
};
viewer.toolbarLayout.annotationEditor = {
default: ['save', '$split', 'edit-select', '$split', 'edit-ink', 'edit-square', 'edit-circle', 'edit-line', 'edit-polyline', 'edit-polygon', '$split', 'edit-erase', '$split', 'edit-undo', 'edit-redo'],
mobile: ['save', '$split', 'edit-select', '$split', 'edit-ink', 'edit-square', 'edit-circle', 'edit-line', 'edit-polyline', 'edit-polygon', '$split', 'edit-erase', '$split', 'edit-undo', 'edit-redo'],
fullscreen: ['$fullscreen', 'save', '$split', 'edit-select', '$split', 'edit-ink', 'edit-square', 'edit-circle', 'edit-line', 'edit-polyline', 'edit-polygon', '$split', 'edit-erase', '$split', 'edit-undo', 'edit-redo']
};
viewer.applyToolbarLayout();
viewer.applyOptions();
return viewer;
}
";
// Used by SupportApiDemo to initialize GcPdfViewer.
public static GcPdfViewerSupportApiDemo.Models.PdfViewerOptions PdfViewerOptions
{
get => new GcPdfViewerSupportApiDemo.Models.PdfViewerOptions(
GcPdfViewerSupportApiDemo.Models.PdfViewerOptions.Options.AnnotationEditorPanel |
GcPdfViewerSupportApiDemo.Models.PdfViewerOptions.Options.ActivateAnnotationEditor,
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-ink", "edit-square", "edit-circle", "edit-line", "edit-polyline", "edit-polygon", "$split", "edit-erase", "$split", "edit-undo", "edit-redo", "save" });
}
}
}