• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.graphics.pdf.component;
18 
19 import android.annotation.ColorInt;
20 import android.annotation.FlaggedApi;
21 import android.annotation.NonNull;
22 import android.graphics.Color;
23 import android.graphics.RectF;
24 import android.graphics.pdf.flags.Flags;
25 import android.graphics.pdf.utils.Preconditions;
26 
27 /**
28  * Represents a free text annotation in a PDF document.
29  * <p>
30  * This class allows creating and manipulating free text
31  * annotations. A free text annotation in a PDF is a type of
32  * annotation that allows you to add text directly onto the page.
33  * <p>
34  * If text color is not set using
35  * {@link #setTextColor(int)}, the default text color is
36  * black and if the background color is not set using
37  * {@link #setBackgroundColor(int)}, the default background color is
38  * white.
39  */
40 @FlaggedApi(Flags.FLAG_ENABLE_EDIT_PDF_TEXT_ANNOTATIONS)
41 public final class FreeTextAnnotation extends PdfAnnotation {
42     @NonNull private RectF mBounds;
43     @NonNull private String mTextContent;
44     private @ColorInt int mTextColor;
45     private @ColorInt int mBackgroundColor;
46 
47     /**
48      * Creates a new free text annotation with the specified bounds and text content.
49      * <p>
50      * The default text color and background color will be black and white respectively
51      *
52      * @param bounds The bounding rectangle of the annotation.
53      * @param textContent The text content of the annotation
54      */
FreeTextAnnotation(@onNull RectF bounds, @NonNull String textContent)55     public FreeTextAnnotation(@NonNull RectF bounds, @NonNull String textContent) {
56         super(PdfAnnotationType.FREETEXT);
57         this.mBounds = bounds;
58         this.mTextContent = textContent;
59         this.mTextColor = Color.BLACK;
60         this.mBackgroundColor = Color.WHITE;
61     }
62 
63     /**
64      * Sets the bounding rectangle of the freetext annotation.
65      *
66      * @param bounds The new bounding rectangle.
67      * @throws NullPointerException if given bounds is null
68      */
setBounds(@onNull RectF bounds)69     public void setBounds(@NonNull RectF bounds) {
70         Preconditions.checkNotNull(bounds, "Bounds should not be null");
71         this.mBounds = bounds;
72     }
73 
74     /**
75      * Returns the bounding rectangle of the freetext annotation.
76      *
77      * @return The bounding rectangle.
78      */
getBounds()79     @NonNull public RectF getBounds() {
80         return mBounds;
81     }
82 
83     /**
84      * Sets the text content of the annotation.
85      *
86      * @param text The new text content.
87      */
setTextContent(@onNull String text)88     public void setTextContent(@NonNull String text) {
89         mTextContent = text;
90     }
91 
92     /**
93      * Returns the text content of the freetext annotation.
94      *
95      * @return The text content.
96      */
getTextContent()97     @NonNull public String getTextContent() {
98         return mTextContent;
99     }
100 
101     /**
102      * Sets the text color of the annotation.
103      *
104      * @param color The new text color.
105      */
setTextColor(@olorInt int color)106     public void setTextColor(@ColorInt int color) {
107         this.mTextColor = color;
108     }
109 
110     /**
111      * Returns the text color of the freetext annotation.
112      *
113      * @return The text color.
114      */
getTextColor()115     public @ColorInt int getTextColor() {
116         return mTextColor;
117     }
118 
119     /**
120      * Sets the background color of the freetext annotation.
121      *
122      * @param color The new background color.
123      */
setBackgroundColor(@olorInt int color)124     public void setBackgroundColor(@ColorInt int color) {
125         this.mBackgroundColor = color;
126     }
127 
128     /**
129      * Returns the background color of the freetext annotation.
130      *
131      * @return The background color.
132      */
getBackgroundColor()133     public @ColorInt int getBackgroundColor() {
134         return this.mBackgroundColor;
135     }
136 
137 }
138