예제 #1
0
 internal PdfDocument(ILog log,
                      IInputBytes inputBytes,
                      HeaderVersion version,
                      CrossReferenceTable crossReferenceTable,
                      bool isLenientParsing,
                      ParsingCachingProviders cachingProviders,
                      IPageFactory pageFactory,
                      Catalog catalog,
                      DocumentInformation information,
                      EncryptionDictionary encryptionDictionary,
                      IPdfTokenScanner pdfScanner,
                      IFilterProvider filterProvider,
                      AcroFormFactory acroFormFactory)
 {
     this.log                  = log;
     this.inputBytes           = inputBytes;
     this.version              = version ?? throw new ArgumentNullException(nameof(version));
     this.isLenientParsing     = isLenientParsing;
     this.cachingProviders     = cachingProviders ?? throw new ArgumentNullException(nameof(cachingProviders));
     this.encryptionDictionary = encryptionDictionary;
     this.pdfScanner           = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
     this.filterProvider       = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
     Information               = information ?? throw new ArgumentNullException(nameof(information));
     pages        = new Pages(log, catalog, pageFactory, isLenientParsing, pdfScanner);
     Structure    = new Structure(catalog, crossReferenceTable, pdfScanner);
     documentForm = new Lazy <AcroForm>(() => acroFormFactory.GetAcroForm(catalog));
 }
예제 #2
0
        private static DictionaryToken ParseTrailer(CrossReferenceTable crossReferenceTable, bool isLenientParsing, IPdfTokenScanner pdfTokenScanner,
                                                    out EncryptionDictionary encryptionDictionary)
        {
            encryptionDictionary = null;

            if (crossReferenceTable.Trailer.EncryptionToken != null)
            {
                if (!DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner, out DictionaryToken encryptionDictionaryToken))
                {
                    throw new PdfDocumentFormatException($"Unrecognized encryption token in trailer: {crossReferenceTable.Trailer.EncryptionToken}.");
                }

                encryptionDictionary = EncryptionDictionaryFactory.Read(encryptionDictionaryToken, pdfTokenScanner);

                //throw new NotSupportedException("Cannot currently parse a document using encryption: " + crossReferenceTable.Trailer.EncryptionToken);
            }

            var rootDictionary = DirectObjectFinder.Get <DictionaryToken>(crossReferenceTable.Trailer.Root, pdfTokenScanner);

            if (!rootDictionary.ContainsKey(NameToken.Type) && isLenientParsing)
            {
                rootDictionary = rootDictionary.With(NameToken.Type, NameToken.Catalog);
            }

            return(rootDictionary);
        }
예제 #3
0
        // This method is a basically a copy of the method UglyToad.PdfPig.Parser.PdfDocumentFactory.ParseTrailer()
        private static DictionaryToken ParseCatalog(CrossReferenceTable crossReferenceTable,
                                                    IPdfTokenScanner pdfTokenScanner,
                                                    out EncryptionDictionary encryptionDictionary)
        {
            encryptionDictionary = null;

            if (crossReferenceTable.Trailer.EncryptionToken != null)
            {
                if (!DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner,
                                               out DictionaryToken encryptionDictionaryToken))
                {
                    throw new PdfDocumentFormatException($"Unrecognized encryption token in trailer: {crossReferenceTable.Trailer.EncryptionToken}.");
                }

                encryptionDictionary = EncryptionDictionaryFactory.Read(encryptionDictionaryToken, pdfTokenScanner);
            }

            var rootDictionary = DirectObjectFinder.Get <DictionaryToken>(crossReferenceTable.Trailer.Root, pdfTokenScanner);

            if (!rootDictionary.ContainsKey(NameToken.Type))
            {
                rootDictionary = rootDictionary.With(NameToken.Type, NameToken.Catalog);
            }

            return(rootDictionary);
        }
예제 #4
0
 internal PdfDocument(ILog log,
                      IInputBytes inputBytes,
                      HeaderVersion version,
                      CrossReferenceTable crossReferenceTable,
                      ParsingCachingProviders cachingProviders,
                      IPageFactory pageFactory,
                      Catalog catalog,
                      DocumentInformation information,
                      EncryptionDictionary encryptionDictionary,
                      IPdfTokenScanner pdfScanner,
                      ILookupFilterProvider filterProvider,
                      AcroFormFactory acroFormFactory,
                      BookmarksProvider bookmarksProvider,
                      bool clipPaths)
 {
     this.log                  = log;
     this.inputBytes           = inputBytes;
     this.version              = version ?? throw new ArgumentNullException(nameof(version));
     this.cachingProviders     = cachingProviders ?? throw new ArgumentNullException(nameof(cachingProviders));
     this.encryptionDictionary = encryptionDictionary;
     this.pdfScanner           = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
     this.filterProvider       = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
     this.bookmarksProvider    = bookmarksProvider ?? throw new ArgumentNullException(nameof(bookmarksProvider));
     this.clipPaths            = clipPaths;
     Information               = information ?? throw new ArgumentNullException(nameof(information));
     pages        = new Pages(catalog, pageFactory, pdfScanner);
     Structure    = new Structure(catalog, crossReferenceTable, pdfScanner);
     Advanced     = new AdvancedPdfDocumentAccess(pdfScanner, filterProvider, catalog);
     documentForm = new Lazy <AcroForm>(() => acroFormFactory.GetAcroForm(catalog));
 }
        public byte[] GetOwnersEncryptionKey(string ownerPassword,
                                             string userPassword,
                                             int revision,
                                             EncryptionDictionary encryptionDictionary)
        {
            var password       = string.IsNullOrWhiteSpace(ownerPassword) ? userPassword : ownerPassword;
            var passwordBuffer = this.PadPassword(password);

            byte[] hash;

            byte[] rc4Bytes;

            if (revision >= 3)
            {
                hash = passwordBuffer;
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    for (var x = 1; x <= 50; x++)
                    {
                        hash = md5.ComputeHash(hash);
                    }
                }

                var keyLength = encryptionDictionary.Length;
                if (keyLength == null)
                {
                    throw new InvalidOperationException("Missing length in encryption dictionary");
                }

                rc4Bytes = hash.Take(keyLength.Value).ToArray();
            }
            else
            {
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    hash = md5.ComputeHash(passwordBuffer);
                }

                if (revision == 2)
                {
                    rc4Bytes = new[] { hash[0], hash[1], hash[2], hash[3], hash[4] };
                }
                else
                {
                    rc4Bytes = null;
                }
            }

            passwordBuffer = PadPassword(userPassword);
            //var encryptedUserPassword

            return(null);
        }
예제 #6
0
        private static (IndirectReference, DictionaryToken) ParseTrailer(CrossReferenceTable crossReferenceTable, bool isLenientParsing, IPdfTokenScanner pdfTokenScanner,
                                                                         out EncryptionDictionary encryptionDictionary)
        {
            encryptionDictionary = GetEncryptionDictionary(crossReferenceTable, pdfTokenScanner);

            var rootDictionary = DirectObjectFinder.Get <DictionaryToken>(crossReferenceTable.Trailer.Root, pdfTokenScanner);

            if (!rootDictionary.ContainsKey(NameToken.Type) && isLenientParsing)
            {
                rootDictionary = rootDictionary.With(NameToken.Type, NameToken.Catalog);
            }

            return(crossReferenceTable.Trailer.Root, rootDictionary);
        }
예제 #7
0
 internal PdfDocumentEncryptedException(string message, EncryptionDictionary dictionary, Exception inner) : base(message, inner)
 {
     Dictionary = dictionary;
 }