Skip to content

Commit

Permalink
Get rid of legacy list box drawing
Browse files Browse the repository at this point in the history
DEVSIX-7517

Autoported commit.
Original commit hash: [a1e83efe1]
  • Loading branch information
AnhelinaM committed Sep 23, 2024
1 parent c072447 commit 87f48a9
Show file tree
Hide file tree
Showing 52 changed files with 244 additions and 463 deletions.
5 changes: 3 additions & 2 deletions itext.tests/itext.forms.tests/itext/forms/PdfFormFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ You should have received a copy of the GNU Affero General Public License
using iText.Kernel.Utils;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Logs;
using iText.Layout.Properties;
using iText.Test;
using iText.Test.Attributes;
Expand Down Expand Up @@ -978,10 +977,13 @@ public virtual void PdfWithDifferentFieldsTest() {
// list
PdfChoiceFormField f = new ChoiceFormFieldBuilder(pdfDoc, "combo").SetWidgetRectangle(new Rectangle(36, 556
, 50, 100)).SetOptions(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }).CreateList();
f.DisableFieldRegeneration();
f.SetValue("9", true);
f.SetValue("4");
f.SetTopIndex(2);
f.SetListSelected(new String[] { "3", "5" });
f.SetMultiSelect(true);
f.EnableFieldRegeneration();
form.AddField(f);
// push button
form.AddField(new PushButtonFormFieldBuilder(pdfDoc, "push button").SetWidgetRectangle(new Rectangle(36, 526
Expand Down Expand Up @@ -1139,7 +1141,6 @@ public virtual void SetFont2Ways() {

[NUnit.Framework.Test]
// Acrobat removes /NeedAppearances flag when document is opened and suggests to resave the document at once.
[LogMessage(LayoutLogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA)]
[LogMessage(FormsLogMessageConstants.INPUT_FIELD_DOES_NOT_FIT)]
public virtual void AppendModeAppearance() {
String inputFile = "appendModeAppearance.pdf";
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ public virtual void PdfA1DocWithPdfA1ComboBoxFieldTest() {

// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
[NUnit.Framework.Test]
[LogMessage(iText.IO.Logs.IoLogMessageConstant.MULTIPLE_VALUES_ON_A_NON_MULTISELECT_FIELD)]
public virtual void PdfA1DocWithPdfA1ListFieldTest() {
String name = "pdfA1DocWithPdfA1ListField";
String fileName = DESTINATION_FOLDER + name + ".pdf";
Expand All @@ -214,6 +213,7 @@ public virtual void PdfA1DocWithPdfA1ListFieldTest() {
f.SetValue("9").SetFont(fontFreeSans);
f.SetValue("4");
f.SetTopIndex(2);
f.SetMultiSelect(true);
f.SetListSelected(new String[] { "3", "5" });
form.AddField(f);
pdfDoc.Close();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
107 changes: 61 additions & 46 deletions itext/itext.forms/itext/forms/fields/PdfFormAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,14 @@ protected internal virtual void DrawListFormFieldAndSaveAppearance() {
if (rectangle == null) {
return;
}
bool multiselect = parent.GetFieldFlag(PdfChoiceFormField.FF_MULTI_SELECT);
if (!(formFieldElement is ListBoxField)) {
// Create it once and reset properties during each widget regeneration.
formFieldElement = new ListBoxField("", 0, parent.GetFieldFlag(PdfChoiceFormField.FF_MULTI_SELECT));
formFieldElement = new ListBoxField(parent.GetPartialFieldName().ToUnicodeString(), 0, multiselect);
}
formFieldElement.SetProperty(FormProperty.FORM_FIELD_MULTIPLE, parent.GetFieldFlag(PdfChoiceFormField.FF_MULTI_SELECT
));
formFieldElement.SetProperty(FormProperty.FORM_FIELD_MULTIPLE, multiselect);
((ListBoxField)formFieldElement).SetTopIndex(parent is PdfChoiceFormField && ((PdfChoiceFormField)parent).
GetTopIndex() != null ? ((PdfChoiceFormField)parent).GetTopIndex().IntValue() : 0);
PdfArray indices = GetParent().GetAsArray(PdfName.I);
PdfArray options = parent.GetOptions();
for (int index = 0; index < options.Size(); ++index) {
Expand All @@ -835,13 +837,25 @@ protected internal virtual void DrawListFormFieldAndSaveAppearance() {
bool selected = indices != null && indices.Contains(new PdfNumber(index));
SelectFieldItem existingItem = ((ListBoxField)formFieldElement).GetOption(exportValue);
if (existingItem == null) {
existingItem = new SelectFieldItem(exportValue, displayValue);
existingItem = displayValue == null ? new SelectFieldItem(exportValue) : new SelectFieldItem(exportValue,
displayValue);
((ListBoxField)formFieldElement).AddOption(existingItem);
}
existingItem.GetElement().SetProperty(Property.TEXT_ALIGNMENT, parent.GetJustification());
existingItem.GetElement().SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
existingItem.GetElement().SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
existingItem.GetElement().SetProperty(FormProperty.FORM_FIELD_SELECTED, selected);
// Workaround for com.itextpdf.forms.form.renderer.SelectFieldListBoxRenderer.applySelectedStyle:
// in HTML rendering mode we want to draw gray background for flattened fields and blue one for interactive,
// but here we temporarily flatten formFieldElement, so blue background property is explicitly set to
// the selected item. We also need to clear background property for not selected items in case field
// is regenerated with modified indices list.
if (selected && (multiselect || index == indices.GetAsNumber(indices.Size() - 1).IntValue())) {
existingItem.GetElement().SetProperty(Property.BACKGROUND, new Background(new DeviceRgb(169, 204, 225)));
}
else {
existingItem.GetElement().SetProperty(Property.BACKGROUND, null);
}
}
formFieldElement.SetProperty(Property.FONT, GetFont());
if (GetColor() != null) {
Expand Down Expand Up @@ -876,9 +890,18 @@ protected internal virtual void DrawTextFormFieldAndSaveAppearance() {
formFieldElement.SetProperty(Property.FONT_SIZE, UnitValue.CreatePointValue(GetFontSize()));
}
else {
formFieldElement.SetProperty(Property.FONT_SIZE, UnitValue.CreatePointValue(GetFontSize(new PdfArray(rectangle
), parent.GetValueAsString())));
float fontSize = GetFontSize(new PdfArray(rectangle), parent.GetValueAsString());
if (fontSize != 0) {
// We want to always draw the text using the given font size even if it's not fit into layout area.
// Without setting this property the height of the drawn field will be 0 which is unexpected.
formFieldElement.SetProperty(Property.FORCED_PLACEMENT, true);
}
formFieldElement.SetProperty(Property.FONT_SIZE, UnitValue.CreatePointValue(fontSize));
value = iText.Commons.Utils.StringUtil.ReplaceAll(value, LINE_ENDINGS_REGEXP, " ");
((InputField)formFieldElement).SetComb(this.IsCombTextFormField());
((InputField)formFieldElement).SetMaxLen((parent is PdfTextFormField ? (PdfTextFormField)parent : PdfFormCreator
.CreateTextFormField(parent.GetPdfObject())).GetMaxLen());
((InputField)formFieldElement).UseAsPassword(parent.IsPassword());
}
formFieldElement.SetValue(value);
formFieldElement.SetProperty(Property.FONT, GetFont());
Expand Down Expand Up @@ -913,7 +936,7 @@ protected internal virtual void DrawComboBoxAndSaveAppearance() {
return;
}
if (!(formFieldElement is ComboBoxField)) {
formFieldElement = new ComboBoxField("");
formFieldElement = new ComboBoxField(parent.GetPartialFieldName().ToUnicodeString());
}
ComboBoxField comboBoxField = (ComboBoxField)formFieldElement;
PrepareComboBoxFieldWithCorrectOptionsAndValues(comboBoxField);
Expand Down Expand Up @@ -1035,49 +1058,38 @@ internal virtual bool RegenerateWidget() {
}
PdfName type = parent.GetFormType();
RetrieveStyles();
if ((PdfName.Ch.Equals(type) && parent.GetFieldFlag(PdfChoiceFormField.FF_COMBO)) || this.IsCombTextFormField
()) {
if (parent.GetFieldFlag(PdfChoiceFormField.FF_COMBO) && formFieldElement != null) {
if (PdfName.Ch.Equals(type)) {
if (parent.GetFieldFlag(PdfChoiceFormField.FF_COMBO)) {
DrawComboBoxAndSaveAppearance();
return true;
}
return TextAndChoiceLegacyDrawer.RegenerateTextAndChoiceField(this);
DrawListFormFieldAndSaveAppearance();
return true;
}
else {
if (PdfName.Ch.Equals(type) && !parent.GetFieldFlag(PdfChoiceFormField.FF_COMBO)) {
if (formFieldElement != null) {
DrawListFormFieldAndSaveAppearance();
return true;
}
else {
return TextAndChoiceLegacyDrawer.RegenerateTextAndChoiceField(this);
}
if (PdfName.Tx.Equals(type)) {
DrawTextFormFieldAndSaveAppearance();
return true;
}
else {
if (PdfName.Tx.Equals(type)) {
DrawTextFormFieldAndSaveAppearance();
return true;
}
else {
if (PdfName.Btn.Equals(type)) {
if (parent.GetFieldFlag(PdfButtonFormField.FF_PUSH_BUTTON)) {
DrawPushButtonFieldAndSaveAppearance();
if (PdfName.Btn.Equals(type)) {
if (parent.GetFieldFlag(PdfButtonFormField.FF_PUSH_BUTTON)) {
DrawPushButtonFieldAndSaveAppearance();
}
else {
if (parent.GetFieldFlag(PdfButtonFormField.FF_RADIO)) {
DrawRadioButtonAndSaveAppearance(GetRadioButtonValue());
}
else {
if (parent.GetFieldFlag(PdfButtonFormField.FF_RADIO)) {
DrawRadioButtonAndSaveAppearance(GetRadioButtonValue());
}
else {
DrawCheckBoxAndSaveAppearance(GetCheckBoxValue());
}
DrawCheckBoxAndSaveAppearance(GetCheckBoxValue());
}
return true;
}
else {
if (PdfName.Sig.Equals(type)) {
DrawSignatureFormFieldAndSaveAppearance();
return true;
}
return true;
}
else {
if (PdfName.Sig.Equals(type)) {
DrawSignatureFormFieldAndSaveAppearance();
return true;
}
}
}
Expand Down Expand Up @@ -1166,14 +1178,17 @@ internal virtual float GetFontSize(PdfArray bBox, String value) {

private bool IsCombTextFormField() {
PdfName type = parent.GetFormType();
if (PdfName.Tx.Equals(type) && parent.GetFieldFlag(PdfTextFormField.FF_COMB)) {
int maxLen = PdfFormCreator.CreateTextFormField(parent.GetPdfObject()).GetMaxLen();
if (maxLen == 0 || parent.IsMultiline()) {
LOGGER.LogError(MessageFormatUtil.Format(iText.IO.Logs.IoLogMessageConstant.COMB_FLAG_MAY_BE_SET_ONLY_IF_MAXLEN_IS_PRESENT
));
return false;
if (PdfName.Tx.Equals(type)) {
PdfTextFormField textField = parent is PdfTextFormField ? (PdfTextFormField)parent : PdfFormCreator.CreateTextFormField
(parent.GetPdfObject());
if (textField.IsComb()) {
if (textField.GetMaxLen() == 0 || textField.IsMultiline() || textField.IsPassword() || textField.IsFileSelect
()) {
LOGGER.LogError(iText.IO.Logs.IoLogMessageConstant.COMB_FLAG_MAY_BE_SET_ONLY_IF_MAXLEN_IS_PRESENT);
return false;
}
return true;
}
return true;
}
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion itext/itext.forms/itext/forms/fields/PdfTextFormField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ public virtual iText.Forms.Fields.PdfTextFormField SetScroll(bool scroll) {
/// If true, the field is automatically divided into as many equally spaced positions,
/// or combs, as the value of MaxLen, and the text is laid out into those combs.
/// </remarks>
/// <returns>whether or not combing is enabled</returns>
/// <returns>
///
/// <see langword="true"/>
/// if combing is enabled,
/// <see langword="false"/>
/// otherwise
/// </returns>
public virtual bool IsComb() {
return GetFieldFlag(FF_COMB);
}
Expand Down
Loading

0 comments on commit 87f48a9

Please sign in to comment.