DocumentRestrictions.cs
//
// 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;
using GrapeCity.Documents.Pdf.Security;

namespace DsPdfWeb.Demos
{
    // This sample shows how to set restrictions on a PDF,
    // e.g. restricting the ability to print or copy content
    // from the document.
    // See also the DocumentPermissions sample, which shows
    // how to examine restrictions in a loaded PDF.
    public class DocumentRestrictions
    {
        public int CreatePDF(Stream stream)
        {
            // Create a new PDF document:
            var doc = new GcPdfDocument();
            var rc = Common.Util.AddNote("This document has the following restrictions:\n" +
                "  - printing is not allowed;\n" +
                "  - content copying is not allowed;\n" +
                "  - document assembly is not allowed.", doc.NewPage());

            // Create a Rev4 security handler and specify some restrictions:
            var ssh4 = new StandardSecurityHandlerRev4()
            {
                // EncryptionAlgorithm = EncryptionAlgorithm.AES,
                // EncryptStrings = true,
                PrintingPermissions = PrintingPermissions.Disabled,
                CopyContent = false,
                EditingPermissions = EditingPermissions.Disabled
            };
            // Assign the handler we created to the document so that it is used when saving the PDF:
            doc.Security.EncryptHandler = ssh4;

            // Add a text prompting the user to check out the source code:
            var pg = doc.Pages.Last;
            var g = pg.Graphics;
            var tl = g.CreateTextLayout();
            tl.AppendLine("See the source code of this demo for details on how to use a security handler to set PDF document restrictions.");
            tl.MaxWidth = pg.Bounds.Width - rc.X * 2;
            tl.MarginLeft = rc.X;
            tl.MarginTop = rc.Bottom + 36;
            g.DrawTextLayout(tl, PointF.Empty);

            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}