Skip to content

Commit 857a099

Browse files
pavel-alayiText-CI
authored andcommitted
Update appearance generating for push buttons
DEVSIX-2172 Autoported commit. Original commit hash: [59d94f4ae]
1 parent ff55835 commit 857a099

File tree

6 files changed

+141
-12
lines changed

6 files changed

+141
-12
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.IO;
3+
using iText.Forms;
4+
using iText.Forms.Fields;
5+
using iText.Kernel.Font;
6+
using iText.Kernel.Geom;
7+
using iText.Kernel.Pdf;
8+
using iText.Kernel.Utils;
9+
using iText.Test;
10+
11+
namespace iText.Pdfa {
12+
public class PdfAPushbuttonfieldTest : ExtendedITextTest {
13+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
14+
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/";
15+
16+
public static readonly String cmpFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
17+
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/cmp/PdfAPushbuttonfieldTest/";
18+
19+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
20+
+ "/test/itext/pdfa/PdfAPushbuttonfieldTest/";
21+
22+
[NUnit.Framework.OneTimeSetUp]
23+
public static void BeforeClass() {
24+
CreateDestinationFolder(destinationFolder);
25+
}
26+
27+
/// <exception cref="System.IO.IOException"/>
28+
/// <exception cref="System.Exception"/>
29+
[NUnit.Framework.Test]
30+
public virtual void PdfA1bButtonAppearanceTest() {
31+
String name = "pdfA1b_ButtonAppearanceTest";
32+
String outPath = destinationFolder + name + ".pdf";
33+
String cmpPath = cmpFolder + "cmp_" + name + ".pdf";
34+
String diff = "diff_" + name + "_";
35+
PdfWriter writer = new PdfWriter(outPath);
36+
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
37+
PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1"
38+
, @is);
39+
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
40+
doc.SetTagged();
41+
doc.GetCatalog().SetLang(new PdfString("en-US"));
42+
doc.AddNewPage();
43+
PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true);
44+
Rectangle rect = new Rectangle(36, 626, 100, 40);
45+
PdfFont font = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
46+
PdfFormField button = PdfFormField.CreatePushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel
47+
.PDF_A_1B);
48+
form.AddField(button);
49+
doc.Close();
50+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
51+
));
52+
}
53+
54+
/// <exception cref="System.IO.IOException"/>
55+
/// <exception cref="System.Exception"/>
56+
[NUnit.Framework.Test]
57+
public virtual void PdfA1bButtonAppearanceRegenerateTest() {
58+
String name = "pdfA1b_ButtonAppearanceRegenerateTest";
59+
String outPath = destinationFolder + name + ".pdf";
60+
String cmpPath = cmpFolder + "cmp_" + name + ".pdf";
61+
String diff = "diff_" + name + "_";
62+
PdfWriter writer = new PdfWriter(outPath);
63+
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
64+
PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1"
65+
, @is);
66+
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
67+
doc.SetTagged();
68+
doc.GetCatalog().SetLang(new PdfString("en-US"));
69+
doc.AddNewPage();
70+
PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true);
71+
Rectangle rect = new Rectangle(36, 626, 100, 40);
72+
PdfFont font = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
73+
PdfFormField button = PdfFormField.CreatePushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel
74+
.PDF_A_1B);
75+
button.RegenerateField();
76+
form.AddField(button);
77+
doc.Close();
78+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
79+
));
80+
}
81+
82+
/// <exception cref="System.IO.IOException"/>
83+
/// <exception cref="System.Exception"/>
84+
[NUnit.Framework.Test]
85+
public virtual void PdfA1bButtonAppearanceSetValueTest() {
86+
String name = "pdfA1b_ButtonAppearanceSetValueTest";
87+
String outPath = destinationFolder + name + ".pdf";
88+
String cmpPath = cmpFolder + "cmp_" + name + ".pdf";
89+
String diff = "diff_" + name + "_";
90+
PdfWriter writer = new PdfWriter(outPath);
91+
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
92+
PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1"
93+
, @is);
94+
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
95+
doc.SetTagged();
96+
doc.GetCatalog().SetLang(new PdfString("en-US"));
97+
doc.AddNewPage();
98+
PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true);
99+
Rectangle rect = new Rectangle(36, 626, 100, 40);
100+
PdfFont font = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true);
101+
PdfFormField button = PdfFormField.CreatePushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel
102+
.PDF_A_1B);
103+
button.SetValue("button");
104+
form.AddField(button);
105+
doc.Close();
106+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff
107+
));
108+
}
109+
}
110+
}

itext/itext.forms/itext/forms/fields/PdfFormField.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,9 @@ public static PdfButtonFormField CreatePushButton(PdfDocument doc, Rectangle rec
12641264
mk.Put(PdfName.CA, new PdfString(caption));
12651265
mk.Put(PdfName.BG, new PdfArray(field.backgroundColor.GetColorValue()));
12661266
annot.SetAppearanceCharacteristics(mk);
1267+
if (pdfAConformanceLevel != null) {
1268+
CreatePushButtonAppearanceState(annot.GetPdfObject());
1269+
}
12671270
return field;
12681271
}
12691272

@@ -2688,36 +2691,37 @@ public virtual bool RegenerateField() {
26882691
if ((ff & PdfButtonFormField.FF_PUSH_BUTTON) != 0) {
26892692
try {
26902693
value = text;
2694+
PdfDictionary widget = GetPdfObject();
26912695
PdfFormXObject appearance;
26922696
Rectangle rect = GetRect(GetPdfObject());
26932697
PdfDictionary apDic = GetPdfObject().GetAsDictionary(PdfName.AP);
26942698
if (apDic == null) {
26952699
IList<PdfWidgetAnnotation> widgets = GetWidgets();
26962700
if (widgets.Count == 1) {
2697-
apDic = widgets[0].GetPdfObject().GetAsDictionary(PdfName.AP);
2701+
widget = widgets[0].GetPdfObject();
2702+
apDic = widget.GetAsDictionary(PdfName.AP);
26982703
}
26992704
}
2705+
if (apDic == null) {
2706+
Put(PdfName.AP, apDic = new PdfDictionary());
2707+
widget = GetPdfObject();
2708+
}
27002709
if (img != null || form != null) {
27012710
appearance = DrawPushButtonAppearance(rect.GetWidth(), rect.GetHeight(), value, null, null, 0);
27022711
}
27032712
else {
2704-
PdfStream asNormal = null;
2705-
if (apDic != null) {
2706-
//TODO DEVSIX-2528 what is PdfName.N is PdfDictionary?
2707-
asNormal = apDic.GetAsStream(PdfName.N);
2708-
}
2709-
Object[] fontAndSize = GetFontAndSize(asNormal);
2713+
//TODO DEVSIX-2528 what if PdfName.N is PdfDictionary?
2714+
Object[] fontAndSize = GetFontAndSize(apDic.GetAsStream(PdfName.N));
27102715
PdfFont localFont = (PdfFont)fontAndSize[0];
27112716
PdfName localFontName = (PdfName)fontAndSize[2];
27122717
float fontSize = (float)fontAndSize[1];
27132718
appearance = DrawPushButtonAppearance(rect.GetWidth(), rect.GetHeight(), value, localFont, localFontName,
27142719
fontSize);
27152720
}
2716-
if (apDic == null) {
2717-
apDic = new PdfDictionary();
2718-
Put(PdfName.AP, apDic);
2719-
}
27202721
apDic.Put(PdfName.N, appearance.GetPdfObject());
2722+
if (pdfAConformanceLevel != null) {
2723+
CreatePushButtonAppearanceState(widget);
2724+
}
27212725
}
27222726
catch (System.IO.IOException e) {
27232727
throw new PdfException(e);
@@ -2783,6 +2787,21 @@ public virtual bool RegenerateField() {
27832787
return true;
27842788
}
27852789

2790+
private static void CreatePushButtonAppearanceState(PdfDictionary widget) {
2791+
PdfDictionary appearances = widget.GetAsDictionary(PdfName.AP);
2792+
PdfStream normalAppearanceStream = appearances.GetAsStream(PdfName.N);
2793+
if (normalAppearanceStream != null) {
2794+
PdfName stateName = widget.GetAsName(PdfName.AS);
2795+
if (stateName == null) {
2796+
stateName = new PdfName("push");
2797+
}
2798+
widget.Put(PdfName.AS, stateName);
2799+
PdfDictionary normalAppearance = new PdfDictionary();
2800+
normalAppearance.Put(stateName, normalAppearanceStream);
2801+
appearances.Put(PdfName.N, normalAppearance);
2802+
}
2803+
}
2804+
27862805
// TODO DEVSIX-2536
27872806
// Actually this entire method is a mess,
27882807
// because only radio group has FF_RADIO type and there is no RadioButton at all.

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
26409b5ce1656907942be1697522d0e1bf6db2fb
1+
59d94f4aefe2febfbc793a7b216b6a620e5c8227

0 commit comments

Comments
 (0)