@@ -548,6 +548,9 @@ export default class PDFForm {
548548 for ( let j = 0 , lenWidgets = widgets . length ; j < lenWidgets ; j ++ ) {
549549 const widget = widgets [ j ] ;
550550 const page = this . findWidgetPage ( widget ) ;
551+ if ( page === undefined ) {
552+ continue ;
553+ }
551554 const widgetRef = this . findWidgetAppearanceRef ( field , widget ) ;
552555
553556 const xObjectKey = page . node . newXObject ( 'FlatWidget' , widgetRef ) ;
@@ -587,6 +590,9 @@ export default class PDFForm {
587590 const widgetRef = this . findWidgetAppearanceRef ( field , widget ) ;
588591
589592 const page = this . findWidgetPage ( widget ) ;
593+ if ( page === undefined ) {
594+ continue ;
595+ }
590596 pages . add ( page ) ;
591597
592598 page . node . removeAnnot ( widgetRef ) ;
@@ -698,7 +704,7 @@ export default class PDFForm {
698704 return this . defaultFontCache . access ( ) ;
699705 }
700706
701- private findWidgetPage ( widget : PDFWidgetAnnotation ) : PDFPage {
707+ private findWidgetPage ( widget : PDFWidgetAnnotation ) : PDFPage | undefined {
702708 const pageRef = widget . P ( ) ;
703709 let page = this . doc . getPages ( ) . find ( ( x ) => x . ref === pageRef ) ;
704710 if ( page === undefined ) {
@@ -709,8 +715,13 @@ export default class PDFForm {
709715
710716 page = this . doc . findPageForAnnotationRef ( widgetRef ) ;
711717
718+ /*
719+ * In some broken PDF's there may be widgets that don't exist on any page.
720+ * We used to throw an error in such cases but it appears that other
721+ * PDF readers just ignore such PDF's.
722+ */
712723 if ( page === undefined ) {
713- throw new Error ( `Could not find page for PDFRef ${ widgetRef } ` ) ;
724+ return undefined ;
714725 }
715726 }
716727
0 commit comments