Skip to content

Commit aa06953

Browse files
committed
Refactor getters and setters in annotation package
Deprecate all getters and setters that don't belong to all subclasses and add respective methods to subclasses that actually support them, move common logic to util classes. DEVSIX-1401
1 parent 476d32f commit aa06953

21 files changed

+1383
-35
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.itextpdf.kernel.pdf.annot;
2+
3+
import com.itextpdf.kernel.pdf.PdfArray;
4+
import com.itextpdf.kernel.pdf.PdfDictionary;
5+
import com.itextpdf.kernel.pdf.PdfName;
6+
7+
class BorderStyleUtil {
8+
9+
private BorderStyleUtil(){
10+
}
11+
12+
/**
13+
* Setter for the border style. Possible values are
14+
* <ul>
15+
* <li>{@link PdfAnnotation#STYLE_SOLID} - A solid rectangle surrounding the annotation.</li>
16+
* <li>{@link PdfAnnotation#STYLE_DASHED} - A dashed rectangle surrounding the annotation.</li>
17+
* <li>{@link PdfAnnotation#STYLE_BEVELED} - A simulated embossed rectangle that appears to be raised above the surface of the page.</li>
18+
* <li>{@link PdfAnnotation#STYLE_INSET} - A simulated engraved rectangle that appears to be recessed below the surface of the page.</li>
19+
* <li>{@link PdfAnnotation#STYLE_UNDERLINE} - A single line along the bottom of the annotation rectangle.</li>
20+
* </ul>
21+
* See also ISO-320001, Table 166.
22+
* @param bs original border style dictionary.
23+
* @param style The new value for the annotation's border style.
24+
* @return Updated border style dictionary entry.
25+
*/
26+
public static final PdfDictionary setStyle(PdfDictionary bs, PdfName style) {
27+
if (null == bs) {
28+
bs = new PdfDictionary();
29+
}
30+
bs.put(PdfName.S, style);
31+
return bs;
32+
}
33+
34+
/**
35+
* Setter for the dashed border style. This property has affect only if {@link PdfAnnotation#STYLE_DASHED}
36+
* style was used for border style (see {@link #setStyle(PdfDictionary, PdfName)}.
37+
* See ISO-320001 8.4.3.6, “Line Dash Pattern” for the format in which dash pattern shall be specified.
38+
*
39+
* @param bs original border style dictionary.
40+
* @param dashPattern a dash array defining a pattern of dashes and gaps that
41+
* shall be used in drawing a dashed border.
42+
* @return Updated border style dictionary entry.
43+
*/
44+
public static final PdfDictionary setDashPattern(PdfDictionary bs, PdfArray dashPattern) {
45+
if (null == bs) {
46+
bs = new PdfDictionary();
47+
}
48+
bs.put(PdfName.D, dashPattern);
49+
return bs;
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.itextpdf.kernel.pdf.annot;
2+
3+
import com.itextpdf.kernel.color.Color;
4+
import com.itextpdf.kernel.color.DeviceCmyk;
5+
import com.itextpdf.kernel.color.DeviceGray;
6+
import com.itextpdf.kernel.color.DeviceRgb;
7+
import com.itextpdf.kernel.pdf.PdfArray;
8+
import com.itextpdf.kernel.pdf.PdfName;
9+
10+
class InteriorColorUtil {
11+
12+
private InteriorColorUtil() {
13+
}
14+
15+
/**
16+
* The interior color which is used to fill areas specific for different types of annotation. For {@link PdfLineAnnotation}
17+
* and polyline annotation ({@link PdfPolyGeomAnnotation} - the annotation's line endings, for {@link PdfSquareAnnotation}
18+
* and {@link PdfCircleAnnotation} - the annotation's rectangle or ellipse, for {@link PdfRedactAnnotation} - the redacted
19+
* region after the affected content has been removed.
20+
* @return {@link Color} of either {@link DeviceGray}, {@link DeviceRgb} or {@link DeviceCmyk} type which defines
21+
* interior color of the annotation, or null if interior color is not specified.
22+
*/
23+
public static Color parseInteriorColor(PdfArray color) {
24+
if (color == null) {
25+
return null;
26+
}
27+
switch (color.size()) {
28+
case 1:
29+
return new DeviceGray(color.getAsNumber(0).floatValue());
30+
case 3:
31+
return new DeviceRgb(color.getAsNumber(0).floatValue(), color.getAsNumber(1).floatValue(), color.getAsNumber(2).floatValue());
32+
case 4:
33+
return new DeviceCmyk(color.getAsNumber(0).floatValue(), color.getAsNumber(1).floatValue(), color.getAsNumber(2).floatValue(), color.getAsNumber(3).floatValue());
34+
default:
35+
return null;
36+
}
37+
}
38+
}

kernel/src/main/java/com/itextpdf/kernel/pdf/annot/PdfAnnotation.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,31 +162,31 @@ public abstract class PdfAnnotation extends PdfObjectWrapper<PdfDictionary> {
162162

163163
/**
164164
* Annotation border style. See ISO-320001, Table 166 (S key).
165-
* Also see {@link PdfAnnotation#setBorderStyle(PdfName)}
165+
* Also see {@link BorderStyleUtil#setStyle(PdfDictionary, PdfName)}
166166
*/
167167
public static final PdfName STYLE_SOLID = PdfName.S;
168168

169169
/**
170170
* Annotation border style. See ISO-320001, Table 166 (S key).
171-
* Also see {@link PdfAnnotation#setBorderStyle(PdfName)}
171+
* Also see {@link BorderStyleUtil#setStyle(PdfDictionary, PdfName)}
172172
*/
173173
public static final PdfName STYLE_DASHED = PdfName.D;
174174

175175
/**
176176
* Annotation border style. See ISO-320001, Table 166 (S key).
177-
* Also see {@link PdfAnnotation#setBorderStyle(PdfName)}
177+
* Also see {@link BorderStyleUtil#setStyle(PdfDictionary, PdfName)}
178178
*/
179179
public static final PdfName STYLE_BEVELED = PdfName.B;
180180

181181
/**
182182
* Annotation border style. See ISO-320001, Table 166 (S key).
183-
* Also see {@link PdfAnnotation#setBorderStyle(PdfName)}
183+
* Also see {@link BorderStyleUtil#setStyle(PdfDictionary, PdfName)}
184184
*/
185185
public static final PdfName STYLE_INSET = PdfName.I;
186186

187187
/**
188188
* Annotation border style. See ISO-320001, Table 166 (S key).
189-
* Also see {@link PdfAnnotation#setBorderStyle(PdfName)}
189+
* Also see {@link BorderStyleUtil#setStyle(PdfDictionary, PdfName)}
190190
*/
191191
public static final PdfName STYLE_UNDERLINE = PdfName.U;
192192

@@ -364,6 +364,7 @@ public void setLayer(IPdfOCG layer) {
364364
* @param action {@link PdfAction} to set to this annotation.
365365
* @return this {@link PdfAnnotation} instance.
366366
*/
367+
@Deprecated
367368
public PdfAnnotation setAction(PdfAction action) {
368369
return put(PdfName.A, action.getPdfObject());
369370
}
@@ -375,6 +376,7 @@ public PdfAnnotation setAction(PdfAction action) {
375376
* @param action {@link PdfAction} to set as additional to this annotation.
376377
* @return this {@link PdfAnnotation} instance.
377378
*/
379+
@Deprecated
378380
public PdfAnnotation setAdditionalAction(PdfName key, PdfAction action) {
379381
PdfAction.setAdditionalAction(this, key, action);
380382
return this;
@@ -891,6 +893,7 @@ public PdfAnnotation setStructParentIndex(int structParentIndex) {
891893
* This flag has affect to not all kinds of annotations.
892894
* @return true if annotation is initially open, false - if closed.
893895
*/
896+
@Deprecated
894897
public boolean getOpen() {
895898
PdfBoolean open = getPdfObject().getAsBoolean(PdfName.Open);
896899
return open != null && open.getValue();
@@ -902,6 +905,7 @@ public boolean getOpen() {
902905
* @param open true if annotation shall initially be open, false - if closed.
903906
* @return this {@link PdfAnnotation} instance.
904907
*/
908+
@Deprecated
905909
public PdfAnnotation setOpen(boolean open) {
906910
return put(PdfName.Open, PdfBoolean.valueOf(open));
907911
}
@@ -925,6 +929,7 @@ public PdfAnnotation setOpen(boolean open) {
925929
* just as Acrobat and probably most other viewers expect.
926930
* @return an {@link PdfArray} of 8 × n numbers specifying the coordinates of n quadrilaterals.
927931
*/
932+
@Deprecated
928933
public PdfArray getQuadPoints() {
929934
return getPdfObject().getAsArray(PdfName.QuadPoints);
930935
}
@@ -944,6 +949,7 @@ public PdfArray getQuadPoints() {
944949
* @param quadPoints an {@link PdfArray} of 8 × n numbers specifying the coordinates of n quadrilaterals.
945950
* @return this {@link PdfAnnotation} instance.
946951
*/
952+
@Deprecated
947953
public PdfAnnotation setQuadPoints(PdfArray quadPoints) {
948954
return put(PdfName.QuadPoints, quadPoints);
949955
}
@@ -955,6 +961,7 @@ public PdfAnnotation setQuadPoints(PdfArray quadPoints) {
955961
* in drawing the annotation’s border.
956962
* @return this {@link PdfAnnotation} instance.
957963
*/
964+
@Deprecated
958965
public PdfAnnotation setBorderStyle(PdfDictionary borderStyle) {
959966
return put(PdfName.BS, borderStyle);
960967
}
@@ -973,13 +980,9 @@ public PdfAnnotation setBorderStyle(PdfDictionary borderStyle) {
973980
* @return The annotation which this method was called on.
974981
* @see PdfAnnotation#getBorderStyle()
975982
*/
983+
@Deprecated
976984
public PdfAnnotation setBorderStyle(PdfName style) {
977-
PdfDictionary styleDict = getBorderStyle();
978-
if (null == styleDict) {
979-
styleDict = new PdfDictionary();
980-
}
981-
styleDict.put(PdfName.S, style);
982-
return setBorderStyle(styleDict);
985+
return setBorderStyle(BorderStyleUtil.setStyle(getBorderStyle(), style));
983986
}
984987

985988
/**
@@ -990,13 +993,9 @@ public PdfAnnotation setBorderStyle(PdfName style) {
990993
* shall be used in drawing a dashed border.
991994
* @return this {@link PdfAnnotation} instance.
992995
*/
996+
@Deprecated
993997
public PdfAnnotation setDashPattern(PdfArray dashPattern) {
994-
PdfDictionary styleDict = getBorderStyle();
995-
if (null == styleDict) {
996-
styleDict = new PdfDictionary();
997-
}
998-
styleDict.put(PdfName.D, dashPattern);
999-
return setBorderStyle(styleDict);
998+
return setBorderStyle(BorderStyleUtil.setDashPattern(getBorderStyle(), dashPattern));
1000999
}
10011000

10021001
/**
@@ -1007,6 +1006,7 @@ public PdfAnnotation setDashPattern(PdfArray dashPattern) {
10071006
* precedence over the BS entry. For more info on BS entry see ISO-320001, Table 166.
10081007
* @return {@link PdfDictionary} which is a border style dictionary or null if it is not specified.
10091008
*/
1009+
@Deprecated
10101010
public PdfDictionary getBorderStyle() {
10111011
return getPdfObject().getAsDictionary(PdfName.BS);
10121012
}
@@ -1037,6 +1037,7 @@ public PdfString getTitle() {
10371037
* @param characteristics the {@link PdfDictionary} with additional information for appearance stream.
10381038
* @return this {@link PdfAnnotation} instance.
10391039
*/
1040+
@Deprecated
10401041
public PdfAnnotation setAppearanceCharacteristics(PdfDictionary characteristics) {
10411042
return put(PdfName.MK, characteristics);
10421043
}
@@ -1047,6 +1048,7 @@ public PdfAnnotation setAppearanceCharacteristics(PdfDictionary characteristics)
10471048
* This property affects {@link PdfWidgetAnnotation} and {@link PdfScreenAnnotation}.
10481049
* @return an appearance characteristics dictionary or null if it isn't specified.
10491050
*/
1051+
@Deprecated
10501052
public PdfDictionary getAppearanceCharacteristics() {
10511053
return getPdfObject().getAsDictionary(PdfName.MK);
10521054
}
@@ -1056,6 +1058,7 @@ public PdfDictionary getAppearanceCharacteristics() {
10561058
* changing an annotation’s appearance state etc, when the annotation is activated.
10571059
* @return {@link PdfDictionary} which defines the characteristics and behaviour of an action.
10581060
*/
1061+
@Deprecated
10591062
public PdfDictionary getAction() {
10601063
return getPdfObject().getAsDictionary(PdfName.A);
10611064
}
@@ -1066,6 +1069,7 @@ public PdfDictionary getAction() {
10661069
* @return an additional actions {@link PdfDictionary}.
10671070
* @see PdfAnnotation#getAction()
10681071
*/
1072+
@Deprecated
10691073
public PdfDictionary getAdditionalAction() {
10701074
return getPdfObject().getAsDictionary(PdfName.AA);
10711075
}

kernel/src/main/java/com/itextpdf/kernel/pdf/annot/PdfCaretAnnotation.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This file is part of the iText (R) project.
4444
package com.itextpdf.kernel.pdf.annot;
4545

4646
import com.itextpdf.kernel.geom.Rectangle;
47+
import com.itextpdf.kernel.pdf.PdfArray;
4748
import com.itextpdf.kernel.pdf.PdfDictionary;
4849
import com.itextpdf.kernel.pdf.PdfName;
4950
import com.itextpdf.kernel.pdf.PdfString;
@@ -72,4 +73,31 @@ public PdfCaretAnnotation setSymbol(PdfString symbol) {
7273
public PdfString getSymbol() {
7374
return getPdfObject().getAsString(PdfName.Sy);
7475
}
76+
77+
/**
78+
* A set of four numbers describing the numerical differences between two rectangles:
79+
* the Rect entry of the annotation and the actual boundaries of the underlying caret.
80+
*
81+
* @return null if not specified, otherwise a {@link PdfArray} with four numbers which correspond to the
82+
* differences in default user space between the left, top, right, and bottom coordinates of Rect and those
83+
* of the inner rectangle, respectively.
84+
*/
85+
public PdfArray getRectangleDifferences() {
86+
return getPdfObject().getAsArray(PdfName.RD);
87+
}
88+
89+
/**
90+
* A set of four numbers describing the numerical differences between two rectangles:
91+
* the Rect entry of the annotation and the actual boundaries of the underlying caret.
92+
*
93+
* @param rect a {@link PdfArray} with four numbers which correspond to the differences in default user space between
94+
* the left, top, right, and bottom coordinates of Rect and those of the inner rectangle, respectively.
95+
* Each value shall be greater than or equal to 0. The sum of the top and bottom differences shall be
96+
* less than the height of Rect, and the sum of the left and right differences shall be less than
97+
* the width of Rect.
98+
* @return this {@link PdfCaretAnnotation} instance.
99+
*/
100+
public PdfCaretAnnotation setRectangleDifferences(PdfArray rect) {
101+
return (PdfCaretAnnotation) put(PdfName.RD, rect);
102+
}
75103
}

0 commit comments

Comments
 (0)