Skip to content

Commit 8608c60

Browse files
committed
Create PdfStrings with Unicode encoding. Copy encoding while content cloning. Minor refactoring.
1 parent aba8751 commit 8608c60

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public static PdfChoiceFormField createChoice(PdfDocument doc, Rectangle rect, S
629629
field.put(PdfName.Opt, options);
630630
field.setFieldFlags(flags);
631631
field.setFieldName(name);
632-
field.getPdfObject().put(PdfName.V, new PdfString(value));
632+
field.getPdfObject().put(PdfName.V, new PdfString(value, PdfEncodings.UNICODE_BIG));
633633
if ((flags & PdfChoiceFormField.FF_COMBO) == 0) {
634634
value = field.optionsArrayToString(options);
635635
}
@@ -1898,12 +1898,11 @@ public boolean regenerateField() {
18981898

18991899
} else {
19001900
if (!getFieldFlag(PdfChoiceFormField.FF_COMBO)) {
1901-
PdfNumber topIndex = ((PdfChoiceFormField) this).getTopIndex();
1902-
PdfArray options = (PdfArray) getOptions().clone();
1903-
if (topIndex != null) {
1904-
PdfObject object = options.get(topIndex.intValue());
1905-
options.remove(topIndex.intValue());
1906-
options.add(0, object);
1901+
PdfNumber topIndex = this.getPdfObject().getAsNumber(PdfName.TI);
1902+
PdfArray options = getOptions();
1903+
if (null != options) {
1904+
PdfArray visibleOptions = null != topIndex ? new PdfArray(options.subList(topIndex.intValue(), options.size() - 1)) : (PdfArray) options.clone();
1905+
value = optionsArrayToString(visibleOptions);
19071906
}
19081907
value = optionsArrayToString(options);
19091908
}
@@ -2424,8 +2423,8 @@ protected Rectangle getRect(PdfDictionary field) {
24242423
protected static PdfArray processOptions(String[][] options) {
24252424
PdfArray array = new PdfArray();
24262425
for (String[] option : options) {
2427-
PdfArray subArray = new PdfArray(new PdfString(option[0]));
2428-
subArray.add(new PdfString(option[1]));
2426+
PdfArray subArray = new PdfArray(new PdfString(option[0], PdfEncodings.UNICODE_BIG));
2427+
subArray.add(new PdfString(option[1], PdfEncodings.UNICODE_BIG));
24292428
array.add(subArray);
24302429
}
24312430
return array;
@@ -2434,7 +2433,7 @@ protected static PdfArray processOptions(String[][] options) {
24342433
protected static PdfArray processOptions(String[] options) {
24352434
PdfArray array = new PdfArray();
24362435
for (String option : options) {
2437-
array.add(new PdfString(option));
2436+
array.add(new PdfString(option, PdfEncodings.UNICODE_BIG));
24382437
}
24392438
return array;
24402439
}
@@ -3223,21 +3222,20 @@ private void applyRotation(PdfFormXObject xObject, float height, float width) {
32233222
}
32243223
}
32253224

3226-
private String optionsArrayToString(PdfArray options) {
3227-
String value = "";
3225+
private static String optionsArrayToString(PdfArray options) {
3226+
StringBuilder stringBuilder = new StringBuilder();
32283227
for (PdfObject obj : options) {
32293228
if (obj.isString()) {
3230-
value += ((PdfString) obj).toUnicodeString() + '\n';
3229+
stringBuilder.append(((PdfString) obj).toUnicodeString()).append('\n');
32313230
} else if (obj.isArray()) {
32323231
PdfObject element = ((PdfArray) obj).get(1);
32333232
if (element.isString()) {
3234-
value += ((PdfString) element).toUnicodeString() + '\n';
3233+
stringBuilder.append(((PdfString) element).toUnicodeString()).append('\n');
32353234
}
32363235
}
32373236
}
3238-
value = value.substring(0, value.length() - 1);
3239-
3240-
return value;
3237+
stringBuilder.deleteCharAt(stringBuilder.length() - 1); // last '\n'
3238+
return stringBuilder.toString();
32413239
}
32423240

32433241
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
@@ -403,5 +403,6 @@ protected void copyContent(PdfObject from, PdfDocument document) {
403403
decryption = string.decryption;
404404
decryptInfoNum = string.decryptInfoNum;
405405
decryptInfoGen = string.decryptInfoGen;
406+
encoding = string.encoding;
406407
}
407408
}

0 commit comments

Comments
 (0)