@@ -58,6 +58,8 @@ This file is part of the iText (R) project.
58
58
import com .itextpdf .kernel .color .DeviceRgb ;
59
59
import com .itextpdf .kernel .font .PdfFont ;
60
60
import com .itextpdf .kernel .font .PdfFontFactory ;
61
+ import com .itextpdf .kernel .geom .AffineTransform ;
62
+ import com .itextpdf .kernel .geom .Matrix ;
61
63
import com .itextpdf .kernel .geom .Rectangle ;
62
64
import com .itextpdf .kernel .pdf .*;
63
65
import com .itextpdf .kernel .pdf .action .PdfAction ;
@@ -1654,8 +1656,10 @@ public PdfFormField setRotation(int degRotation) {
1654
1656
PdfDictionary mk = getWidgets ().get (0 ).getAppearanceCharacteristics ();
1655
1657
if (mk == null ) {
1656
1658
mk = new PdfDictionary ();
1659
+ this .put (PdfName .MK ,mk );
1657
1660
}
1658
1661
mk .put (PdfName .R , new PdfNumber (degRotation ));
1662
+
1659
1663
this .rotation = degRotation ;
1660
1664
regenerateField ();
1661
1665
return this ;
@@ -1761,17 +1765,74 @@ public boolean regenerateField() {
1761
1765
fontSize = (float )DEFAULT_FONT_SIZE ;
1762
1766
}
1763
1767
1764
- int rotation = 0 ;
1768
+ //Apply Page rotation, clockwise
1769
+ int pageRotation = 0 ;
1765
1770
if (page != null ) {
1766
- rotation = page .getRotation ();
1771
+ pageRotation = page .getRotation ();
1767
1772
}
1768
1773
PdfArray matrix = null ;
1769
- if (rotation != 0 && rotation % 90 == 0 ) {
1770
- double angle = Math .PI * rotation / 180 ;
1771
- matrix = new PdfArray (new double []{Math .cos (angle ), Math .sin (angle ), -Math .sin (angle ), Math .cos (angle ), 0 , 0 });
1774
+ if (pageRotation % 90 == 0 ) {
1775
+ //Cast angle to [0, 360]
1776
+ double angle = pageRotation %360 ;
1777
+ //Get angle in radians
1778
+ angle = Math .PI * angle / 180 ;
1779
+ //Calculate origin offset
1780
+ double translationWidth = 0 ;
1781
+ double translationHeight = 0 ;
1782
+ if (angle >= Math .PI /2 && angle <= Math .PI ){
1783
+ translationWidth = bBox .toRectangle ().getWidth ();
1784
+ }
1785
+ if (angle >= Math .PI ){
1786
+ translationHeight = bBox .toRectangle ().getHeight ();
1787
+ }
1788
+ //Store rotation and translation in the matrix
1789
+ matrix = new PdfArray (new double []{Math .cos (angle ), Math .sin (angle ), -Math .sin (angle ), Math .cos (angle ),translationWidth , translationHeight });
1772
1790
Rectangle rect = bBox .toRectangle ();
1773
- rect .setWidth (bBox .toRectangle ().getHeight ());
1774
- rect .setHeight (bBox .toRectangle ().getWidth ());
1791
+ //If the angle is 90 or 270, height and width of the bounding box need to be switched
1792
+ if (angle == Math .PI /2 || angle == 3 *Math .PI /2 ){
1793
+ rect .setWidth (bBox .toRectangle ().getHeight ());
1794
+ rect .setHeight (bBox .toRectangle ().getWidth ());
1795
+ }
1796
+ //Copy Bounding box
1797
+ bBox = new PdfArray (rect );
1798
+ }
1799
+ //Apply field rotation
1800
+ float rotation = 0 ;
1801
+ if (this .getPdfObject ().getAsDictionary (PdfName .MK )!= null
1802
+ && this .getPdfObject ().getAsDictionary (PdfName .MK ).get (PdfName .R ) != null ){
1803
+ rotation = this .getPdfObject ().getAsDictionary (PdfName .MK ).getAsFloat (PdfName .R );
1804
+ }
1805
+ //rotation = this.rotation;
1806
+ //Field rotation is specified as counterclockwise
1807
+ //rotation *=-1;
1808
+ if (rotation % 90 == 0 ) {
1809
+ //Cast angle to [0, 360]
1810
+ double angle = rotation %360 ;
1811
+ //Get angle in radians
1812
+ angle = Math .PI * angle / 180 ;
1813
+ //Calculate origin offset
1814
+ double translationWidth = 0 ;
1815
+ double translationHeight = 0 ;
1816
+ if (angle >= Math .PI /2 && angle <= Math .PI ){
1817
+ translationWidth = bBox .toRectangle ().getWidth ();
1818
+ }
1819
+ if (angle >= Math .PI ){
1820
+ translationHeight = bBox .toRectangle ().getHeight ();
1821
+ }
1822
+ //Concatenate rotation and translation into the matrix
1823
+ Matrix currentMatrix = new Matrix (matrix .getAsNumber (0 ).floatValue (),matrix .getAsNumber (1 ).floatValue (),matrix .getAsNumber (2 ).floatValue (),matrix .getAsNumber (3 ).floatValue (),matrix .getAsNumber (4 ).floatValue (),matrix .getAsNumber (5 ).floatValue ());
1824
+ Matrix toConcatenate = new Matrix ((float )Math .cos (angle ), (float )Math .sin (angle ),(float ) (-Math .sin (angle )), (float )(Math .cos (angle )),(float )translationWidth , (float )translationHeight );
1825
+ currentMatrix = currentMatrix .multiply (toConcatenate );
1826
+
1827
+ matrix = new PdfArray (new float []{currentMatrix .get (0 ), currentMatrix .get (1 ), currentMatrix .get (3 ), currentMatrix .get (4 ),currentMatrix .get (6 ), currentMatrix .get (7 )});
1828
+
1829
+ Rectangle rect = bBox .toRectangle ();
1830
+ //If the angle is 90 or 270, height and width of the bounding box need to be switched
1831
+ if (angle == Math .PI /2 || angle == 3 *Math .PI /2 ){
1832
+ rect .setWidth (bBox .toRectangle ().getHeight ());
1833
+ rect .setHeight (bBox .toRectangle ().getWidth ());
1834
+ }
1835
+ //Copy Bounding box
1775
1836
bBox = new PdfArray (rect );
1776
1837
}
1777
1838
0 commit comments