Skip to content

Commit 2f64f1b

Browse files
LodrKumquatiText-CI
authored andcommitted
Set color on font when drawing a TextField with MaxLen
Regenerate field when MaxLen status is changed DEVSIX-2751
1 parent 5f4b8e2 commit 2f64f1b

File tree

8 files changed

+63
-9
lines changed

8 files changed

+63
-9
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This file is part of the iText (R) project.
9090
import com.itextpdf.layout.property.Leading;
9191
import com.itextpdf.layout.property.Property;
9292
import com.itextpdf.layout.property.TextAlignment;
93+
import com.itextpdf.layout.property.TransparentColor;
9394
import com.itextpdf.layout.property.VerticalAlignment;
9495
import org.slf4j.Logger;
9596
import org.slf4j.LoggerFactory;
@@ -1486,7 +1487,12 @@ public boolean isPassword() {
14861487
* @return the edited field
14871488
*/
14881489
public PdfFormField setFieldFlags(int flags) {
1489-
return put(PdfName.Ff, new PdfNumber(flags));
1490+
int oldFlags = getFieldFlags();
1491+
put(PdfName.Ff, new PdfNumber(flags));
1492+
if (((oldFlags ^ flags) & PdfTextFormField.FF_COMB) != 0
1493+
&& PdfName.Tx.equals(getFormType()) && new PdfTextFormField(getPdfObject()).getMaxLen() != 0)
1494+
regenerateField();
1495+
return this;
14901496
}
14911497

14921498
/**
@@ -2865,6 +2871,8 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
28652871

28662872
Style paragraphStyle = new Style().setFont(font).setFontSize(fontSize);
28672873
paragraphStyle.setProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, 1));
2874+
if (color != null)
2875+
paragraphStyle.setProperty(Property.FONT_COLOR, new TransparentColor(color));
28682876

28692877
int maxLen = new PdfTextFormField(getPdfObject()).getMaxLen();
28702878
// check if /Comb has been set
@@ -2886,18 +2894,15 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
28862894
float startOffset = widthPerCharacter * (start + 0.5f);
28872895
for (int i = 0; i < numberOfCharacters; i++) {
28882896
modelCanvas.showTextAligned(new Paragraph(value.substring(i, i+1)).addStyle(paragraphStyle),
2889-
startOffset + widthPerCharacter * i, rect.getHeight() / 2, TextAlignment.CENTER, VerticalAlignment.MIDDLE);
2897+
startOffset + widthPerCharacter * i, rect.getHeight() / 2, TextAlignment.CENTER, VerticalAlignment.MIDDLE);
28902898
}
28912899
} else {
28922900
if (this.getFieldFlag(PdfTextFormField.FF_COMB)) {
28932901
Logger logger = LoggerFactory.getLogger(PdfFormField.class);
28942902
logger.error(MessageFormatUtil.format(LogMessageConstant.COMB_FLAG_MAY_BE_SET_ONLY_IF_MAXLEN_IS_PRESENT));
28952903
}
2896-
Paragraph paragraph = new Paragraph(value).addStyle(paragraphStyle).setPaddings(0, X_OFFSET, 0, X_OFFSET);
2897-
if (color != null) {
2898-
paragraph.setFontColor(color);
2899-
}
2900-
modelCanvas.showTextAligned(paragraph, x, rect.getHeight() / 2, textAlignment, VerticalAlignment.MIDDLE);
2904+
modelCanvas.showTextAligned(new Paragraph(value).addStyle(paragraphStyle).setPaddings(0, X_OFFSET, 0, X_OFFSET),
2905+
x, rect.getHeight() / 2, textAlignment, VerticalAlignment.MIDDLE);
29012906
}
29022907
canvas.
29032908
restoreState().

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,14 @@ public int getMaxLen() {
221221
}
222222

223223
/**
224-
* Sets the maximum length of the field?s text, in characters.
224+
* Sets the maximum length of the field's text, in characters.
225225
* @param maxLen the maximum text length
226226
* @return current
227227
*/
228228
public PdfTextFormField setMaxLen(int maxLen) {
229-
return (PdfTextFormField) put(PdfName.MaxLen, new PdfNumber(maxLen));
229+
put(PdfName.MaxLen, new PdfNumber(maxLen));
230+
if (getFieldFlag(FF_COMB))
231+
regenerateField();
232+
return this;
230233
}
231234
}

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,4 +841,50 @@ public void maxLenDeepInheritanceTest() throws IOException, InterruptedException
841841

842842
Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFilename, destinationFolder, "diff_"));
843843
}
844+
845+
@Test
846+
public void maxLenColoredTest() throws IOException, InterruptedException {
847+
String srcPdf = sourceFolder + "maxLenColoredTest.pdf";
848+
String outPdf = destinationFolder + "maxLenColoredTest.pdf";
849+
String cmpPdf = sourceFolder + "cmp_maxLenColoredTest.pdf";
850+
851+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(srcPdf), new PdfWriter(outPdf));
852+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, false);
853+
form.getField("magenta").setColor(ColorConstants.MAGENTA);
854+
855+
pdfDoc.close();
856+
857+
CompareTool compareTool = new CompareTool();
858+
String errorMessage = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
859+
if (errorMessage != null) {
860+
Assert.fail(errorMessage);
861+
}
862+
}
863+
864+
@Test
865+
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.COMB_FLAG_MAY_BE_SET_ONLY_IF_MAXLEN_IS_PRESENT, count = 2)})
866+
public void regenerateMaxLenCombTest() throws IOException, InterruptedException {
867+
String srcPdf = sourceFolder + "regenerateMaxLenCombTest.pdf";
868+
String outPdf = destinationFolder + "regenerateMaxLenCombTest.pdf";
869+
String cmpPdf = sourceFolder + "cmp_regenerateMaxLenCombTest.pdf";
870+
871+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(srcPdf), new PdfWriter(outPdf));
872+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
873+
874+
for (int i = 0; i < 12; i++) {
875+
PdfTextFormField field = (PdfTextFormField) form.getField("field " + i);
876+
if (i < 8)
877+
field.setMaxLen(i < 4 ? 7 : 0);
878+
if (i % 6 > 1)
879+
field.setFieldFlag(PdfTextFormField.FF_COMB, i % 2 == 0 ? true : false);
880+
881+
}
882+
pdfDoc.close();
883+
884+
CompareTool compareTool = new CompareTool();
885+
String errorMessage = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
886+
if (errorMessage != null) {
887+
Assert.fail(errorMessage);
888+
}
889+
}
844890
}

0 commit comments

Comments
 (0)