@@ -763,57 +763,10 @@ protected internal virtual void PreClose(IDictionary<PdfName, int?> exclusionSiz
763
763
}
764
764
cryptoDictionary . GetPdfObject ( ) . MakeIndirect ( document ) ;
765
765
if ( fieldExist ) {
766
- PdfSignatureFormField sigField = ( PdfSignatureFormField ) acroForm . GetField ( fieldName ) ;
767
- sigField . Put ( PdfName . V , cryptoDictionary . GetPdfObject ( ) ) ;
768
- fieldLock = sigField . GetSigFieldLockDictionary ( ) ;
769
- if ( fieldLock == null && this . fieldLock != null ) {
770
- this . fieldLock . GetPdfObject ( ) . MakeIndirect ( document ) ;
771
- sigField . Put ( PdfName . Lock , this . fieldLock . GetPdfObject ( ) ) ;
772
- fieldLock = this . fieldLock ;
773
- }
774
- sigField . Put ( PdfName . P , document . GetPage ( appearance . GetPageNumber ( ) ) . GetPdfObject ( ) ) ;
775
- sigField . Put ( PdfName . V , cryptoDictionary . GetPdfObject ( ) ) ;
776
- PdfObject obj = sigField . GetPdfObject ( ) . Get ( PdfName . F ) ;
777
- int flags = 0 ;
778
- if ( obj != null && obj . IsNumber ( ) ) {
779
- flags = ( ( PdfNumber ) obj ) . IntValue ( ) ;
780
- }
781
- flags |= PdfAnnotation . LOCKED ;
782
- sigField . Put ( PdfName . F , new PdfNumber ( flags ) ) ;
783
- PdfDictionary ap = new PdfDictionary ( ) ;
784
- ap . Put ( PdfName . N , appearance . GetAppearance ( ) . GetPdfObject ( ) ) ;
785
- sigField . Put ( PdfName . AP , ap ) ;
786
- sigField . SetModified ( ) ;
766
+ fieldLock = PopulateExistingSignatureFormField ( acroForm ) ;
787
767
}
788
768
else {
789
- PdfWidgetAnnotation widget = new PdfWidgetAnnotation ( appearance . GetPageRect ( ) ) ;
790
- widget . SetFlags ( PdfAnnotation . PRINT | PdfAnnotation . LOCKED ) ;
791
- PdfSignatureFormField sigField = PdfFormField . CreateSignature ( document ) ;
792
- sigField . SetFieldName ( name ) ;
793
- sigField . Put ( PdfName . V , cryptoDictionary . GetPdfObject ( ) ) ;
794
- sigField . AddKid ( widget ) ;
795
- if ( this . fieldLock != null ) {
796
- this . fieldLock . GetPdfObject ( ) . MakeIndirect ( document ) ;
797
- sigField . Put ( PdfName . Lock , this . fieldLock . GetPdfObject ( ) ) ;
798
- fieldLock = this . fieldLock ;
799
- }
800
- int pagen = appearance . GetPageNumber ( ) ;
801
- widget . SetPage ( document . GetPage ( pagen ) ) ;
802
- PdfDictionary ap = widget . GetAppearanceDictionary ( ) ;
803
- if ( ap == null ) {
804
- ap = new PdfDictionary ( ) ;
805
- widget . Put ( PdfName . AP , ap ) ;
806
- }
807
- ap . Put ( PdfName . N , appearance . GetAppearance ( ) . GetPdfObject ( ) ) ;
808
- acroForm . AddField ( sigField , document . GetPage ( pagen ) ) ;
809
- if ( acroForm . GetPdfObject ( ) . IsIndirect ( ) ) {
810
- acroForm . SetModified ( ) ;
811
- }
812
- else {
813
- //Acroform dictionary is a Direct dictionary,
814
- //for proper flushing, catalog needs to be marked as modified
815
- document . GetCatalog ( ) . SetModified ( ) ;
816
- }
769
+ fieldLock = CreateNewSignatureFormField ( acroForm , name ) ;
817
770
}
818
771
exclusionLocations = new Dictionary < PdfName , PdfLiteral > ( ) ;
819
772
PdfLiteral lit = new PdfLiteral ( 80 ) ;
@@ -899,6 +852,103 @@ protected internal virtual void PreClose(IDictionary<PdfName, int?> exclusionSiz
899
852
}
900
853
}
901
854
855
+ /// <summary>Populates already existing signature form field in the acroForm object.</summary>
856
+ /// <remarks>
857
+ /// Populates already existing signature form field in the acroForm object.
858
+ /// This method is called during the
859
+ /// <see cref="PreClose(System.Collections.Generic.IDictionary{K, V})"/>
860
+ /// method if the signature field already exists.
861
+ /// </remarks>
862
+ /// <param name="acroForm">
863
+ ///
864
+ /// <see cref="iText.Forms.PdfAcroForm"/>
865
+ /// object in which the signature field will be populated
866
+ /// </param>
867
+ /// <returns>signature field lock dictionary</returns>
868
+ protected internal virtual PdfSigFieldLock PopulateExistingSignatureFormField ( PdfAcroForm acroForm ) {
869
+ PdfSignatureFormField sigField = ( PdfSignatureFormField ) acroForm . GetField ( fieldName ) ;
870
+ sigField . Put ( PdfName . V , cryptoDictionary . GetPdfObject ( ) ) ;
871
+ PdfSigFieldLock sigFieldLock = sigField . GetSigFieldLockDictionary ( ) ;
872
+ if ( sigFieldLock == null && this . fieldLock != null ) {
873
+ this . fieldLock . GetPdfObject ( ) . MakeIndirect ( document ) ;
874
+ sigField . Put ( PdfName . Lock , this . fieldLock . GetPdfObject ( ) ) ;
875
+ sigFieldLock = this . fieldLock ;
876
+ }
877
+ sigField . Put ( PdfName . P , document . GetPage ( appearance . GetPageNumber ( ) ) . GetPdfObject ( ) ) ;
878
+ sigField . Put ( PdfName . V , cryptoDictionary . GetPdfObject ( ) ) ;
879
+ PdfObject obj = sigField . GetPdfObject ( ) . Get ( PdfName . F ) ;
880
+ int flags = 0 ;
881
+ if ( obj != null && obj . IsNumber ( ) ) {
882
+ flags = ( ( PdfNumber ) obj ) . IntValue ( ) ;
883
+ }
884
+ flags |= PdfAnnotation . LOCKED ;
885
+ sigField . Put ( PdfName . F , new PdfNumber ( flags ) ) ;
886
+ if ( appearance . IsInvisible ( ) ) {
887
+ // According to the spec, appearance stream is not required if the width and height of the rectangle are 0
888
+ sigField . Remove ( PdfName . AP ) ;
889
+ }
890
+ else {
891
+ PdfDictionary ap = new PdfDictionary ( ) ;
892
+ ap . Put ( PdfName . N , appearance . GetAppearance ( ) . GetPdfObject ( ) ) ;
893
+ sigField . Put ( PdfName . AP , ap ) ;
894
+ }
895
+ sigField . SetModified ( ) ;
896
+ return sigFieldLock ;
897
+ }
898
+
899
+ /// <summary>Creates new signature form field and adds it to the acroForm object.</summary>
900
+ /// <remarks>
901
+ /// Creates new signature form field and adds it to the acroForm object.
902
+ /// This method is called during the
903
+ /// <see cref="PreClose(System.Collections.Generic.IDictionary{K, V})"/>
904
+ /// method if the signature field doesn't exist.
905
+ /// </remarks>
906
+ /// <param name="acroForm">
907
+ ///
908
+ /// <see cref="iText.Forms.PdfAcroForm"/>
909
+ /// object in which new signature field will be added
910
+ /// </param>
911
+ /// <param name="name">the name of the field</param>
912
+ /// <returns>signature field lock dictionary</returns>
913
+ protected internal virtual PdfSigFieldLock CreateNewSignatureFormField ( PdfAcroForm acroForm , String name ) {
914
+ PdfWidgetAnnotation widget = new PdfWidgetAnnotation ( appearance . GetPageRect ( ) ) ;
915
+ widget . SetFlags ( PdfAnnotation . PRINT | PdfAnnotation . LOCKED ) ;
916
+ PdfSignatureFormField sigField = PdfFormField . CreateSignature ( document ) ;
917
+ sigField . SetFieldName ( name ) ;
918
+ sigField . Put ( PdfName . V , cryptoDictionary . GetPdfObject ( ) ) ;
919
+ sigField . AddKid ( widget ) ;
920
+ PdfSigFieldLock sigFieldLock = sigField . GetSigFieldLockDictionary ( ) ;
921
+ if ( this . fieldLock != null ) {
922
+ this . fieldLock . GetPdfObject ( ) . MakeIndirect ( document ) ;
923
+ sigField . Put ( PdfName . Lock , this . fieldLock . GetPdfObject ( ) ) ;
924
+ sigFieldLock = this . fieldLock ;
925
+ }
926
+ int pagen = appearance . GetPageNumber ( ) ;
927
+ widget . SetPage ( document . GetPage ( pagen ) ) ;
928
+ if ( appearance . IsInvisible ( ) ) {
929
+ // According to the spec, appearance stream is not required if the width and height of the rectangle are 0
930
+ widget . Remove ( PdfName . AP ) ;
931
+ }
932
+ else {
933
+ PdfDictionary ap = widget . GetAppearanceDictionary ( ) ;
934
+ if ( ap == null ) {
935
+ ap = new PdfDictionary ( ) ;
936
+ widget . Put ( PdfName . AP , ap ) ;
937
+ }
938
+ ap . Put ( PdfName . N , appearance . GetAppearance ( ) . GetPdfObject ( ) ) ;
939
+ }
940
+ acroForm . AddField ( sigField , document . GetPage ( pagen ) ) ;
941
+ if ( acroForm . GetPdfObject ( ) . IsIndirect ( ) ) {
942
+ acroForm . SetModified ( ) ;
943
+ }
944
+ else {
945
+ //Acroform dictionary is a Direct dictionary,
946
+ //for proper flushing, catalog needs to be marked as modified
947
+ document . GetCatalog ( ) . SetModified ( ) ;
948
+ }
949
+ return sigFieldLock ;
950
+ }
951
+
902
952
/// <summary>Gets the document bytes that are hashable when using external signatures.</summary>
903
953
/// <remarks>
904
954
/// Gets the document bytes that are hashable when using external signatures.
0 commit comments