/* ----------------------------------------------------------------- */ /// /// Create /// /// <summary> /// Creates a new instance of the PdfiumReader class with the /// specified arguments. /// </summary> /// /// <param name="src">Path of the PDF file.</param> /// <param name="qv">Password string or query.</param> /// <param name="fullaccess">Requires full access.</param> /// <param name="io">I/O handler.</param> /// /// <returns>PdfiumReader object.</returns> /// /* ----------------------------------------------------------------- */ public static PdfiumReader Create(string src, QueryMessage <IQuery <string, string>, string> qv, bool fullaccess, IO io) { var dest = new PdfiumReader(src, io); while (true) { try { dest.Load(qv.Value); var denied = fullaccess && dest.File is PdfFile pf && !pf.FullAccess; if (denied) { throw new LoadException(LoadStatus.PasswordError); } return(dest); } catch (LoadException err) { if (err.Status != LoadStatus.PasswordError) { throw; } var args = qv.Query.RequestPassword(src); if (!args.Cancel) { qv.Value = args.Value; } else { throw new OperationCanceledException(); } } } }
/* ----------------------------------------------------------------- */ /// /// Create /// /// <summary> /// Creates a new instance of the PdfiumReader class with the /// specified arguments. /// </summary> /// /// <param name="src">Path of the PDF file.</param> /// <param name="query">Password query.</param> /// <param name="fullaccess">Requires full access.</param> /// <param name="io">I/O handler.</param> /// /// <returns>PdfiumReader object.</returns> /// /* ----------------------------------------------------------------- */ public static PdfiumReader Create(string src, IQuery <string> query, bool fullaccess, IO io) { var dest = new PdfiumReader(src, io); var pass = (query as QueryValue <string>)?.Value ?? string.Empty; while (true) { try { dest.Load(pass); var denied = fullaccess && dest.File is PdfFile pf && !pf.FullAccess; if (denied) { throw new LoadException(LoadStatus.PasswordError); } return(dest); } catch (LoadException err) { if (err.Status != LoadStatus.PasswordError) { throw; } if (query is QueryValue <string> ) { throw new EncryptionException(err.Message, err); } var args = query.RequestPassword(src); if (!args.Cancel) { pass = args.Result; } else { throw new OperationCanceledException(); } } } }
/* ----------------------------------------------------------------- */ /// /// Load /// /// <summary> /// Loads the PDF document with the specified arguments. /// </summary> /// /* ----------------------------------------------------------------- */ private static void Load(string src, PdfiumReader dest, QueryMessage <IQuery <string>, string> password, OpenOption options ) { while (true) { try { dest.Load(password.Value); var denied = options.FullAccess && dest.File is PdfFile f && !f.FullAccess; if (denied) { throw new PdfiumException(PdfiumStatus.PasswordError); } else { return; } } catch (PdfiumException err) { if (err.Status != PdfiumStatus.PasswordError) { throw; } var msg = password.Source.Request(src); if (!msg.Cancel) { password.Value = msg.Value; } else { throw new OperationCanceledException("Password"); } } } }