@@ -534,65 +534,71 @@ export class DocumentParser {
534534 * Decrypt an object's strings and stream data.
535535 */
536536 const decryptObject = ( obj : PdfObject , objNum : number , genNum : number ) : PdfObject => {
537- if ( ! securityHandler ?. isAuthenticated ) {
538- return obj ;
539- }
540-
541- if ( obj instanceof PdfString ) {
542- const decrypted = securityHandler . decryptString ( obj . bytes , objNum , genNum ) ;
543-
544- return new PdfString ( decrypted , obj . format ) ;
545- }
537+ try {
538+ if ( ! securityHandler ?. isAuthenticated ) {
539+ return obj ;
540+ }
546541
547- if ( obj instanceof PdfArray ) {
548- const decryptedItems : PdfObject [ ] = [ ] ;
542+ if ( obj instanceof PdfString ) {
543+ const decrypted = securityHandler . decryptString ( obj . bytes , objNum , genNum ) ;
549544
550- for ( const item of obj ) {
551- decryptedItems . push ( decryptObject ( item , objNum , genNum ) ) ;
545+ return new PdfString ( decrypted , obj . format ) ;
552546 }
553547
554- return new PdfArray ( decryptedItems ) ;
555- }
548+ if ( obj instanceof PdfArray ) {
549+ const decryptedItems : PdfObject [ ] = [ ] ;
556550
557- // Check PdfStream BEFORE PdfDict (PdfStream extends PdfDict)
558- if ( obj instanceof PdfStream ) {
559- // Check if this stream should be encrypted
560- const streamType = obj . getName ( "Type" ) ?. value ;
551+ for ( const item of obj ) {
552+ decryptedItems . push ( decryptObject ( item , objNum , genNum ) ) ;
553+ }
561554
562- if ( ! securityHandler . shouldEncryptStream ( streamType ) ) {
563- return obj ;
555+ return new PdfArray ( decryptedItems ) ;
564556 }
565557
566- // Decrypt stream data
567- const decryptedData = securityHandler . decryptStream ( obj . data , objNum , genNum ) ;
558+ // Check PdfStream BEFORE PdfDict (PdfStream extends PdfDict)
559+ if ( obj instanceof PdfStream ) {
560+ // Check if this stream should be encrypted
561+ const streamType = obj . getName ( "Type" ) ?. value ;
562+
563+ if ( ! securityHandler . shouldEncryptStream ( streamType ) ) {
564+ return obj ;
565+ }
566+
567+ // Decrypt stream data
568+ const decryptedData = securityHandler . decryptStream ( obj . data , objNum , genNum ) ;
568569
569- // Create new stream with decrypted data
570- // Copy dictionary entries (strings in dict will be decrypted when accessed)
571- const newStream = new PdfStream ( obj , decryptedData ) ;
570+ // Create new stream with decrypted data
571+ // Copy dictionary entries (strings in dict will be decrypted when accessed)
572+ const newStream = new PdfStream ( obj , decryptedData ) ;
572573
573- // Decrypt strings in the dictionary entries
574- for ( const [ key , value ] of obj ) {
575- const decryptedValue = decryptObject ( value , objNum , genNum ) ;
574+ // Decrypt strings in the dictionary entries
575+ for ( const [ key , value ] of obj ) {
576+ const decryptedValue = decryptObject ( value , objNum , genNum ) ;
576577
577- if ( decryptedValue !== value ) {
578- newStream . set ( key . value , decryptedValue ) ;
578+ if ( decryptedValue !== value ) {
579+ newStream . set ( key . value , decryptedValue ) ;
580+ }
579581 }
582+
583+ return newStream ;
580584 }
581585
582- return newStream ;
583- }
586+ if ( obj instanceof PdfDict ) {
587+ const decryptedDict = new PdfDict ( ) ;
584588
585- if ( obj instanceof PdfDict ) {
586- const decryptedDict = new PdfDict ( ) ;
589+ for ( const [ key , value ] of obj ) {
590+ decryptedDict . set ( key . value , decryptObject ( value , objNum , genNum ) ) ;
591+ }
587592
588- for ( const [ key , value ] of obj ) {
589- decryptedDict . set ( key . value , decryptObject ( value , objNum , genNum ) ) ;
593+ return decryptedDict ;
590594 }
591595
592- return decryptedDict ;
593- }
596+ return obj ;
597+ } catch ( error ) {
598+ console . warn ( `Failed to decrypt object ${ objNum } ${ genNum } :` , error ) ;
594599
595- return obj ;
600+ return obj ;
601+ }
596602 } ;
597603
598604 const getObject = ( ref : PdfRef ) : PdfObject | null => {
0 commit comments