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); }
// 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); }
private static EncryptionDictionary GetEncryptionDictionary(CrossReferenceTable crossReferenceTable, IPdfTokenScanner pdfTokenScanner) { if (crossReferenceTable.Trailer.EncryptionToken == null) { return(null); } if (!DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner, out DictionaryToken encryptionDictionaryToken)) { if (DirectObjectFinder.TryGet(crossReferenceTable.Trailer.EncryptionToken, pdfTokenScanner, out NullToken _)) { return(null); } throw new PdfDocumentFormatException($"Unrecognized encryption token in trailer: {crossReferenceTable.Trailer.EncryptionToken}."); } var result = EncryptionDictionaryFactory.Read(encryptionDictionaryToken, pdfTokenScanner); return(result); }