NoPassGetMetadata.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.Pdf.Security;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos
{
    // This example shows how to load a password protected PDF and try to read its metadata.
    // Keep in mind that in some cases metadata in a PDF may also be encrypted, in which case
    // reading it without the password is impossible.
    // For reference, the loaded PDF is protected with the owner password 'owner' and user password 'user'.
    public class NoPassGetMetadata
    {
        public int CreatePDF(Stream stream)
        {
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands-password-user.pdf"));
            // Set up DecryptionOptions to allow loading password protected PDFs without password:
            var dopt = new DecryptionOptions() { ThrowExceptionIfInvalidPassword = false };
            var docSrc = new GcPdfDocument();
            docSrc.Load(fs, dopt);

            // The PDF to hold the results:
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            // Set up a TextLayout to format the results:
            var tl = page.Graphics.CreateTextLayout();
            tl.DefaultFormat.Font = StandardFonts.Courier;
            tl.DefaultFormat.FontSize = 14;
            tl.MaxWidth = doc.PageSize.Width;
            tl.MaxHeight = doc.PageSize.Height;
            tl.MarginAll = tl.Resolution;
            var captionFmt = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.CourierBold, FontSize = tl.DefaultFormat.FontSize + 2 };

            // All security handlers before StandardSecurityHandlerRev4 always encrypted metadata,
            // the StandardSecurityHandlerRev4 has EncryptMetadata property which we need to check:
            bool metadataIsEncrypted = true;
            if (docSrc.Security.EncryptHandler is StandardSecurityHandlerRev4 ssh)
                metadataIsEncrypted = ssh.EncryptMetadata;

            if (!metadataIsEncrypted)
            {
                Metadata m = docSrc.Metadata;
                tl.AppendLine("The PDF has unencrypted metadata:", captionFmt);
                tl.AppendLine($"  Copyright: {m.Copyright}");
                tl.AppendLine($"  CreatorTool: {m.CreatorTool}");
                if (m.CreateDate.HasValue)
                    tl.AppendLine($"  CreateDate: {m.CreateDate.Value.ToUniversalTime():R}");
                tl.AppendLine($"  Description: {m.Description}");
                if (m.ModifyDate.HasValue)
                    tl.AppendLine($"  ModifyDate: {m.ModifyDate.Value.ToUniversalTime():R}");
                tl.AppendLine($"  Producer: {m.Producer}");
                tl.AppendLine($"  Title: {m.Title}");
            }
            else
            {
                tl.AppendLine("Cannot read metadata because the PDF metadata is encrypted.", captionFmt);
            }

            // Render the results:
            tl.PerformLayout(true);
            while (true)
            {
                var splitResult = tl.Split(null, out TextLayout rest);
                page.Graphics.DrawTextLayout(tl, PointF.Empty);
                if (splitResult != SplitResult.Split)
                    break;
                tl = rest;
                tl.MarginTop = tl.Resolution;
                page = doc.Pages.Add();
            }

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