Skip to content

Commit 363b7ce

Browse files
committed
Create PdfStrings with Unicode encoding. Copy encoding while content cloning. Minor refactoring.
1 parent 93f7e78 commit 363b7ce

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

forms/src/main/java/com/itextpdf/forms/fields/PdfFormField.java

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ public PdfFormField(PdfDictionary pdfObject) {
259259

260260
/**
261261
* Creates a minimal {@link PdfFormField}.
262-
* @param pdfDocument The document
262+
*
263+
* @param pdfDocument The document
263264
*/
264265
protected PdfFormField(PdfDocument pdfDocument) {
265266
this((PdfDictionary) new PdfDictionary().makeIndirect(pdfDocument));
@@ -272,8 +273,8 @@ protected PdfFormField(PdfDocument pdfDocument) {
272273
/**
273274
* Creates a form field as a parent of a {@link PdfWidgetAnnotation}.
274275
*
275-
* @param widget The widget which will be a kid of the {@link PdfFormField}
276-
* @param pdfDocument The document
276+
* @param widget The widget which will be a kid of the {@link PdfFormField}
277+
* @param pdfDocument The document
277278
*/
278279
protected PdfFormField(PdfWidgetAnnotation widget, PdfDocument pdfDocument) {
279280
this((PdfDictionary) new PdfDictionary().makeIndirect(pdfDocument));
@@ -699,7 +700,7 @@ public static PdfChoiceFormField createChoice(PdfDocument doc, Rectangle rect, S
699700
field.put(PdfName.Opt, options);
700701
field.setFieldFlags(flags);
701702
field.setFieldName(name);
702-
field.getPdfObject().put(PdfName.V, new PdfString(value));
703+
field.getPdfObject().put(PdfName.V, new PdfString(value, PdfEncodings.UNICODE_BIG));
703704
if ((flags & PdfChoiceFormField.FF_COMBO) == 0) {
704705
value = field.optionsArrayToString(options);
705706
}
@@ -1771,7 +1772,7 @@ public PdfObject getRichText() {
17711772
* May be either {@link PdfStream} or {@link PdfString}.
17721773
*
17731774
* @param richText a new rich text value
1774-
* @return The edited PdfFormField
1775+
* @return The edited PdfFormField
17751776
*/
17761777
public PdfFormField setRichText(PdfObject richText) {
17771778
getPdfObject().put(PdfName.RV, richText);
@@ -1793,8 +1794,8 @@ public PdfFont getFont() {
17931794
* Note that the font will be added to the document so ensure that the font is embedded
17941795
* if it's a pdf/a document.
17951796
*
1796-
* @param font The new font to be set
1797-
* @return The edited PdfFormField
1797+
* @param font The new font to be set
1798+
* @return The edited PdfFormField
17981799
*/
17991800
public PdfFormField setFont(PdfFont font) {
18001801
this.font = font;
@@ -1806,8 +1807,8 @@ public PdfFormField setFont(PdfFont font) {
18061807
* Basic setter for the <code>fontSize</code> property. Regenerates the
18071808
* field appearance after setting the new value.
18081809
*
1809-
* @param fontSize The new font size to be set
1810-
* @return The edited PdfFormField
1810+
* @param fontSize The new font size to be set
1811+
* @return The edited PdfFormField
18111812
*/
18121813
public PdfFormField setFontSize(float fontSize) {
18131814
this.fontSize = fontSize;
@@ -1819,8 +1820,8 @@ public PdfFormField setFontSize(float fontSize) {
18191820
* Basic setter for the <code>fontSize</code> property. Regenerates the
18201821
* field appearance after setting the new value.
18211822
*
1822-
* @param fontSize The new font size to be set
1823-
* @return The edited PdfFormField
1823+
* @param fontSize The new font size to be set
1824+
* @return The edited PdfFormField
18241825
*/
18251826
public PdfFormField setFontSize(int fontSize) {
18261827
setFontSize((float) fontSize);
@@ -1831,9 +1832,9 @@ public PdfFormField setFontSize(int fontSize) {
18311832
* Combined setter for the <code>font</code> and <code>fontSize</code>
18321833
* properties. Regenerates the field appearance after setting the new value.
18331834
*
1834-
* @param font The new font to be set
1835-
* @param fontSize The new font size to be set
1836-
* @return The edited PdfFormField
1835+
* @param font The new font to be set
1836+
* @param fontSize The new font size to be set
1837+
* @return The edited PdfFormField
18371838
*/
18381839
public PdfFormField setFontAndSize(PdfFont font, int fontSize) {
18391840
this.font = font;
@@ -1846,8 +1847,8 @@ public PdfFormField setFontAndSize(PdfFont font, int fontSize) {
18461847
* Basic setter for the <code>backgroundColor</code> property. Regenerates
18471848
* the field appearance after setting the new value.
18481849
*
1849-
* @param backgroundColor The new color to be set
1850-
* @return The edited PdfFormField
1850+
* @param backgroundColor The new color to be set
1851+
* @return The edited PdfFormField
18511852
*/
18521853
public PdfFormField setBackgroundColor(Color backgroundColor) {
18531854
this.backgroundColor = backgroundColor;
@@ -1864,8 +1865,8 @@ public PdfFormField setBackgroundColor(Color backgroundColor) {
18641865
* Basic setter for the <code>degRotation</code> property. Regenerates
18651866
* the field appearance after setting the new value.
18661867
*
1867-
* @param degRotation The new degRotation to be set
1868-
* @return The edited PdfFormField
1868+
* @param degRotation The new degRotation to be set
1869+
* @return The edited PdfFormField
18691870
*/
18701871
public PdfFormField setRotation(int degRotation) {
18711872
if (degRotation % 90 != 0) {
@@ -1893,8 +1894,8 @@ public PdfFormField setRotation(int degRotation) {
18931894
/**
18941895
* Sets the action on all {@link PdfWidgetAnnotation widgets} of this form field.
18951896
*
1896-
* @param action The action
1897-
* @return The edited field
1897+
* @param action The action
1898+
* @return The edited field
18981899
*/
18991900
public PdfFormField setAction(PdfAction action) {
19001901
List<PdfWidgetAnnotation> widgets = getWidgets();
@@ -1912,7 +1913,7 @@ public PdfFormField setAction(PdfAction action) {
19121913
* {@link #regenerateField() regenerateField} method
19131914
*
19141915
* @param checkType the new checkbox marker
1915-
* @return The edited field
1916+
* @return The edited field
19161917
*/
19171918
public PdfFormField setCheckType(int checkType) {
19181919
if (checkType < TYPE_CHECK || checkType > TYPE_STAR) {
@@ -1936,7 +1937,7 @@ public PdfFormField setCheckType(int checkType) {
19361937
* Options are: HIDDEN, HIDDEN_BUT_PRINTABLE, VISIBLE, VISIBLE_BUT_DOES_NOT_PRINT
19371938
*
19381939
* @param visibility visibility option
1939-
* @return The edited field
1940+
* @return The edited field
19401941
*/
19411942
public PdfFormField setVisibility(int visibility) {
19421943
switch (visibility) {
@@ -2095,7 +2096,7 @@ public boolean regenerateField() {
20952096
PdfNumber topIndex = this.getPdfObject().getAsNumber(PdfName.TI);
20962097
PdfArray options = getOptions();
20972098
if (null != options) {
2098-
PdfArray visibleOptions = null != topIndex ? new PdfArray(options.subList(topIndex.intValue(), options.size()-1)) : (PdfArray) options.clone();
2099+
PdfArray visibleOptions = null != topIndex ? new PdfArray(options.subList(topIndex.intValue(), options.size() - 1)) : (PdfArray) options.clone();
20992100
value = optionsArrayToString(visibleOptions);
21002101
}
21012102
}
@@ -2348,8 +2349,8 @@ public float getBorderWidth() {
23482349
/**
23492350
* Sets the border width for the field.
23502351
*
2351-
* @param borderWidth The new border width.
2352-
* @return The edited field
2352+
* @param borderWidth The new border width.
2353+
* @return The edited field
23532354
*/
23542355
public PdfFormField setBorderWidth(float borderWidth) {
23552356
PdfDictionary bs = getWidgets().get(0).getBorderStyle();
@@ -2616,8 +2617,8 @@ protected Rectangle getRect(PdfDictionary field) {
26162617
protected static PdfArray processOptions(String[][] options) {
26172618
PdfArray array = new PdfArray();
26182619
for (String[] option : options) {
2619-
PdfArray subArray = new PdfArray(new PdfString(option[0]));
2620-
subArray.add(new PdfString(option[1]));
2620+
PdfArray subArray = new PdfArray(new PdfString(option[0], PdfEncodings.UNICODE_BIG));
2621+
subArray.add(new PdfString(option[1], PdfEncodings.UNICODE_BIG));
26212622
array.add(subArray);
26222623
}
26232624
return array;
@@ -2626,7 +2627,7 @@ protected static PdfArray processOptions(String[][] options) {
26262627
protected static PdfArray processOptions(String[] options) {
26272628
PdfArray array = new PdfArray();
26282629
for (String option : options) {
2629-
array.add(new PdfString(option));
2630+
array.add(new PdfString(option, PdfEncodings.UNICODE_BIG));
26302631
}
26312632
return array;
26322633
}
@@ -2761,11 +2762,11 @@ protected static Object[] splitDAelements(String da) {
27612762
/**
27622763
* Draws the visual appearance of text in a form field.
27632764
*
2764-
* @param rect The location on the page for the list field
2765-
* @param font a {@link PdfFont}
2766-
* @param fontSize The size of the font
2767-
* @param value The initial value
2768-
* @param appearance The appearance
2765+
* @param rect The location on the page for the list field
2766+
* @param font a {@link PdfFont}
2767+
* @param fontSize The size of the font
2768+
* @param value The initial value
2769+
* @param appearance The appearance
27692770
*/
27702771
protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize, String value, PdfFormXObject appearance) {
27712772
PdfStream stream = (PdfStream) new PdfStream().makeIndirect(getDocument());
@@ -2852,11 +2853,11 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
28522853
/**
28532854
* Draws the visual appearance of multiline text in a form field.
28542855
*
2855-
* @param rect The location on the page for the list field
2856-
* @param font a {@link PdfFont}
2857-
* @param fontSize The size of the font
2858-
* @param value The initial value
2859-
* @param appearance The appearance
2856+
* @param rect The location on the page for the list field
2857+
* @param font a {@link PdfFont}
2858+
* @param fontSize The size of the font
2859+
* @param value The initial value
2860+
* @param appearance The appearance
28602861
*/
28612862
protected void drawMultiLineTextAppearance(Rectangle rect, PdfFont font, float fontSize, String value, PdfFormXObject appearance) {
28622863
PdfStream stream = (PdfStream) new PdfStream().makeIndirect(getDocument());
@@ -2914,10 +2915,10 @@ protected void drawMultiLineTextAppearance(Rectangle rect, PdfFont font, float f
29142915
/**
29152916
* Draws a border using the borderWidth and borderColor of the form field.
29162917
*
2917-
* @param canvas The {@link PdfCanvas} on which to draw
2918-
* @param xObject The PdfFormXObject
2919-
* @param width The width of the rectangle to draw
2920-
* @param height The height of the rectangle to draw
2918+
* @param canvas The {@link PdfCanvas} on which to draw
2919+
* @param xObject The PdfFormXObject
2920+
* @param width The width of the rectangle to draw
2921+
* @param height The height of the rectangle to draw
29212922
*/
29222923
protected void drawBorder(PdfCanvas canvas, PdfFormXObject xObject, float width, float height) {
29232924
canvas.saveState();
@@ -3324,21 +3325,20 @@ private void applyRotation(PdfFormXObject xObject, float height, float width) {
33243325
}
33253326
}
33263327

3327-
private String optionsArrayToString(PdfArray options) {
3328-
String value = "";
3328+
private static String optionsArrayToString(PdfArray options) {
3329+
StringBuilder stringBuilder = new StringBuilder();
33293330
for (PdfObject obj : options) {
33303331
if (obj.isString()) {
3331-
value += ((PdfString) obj).toUnicodeString() + '\n';
3332+
stringBuilder.append(((PdfString) obj).toUnicodeString()).append('\n');
33323333
} else if (obj.isArray()) {
33333334
PdfObject element = ((PdfArray) obj).get(1);
33343335
if (element.isString()) {
3335-
value += ((PdfString) element).toUnicodeString() + '\n';
3336+
stringBuilder.append(((PdfString) element).toUnicodeString()).append('\n');
33363337
}
33373338
}
33383339
}
3339-
value = value.substring(0, value.length() - 1);
3340-
3341-
return value;
3340+
stringBuilder.deleteCharAt(stringBuilder.length() - 1); // last '\n'
3341+
return stringBuilder.toString();
33423342
}
33433343

33443344
private static double degreeToRadians(double angle) {

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfString.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,6 @@ protected void copyContent(PdfObject from, PdfDocument document) {
317317
decryption = string.decryption;
318318
decryptInfoNum = string.decryptInfoNum;
319319
decryptInfoGen = string.decryptInfoGen;
320+
encoding = string.encoding;
320321
}
321322
}

0 commit comments

Comments
 (0)