Skip to content

Commit 59d94f4

Browse files
committed
Update appearance generating for push buttons
DEVSIX-2172
1 parent 26409b5 commit 59d94f4

File tree

5 files changed

+149
-11
lines changed

5 files changed

+149
-11
lines changed

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,10 @@ public static PdfButtonFormField createPushButton(PdfDocument doc, Rectangle rec
873873
mk.put(PdfName.BG, new PdfArray(field.backgroundColor.getColorValue()));
874874
annot.setAppearanceCharacteristics(mk);
875875

876+
if (pdfAConformanceLevel != null) {
877+
createPushButtonAppearanceState(annot.getPdfObject());
878+
}
879+
876880
return field;
877881
}
878882

@@ -2069,36 +2073,38 @@ public boolean regenerateField() {
20692073
if ((ff & PdfButtonFormField.FF_PUSH_BUTTON) != 0) {
20702074
try {
20712075
value = text;
2076+
PdfDictionary widget = getPdfObject();
20722077
PdfFormXObject appearance;
20732078
Rectangle rect = getRect(getPdfObject());
20742079
PdfDictionary apDic = getPdfObject().getAsDictionary(PdfName.AP);
20752080
if (apDic == null) {
20762081
List<PdfWidgetAnnotation> widgets = getWidgets();
20772082
if (widgets.size() == 1) {
2078-
apDic = widgets.get(0).getPdfObject().getAsDictionary(PdfName.AP);
2083+
widget = widgets.get(0).getPdfObject();
2084+
apDic = widget.getAsDictionary(PdfName.AP);
20792085
}
20802086
}
2087+
if (apDic == null) {
2088+
put(PdfName.AP, apDic = new PdfDictionary());
2089+
widget = getPdfObject();
2090+
}
20812091
if (img != null || form != null) {
20822092
appearance = drawPushButtonAppearance(rect.getWidth(), rect.getHeight(), value, null, null, 0);
20832093
} else {
2084-
PdfStream asNormal = null;
2085-
if (apDic != null) {
2086-
//TODO DEVSIX-2528 what is PdfName.N is PdfDictionary?
2087-
asNormal = apDic.getAsStream(PdfName.N);
2088-
}
2089-
Object[] fontAndSize = getFontAndSize(asNormal);
2094+
//TODO DEVSIX-2528 what if PdfName.N is PdfDictionary?
2095+
Object[] fontAndSize = getFontAndSize(apDic.getAsStream(PdfName.N));
20902096
PdfFont localFont = (PdfFont) fontAndSize[0];
20912097
PdfName localFontName = (PdfName) fontAndSize[2];
20922098
float fontSize = (float) fontAndSize[1];
20932099
appearance = drawPushButtonAppearance(rect.getWidth(), rect.getHeight(), value,
20942100
localFont, localFontName, fontSize);
20952101
}
2102+
apDic.put(PdfName.N, appearance.getPdfObject());
20962103

2097-
if (apDic == null) {
2098-
apDic = new PdfDictionary();
2099-
put(PdfName.AP, apDic);
2104+
if (pdfAConformanceLevel != null) {
2105+
createPushButtonAppearanceState(widget);
21002106
}
2101-
apDic.put(PdfName.N, appearance.getPdfObject());
2107+
21022108
} catch (IOException e) {
21032109
throw new PdfException(e);
21042110
}
@@ -2152,6 +2158,21 @@ public boolean regenerateField() {
21522158
return true;
21532159
}
21542160

2161+
private static void createPushButtonAppearanceState(PdfDictionary widget) {
2162+
PdfDictionary appearances = widget.getAsDictionary(PdfName.AP);
2163+
PdfStream normalAppearanceStream = appearances.getAsStream(PdfName.N);
2164+
if (normalAppearanceStream != null) {
2165+
PdfName stateName = widget.getAsName(PdfName.AS);
2166+
if (stateName == null) {
2167+
stateName = new PdfName("push");
2168+
}
2169+
widget.put(PdfName.AS, stateName);
2170+
PdfDictionary normalAppearance = new PdfDictionary();
2171+
normalAppearance.put(stateName, normalAppearanceStream);
2172+
appearances.put(PdfName.N, normalAppearance);
2173+
}
2174+
}
2175+
21552176
// TODO DEVSIX-2536
21562177
// Actually this entire method is a mess,
21572178
// because only radio group has FF_RADIO type and there is no RadioButton at all.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.itextpdf.pdfa;
2+
3+
import com.itextpdf.forms.PdfAcroForm;
4+
import com.itextpdf.forms.fields.PdfButtonFormField;
5+
import com.itextpdf.forms.fields.PdfFormField;
6+
import com.itextpdf.kernel.colors.ColorConstants;
7+
import com.itextpdf.kernel.font.PdfFont;
8+
import com.itextpdf.kernel.font.PdfFontFactory;
9+
import com.itextpdf.kernel.geom.Rectangle;
10+
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
11+
import com.itextpdf.kernel.pdf.PdfOutputIntent;
12+
import com.itextpdf.kernel.pdf.PdfString;
13+
import com.itextpdf.kernel.pdf.PdfWriter;
14+
import com.itextpdf.kernel.utils.CompareTool;
15+
import com.itextpdf.test.ExtendedITextTest;
16+
import com.itextpdf.test.annotations.type.IntegrationTest;
17+
import org.junit.Assert;
18+
import org.junit.BeforeClass;
19+
import org.junit.Test;
20+
import org.junit.experimental.categories.Category;
21+
22+
import java.io.FileInputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
26+
@Category(IntegrationTest.class)
27+
public class PdfAPushbuttonfieldTest extends ExtendedITextTest {
28+
29+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/pdfa/";
30+
public static final String cmpFolder = "./src/test/resources/com/itextpdf/pdfa/cmp/PdfAPushbuttonfieldTest/";
31+
public static final String destinationFolder = "./target/test/com/itextpdf/pdfa/PdfAPushbuttonfieldTest/";
32+
33+
@BeforeClass
34+
public static void beforeClass() {
35+
createDestinationFolder(destinationFolder);
36+
}
37+
38+
@Test
39+
public void pdfA1bButtonAppearanceTest() throws IOException, InterruptedException {
40+
String name = "pdfA1b_ButtonAppearanceTest";
41+
String outPath = destinationFolder + name + ".pdf";
42+
String cmpPath = cmpFolder + "cmp_" + name + ".pdf";
43+
String diff = "diff_" + name + "_";
44+
45+
PdfWriter writer = new PdfWriter(outPath);
46+
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
47+
PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is);
48+
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
49+
doc.setTagged();
50+
doc.getCatalog().setLang(new PdfString("en-US"));
51+
doc.addNewPage();
52+
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
53+
54+
Rectangle rect = new Rectangle(36, 626, 100, 40);
55+
PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
56+
PdfFormField button = PdfFormField.createPushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel.PDF_A_1B);
57+
form.addField(button);
58+
59+
doc.close();
60+
61+
Assert.assertNull(new CompareTool().compareByContent(outPath, cmpPath, destinationFolder, diff));
62+
}
63+
64+
@Test
65+
public void pdfA1bButtonAppearanceRegenerateTest() throws IOException, InterruptedException {
66+
String name = "pdfA1b_ButtonAppearanceRegenerateTest";
67+
String outPath = destinationFolder + name + ".pdf";
68+
String cmpPath = cmpFolder + "cmp_" + name + ".pdf";
69+
String diff = "diff_" + name + "_";
70+
71+
PdfWriter writer = new PdfWriter(outPath);
72+
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
73+
PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is);
74+
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
75+
doc.setTagged();
76+
doc.getCatalog().setLang(new PdfString("en-US"));
77+
doc.addNewPage();
78+
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
79+
80+
Rectangle rect = new Rectangle(36, 626, 100, 40);
81+
PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
82+
PdfFormField button = PdfFormField.createPushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel.PDF_A_1B);
83+
button.regenerateField();
84+
form.addField(button);
85+
86+
doc.close();
87+
88+
Assert.assertNull(new CompareTool().compareByContent(outPath, cmpPath, destinationFolder, diff));
89+
}
90+
91+
@Test
92+
public void pdfA1bButtonAppearanceSetValueTest() throws IOException, InterruptedException {
93+
String name = "pdfA1b_ButtonAppearanceSetValueTest";
94+
String outPath = destinationFolder + name + ".pdf";
95+
String cmpPath = cmpFolder + "cmp_" + name + ".pdf";
96+
String diff = "diff_" + name + "_";
97+
98+
PdfWriter writer = new PdfWriter(outPath);
99+
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
100+
PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is);
101+
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
102+
doc.setTagged();
103+
doc.getCatalog().setLang(new PdfString("en-US"));
104+
doc.addNewPage();
105+
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
106+
107+
Rectangle rect = new Rectangle(36, 626, 100, 40);
108+
PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
109+
PdfFormField button = PdfFormField.createPushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel.PDF_A_1B);
110+
button.setValue("button");
111+
form.addField(button);
112+
113+
doc.close();
114+
115+
Assert.assertNull(new CompareTool().compareByContent(outPath, cmpPath, destinationFolder, diff));
116+
}
117+
}

0 commit comments

Comments
 (0)