@@ -762,7 +762,7 @@ public virtual void FlattenFields() {
762
762
}
763
763
// Subtype is required key, if there is no Subtype it is invalid XObject. DEVSIX-725
764
764
if ( xObject != null && xObject . GetPdfObject ( ) . Get ( PdfName . Subtype ) != null ) {
765
- Rectangle box = fieldObject . GetAsRectangle ( PdfName . Rect ) ;
765
+ Rectangle annotBBox = fieldObject . GetAsRectangle ( PdfName . Rect ) ;
766
766
if ( page . IsFlushed ( ) ) {
767
767
throw new PdfException ( PdfException . PageAlreadyFlushedUseAddFieldAppearanceToPageMethodBeforePageFlushing ) ;
768
768
}
@@ -779,15 +779,10 @@ public virtual void FlattenFields() {
779
779
TagReference tagRef = tagPointer . GetTagReference ( ) ;
780
780
canvas . OpenTag ( tagRef ) ;
781
781
}
782
- PdfArray oldMatrix = xObject . GetPdfObject ( ) . GetAsArray ( PdfName . Matrix ) ;
783
- if ( oldMatrix != null && JavaUtil . ArraysEquals ( oldMatrix . ToFloatArray ( ) , new float [ ] { 1 , 0 , 0 , 1 , 0 , 0 } )
784
- ) {
785
- Rectangle boundingBox = xObject . GetBBox ( ) . ToRectangle ( ) ;
786
- PdfArray newMatrixArray = new PdfArray ( new float [ ] { box . GetWidth ( ) / boundingBox . GetWidth ( ) , 0 , 0 , box . GetHeight
787
- ( ) / boundingBox . GetHeight ( ) , 0 , 0 } ) ;
788
- xObject . Put ( PdfName . Matrix , new PdfArray ( newMatrixArray ) ) ;
789
- }
790
- canvas . AddXObject ( xObject , box . GetX ( ) , box . GetY ( ) ) ;
782
+ AffineTransform at = CalcFieldAppTransformToAnnotRect ( xObject , annotBBox ) ;
783
+ float [ ] m = new float [ 6 ] ;
784
+ at . GetMatrix ( m ) ;
785
+ canvas . AddXObject ( xObject , m [ 0 ] , m [ 1 ] , m [ 2 ] , m [ 3 ] , m [ 4 ] , m [ 5 ] ) ;
791
786
if ( tagPointer != null ) {
792
787
canvas . CloseTag ( ) ;
793
788
}
@@ -1221,5 +1216,43 @@ private ICollection<PdfFormField> PrepareFieldsForFlattening(PdfFormField field)
1221
1216
}
1222
1217
return preparedFields ;
1223
1218
}
1219
+
1220
+ private AffineTransform CalcFieldAppTransformToAnnotRect ( PdfFormXObject xObject , Rectangle annotBBox ) {
1221
+ PdfArray bBox = xObject . GetBBox ( ) ;
1222
+ if ( bBox . Size ( ) != 4 ) {
1223
+ bBox = new PdfArray ( new Rectangle ( 0 , 0 ) ) ;
1224
+ xObject . SetBBox ( bBox ) ;
1225
+ }
1226
+ float [ ] xObjBBox = bBox . ToFloatArray ( ) ;
1227
+ PdfArray xObjMatrix = xObject . GetPdfObject ( ) . GetAsArray ( PdfName . Matrix ) ;
1228
+ Rectangle transformedRect ;
1229
+ if ( xObjMatrix != null && xObjMatrix . Size ( ) == 6 ) {
1230
+ Point [ ] xObjRectPoints = new Point [ ] { new Point ( xObjBBox [ 0 ] , xObjBBox [ 1 ] ) , new Point ( xObjBBox [ 0 ] , xObjBBox
1231
+ [ 3 ] ) , new Point ( xObjBBox [ 2 ] , xObjBBox [ 1 ] ) , new Point ( xObjBBox [ 2 ] , xObjBBox [ 3 ] ) } ;
1232
+ Point [ ] transformedAppBoxPoints = new Point [ xObjRectPoints . Length ] ;
1233
+ new AffineTransform ( xObjMatrix . ToDoubleArray ( ) ) . Transform ( xObjRectPoints , 0 , transformedAppBoxPoints , 0 , xObjRectPoints
1234
+ . Length ) ;
1235
+ float [ ] transformedRectArr = new float [ ] { float . MaxValue , float . MaxValue , - float . MaxValue , - float . MaxValue
1236
+ } ;
1237
+ foreach ( Point p in transformedAppBoxPoints ) {
1238
+ transformedRectArr [ 0 ] = ( float ) Math . Min ( transformedRectArr [ 0 ] , p . x ) ;
1239
+ transformedRectArr [ 1 ] = ( float ) Math . Min ( transformedRectArr [ 1 ] , p . y ) ;
1240
+ transformedRectArr [ 2 ] = ( float ) Math . Max ( transformedRectArr [ 2 ] , p . x ) ;
1241
+ transformedRectArr [ 3 ] = ( float ) Math . Max ( transformedRectArr [ 3 ] , p . y ) ;
1242
+ }
1243
+ transformedRect = new Rectangle ( transformedRectArr [ 0 ] , transformedRectArr [ 1 ] , transformedRectArr [ 2 ] - transformedRectArr
1244
+ [ 0 ] , transformedRectArr [ 3 ] - transformedRectArr [ 1 ] ) ;
1245
+ }
1246
+ else {
1247
+ transformedRect = new Rectangle ( 0 , 0 ) . SetBbox ( xObjBBox [ 0 ] , xObjBBox [ 1 ] , xObjBBox [ 2 ] , xObjBBox [ 3 ] ) ;
1248
+ }
1249
+ AffineTransform at = AffineTransform . GetTranslateInstance ( - transformedRect . GetX ( ) , - transformedRect . GetY ( )
1250
+ ) ;
1251
+ float scaleX = transformedRect . GetWidth ( ) == 0 ? 1 : annotBBox . GetWidth ( ) / transformedRect . GetWidth ( ) ;
1252
+ float scaleY = transformedRect . GetHeight ( ) == 0 ? 1 : annotBBox . GetHeight ( ) / transformedRect . GetHeight ( ) ;
1253
+ at . PreConcatenate ( AffineTransform . GetScaleInstance ( scaleX , scaleY ) ) ;
1254
+ at . PreConcatenate ( AffineTransform . GetTranslateInstance ( annotBBox . GetX ( ) , annotBBox . GetY ( ) ) ) ;
1255
+ return at ;
1256
+ }
1224
1257
}
1225
1258
}
0 commit comments