//
// 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 example shows how to load a password protected PDF and get its page count and page sizes.
// For reference, the loaded PDF is protected with the owner password 'owner' and user password 'user'.
public class NoPassGetPageInfo
{
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 };
// Get and print some of the source PDF stats:
tl.AppendLine($"The source PDF contains {docSrc.Pages.Count} page(s).", captionFmt);
foreach (var pg in docSrc.Pages)
{
tl.AppendLine($" Page at index {pg.Index}:", captionFmt);
var sz = pg.GetRenderSize();
tl.AppendLine($" Page size is {sz.Width}x{sz.Height}.");
tl.AppendLine($" Page has {(pg.Landscape ? "landscape" : "portrait")} orientation.");
tl.AppendLine($" Page paper kind is '{pg.PaperKind}'.");
}
// 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;
}
}
}