Skip to content

Commit 9537a79

Browse files
Eugene BochiloiText-CI
authored andcommitted
Introduce way to pass MetaInfo to forms module
DEVSIX-6328 Autoported commit. Original commit hash: [026a00424]
1 parent 55cc486 commit 9537a79

File tree

9 files changed

+120
-38
lines changed

9 files changed

+120
-38
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.IO;
3+
using iText.Html2pdf.Attach.Impl.Layout;
4+
using iText.Html2pdf.Attach.Impl.Layout.Form.Element;
5+
using iText.Kernel.Colors;
6+
using iText.Kernel.Pdf;
7+
using iText.Kernel.Utils;
8+
using iText.Layout;
9+
using iText.Layout.Element;
10+
using iText.Layout.Properties;
11+
using iText.Test;
12+
13+
namespace iText.Html2pdf.Attach.Impl.Layout.Form.Renderer {
14+
public class ButtonColorTest : ExtendedITextTest {
15+
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
16+
.CurrentContext.TestDirectory) + "/resources/itext/html2pdf/attach/impl/layout/form/renderer/ButtonColorTest/";
17+
18+
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
19+
+ "/test/itext/html2pdf/attach/impl/layout/form/renderer/ButtonColorTest/";
20+
21+
[NUnit.Framework.OneTimeSetUp]
22+
public static void BeforeClass() {
23+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
24+
}
25+
26+
[NUnit.Framework.Test]
27+
public virtual void ButtonsWithColorTest() {
28+
String outPdf = DESTINATION_FOLDER + "buttonsWithColor.pdf";
29+
String cmpPdf = SOURCE_FOLDER + "cmp_buttonsWithColor.pdf";
30+
DrawButtons(outPdf, cmpPdf, ColorConstants.RED);
31+
}
32+
33+
[NUnit.Framework.Test]
34+
public virtual void ButtonsWithoutColorTest() {
35+
String outPdf = DESTINATION_FOLDER + "buttonsWithoutColor.pdf";
36+
String cmpPdf = SOURCE_FOLDER + "cmp_buttonsWithoutColor.pdf";
37+
DrawButtons(outPdf, cmpPdf, null);
38+
}
39+
40+
private static void DrawButtons(String outPdf, String cmpPdf, Color color) {
41+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outPdf, FileMode.Create)))) {
42+
using (Document document = new Document(pdfDocument)) {
43+
Button button = new Button("button");
44+
button.Add(new Paragraph("button child"));
45+
InputButton inputButton = new InputButton("input button");
46+
button.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, false);
47+
inputButton.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, false);
48+
button.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, "button value");
49+
inputButton.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, "input button value");
50+
button.SetProperty(Property.FONT_COLOR, color == null ? null : new TransparentColor(color));
51+
inputButton.SetProperty(Property.BACKGROUND, color == null ? null : new Background(color));
52+
document.Add(button);
53+
document.Add(inputButton);
54+
}
55+
}
56+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER));
57+
}
58+
}
59+
}

itext/itext.html2pdf/itext/html2pdf/attach/impl/layout/form/renderer/AbstractFormFieldRenderer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ protected internal virtual void WriteAcroFormFieldLangAttribute(PdfDocument pdfD
233233
}
234234
}
235235

236+
internal virtual MetaInfoContainer GetMetaInfo() {
237+
return this.GetProperty<MetaInfoContainer>(Property.META_INFO);
238+
}
239+
236240
private void ProcessLangAttribute() {
237241
IPropertyContainer propertyContainer = flatRenderer.GetModelElement();
238242
if (propertyContainer is IAccessibleElement) {

itext/itext.html2pdf/itext/html2pdf/attach/impl/layout/form/renderer/ButtonRenderer.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ source product.
4646
using iText.Forms.Fields;
4747
using iText.Html2pdf.Attach.Impl.Layout;
4848
using iText.Html2pdf.Attach.Impl.Layout.Form.Element;
49+
using iText.Kernel.Colors;
50+
using iText.Kernel.Font;
4951
using iText.Kernel.Geom;
5052
using iText.Kernel.Pdf;
5153
using iText.Kernel.Pdf.Annot;
@@ -80,20 +82,26 @@ public override void Draw(DrawContext drawContext) {
8082
Rectangle area = GetOccupiedArea().GetBBox().Clone();
8183
ApplyMargins(area, false);
8284
PdfPage page = doc.GetPage(occupiedArea.GetPageNumber());
83-
PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, doc.GetDefaultFont(), fontSize
84-
.GetValue());
85-
button.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_NONE);
86-
button.SetBorderWidth(0);
87-
button.SetBackgroundColor(null);
88-
TransparentColor color = GetPropertyAsTransparentColor(Property.FONT_COLOR);
89-
if (color != null) {
90-
button.SetColor(color.GetColor());
91-
}
92-
PdfAcroForm forms = PdfAcroForm.GetAcroForm(doc, true);
93-
//Add fields only if it isn't already added. This can happen on split.
94-
if (forms.GetField(name) == null) {
95-
forms.AddField(button, page);
85+
TransparentColor transparentColor = GetPropertyAsTransparentColor(Property.FONT_COLOR);
86+
Color color = transparentColor == null ? null : transparentColor.GetColor();
87+
float fontSizeValue = fontSize.GetValue();
88+
PdfFont font = doc.GetDefaultFont();
89+
FormsMetaInfoStaticContainer.UseMetaInfoDuringTheAction(this.GetProperty<MetaInfoContainer>(Property.META_INFO
90+
), () => {
91+
PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, font, fontSizeValue);
92+
button.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_NONE);
93+
button.SetBorderWidth(0);
94+
button.SetBackgroundColor(null);
95+
if (color != null) {
96+
button.SetColor(color);
97+
}
98+
PdfAcroForm forms = PdfAcroForm.GetAcroForm(doc, true);
99+
//Add fields only if it isn't already added. This can happen on split.
100+
if (forms.GetField(name) == null) {
101+
forms.AddField(button, page);
102+
}
96103
}
104+
);
97105
if (doc.IsTagged()) {
98106
TagTreePointer formParentPointer = doc.GetTagStructureContext().GetAutoTaggingPointer();
99107
IList<String> kidsRoles = formParentPointer.GetKidsRoles();

itext/itext.html2pdf/itext/html2pdf/attach/impl/layout/form/renderer/InputButtonRenderer.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ source product.
5050
using iText.Html2pdf.Attach.Impl.Layout;
5151
using iText.Html2pdf.Attach.Impl.Layout.Form.Element;
5252
using iText.Html2pdf.Logs;
53+
using iText.Kernel.Colors;
5354
using iText.Kernel.Geom;
5455
using iText.Kernel.Pdf;
5556
using iText.Layout.Layout;
@@ -134,14 +135,18 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
134135
Rectangle area = flatRenderer.GetOccupiedArea().GetBBox().Clone();
135136
ApplyPaddings(area, true);
136137
PdfPage page = doc.GetPage(occupiedArea.GetPageNumber());
137-
PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, font, fontSize.GetValue(
138-
));
139138
Background background = this.GetProperty<Background>(Property.BACKGROUND);
140-
if (background != null && background.GetColor() != null) {
141-
button.SetBackgroundColor(background.GetColor());
139+
Color backgroundColor = background == null ? null : background.GetColor();
140+
float fontSizeValue = fontSize.GetValue();
141+
FormsMetaInfoStaticContainer.UseMetaInfoDuringTheAction(GetMetaInfo(), () => {
142+
PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, font, fontSizeValue);
143+
if (backgroundColor != null) {
144+
button.SetBackgroundColor(backgroundColor);
145+
}
146+
ApplyDefaultFieldProperties(button);
147+
PdfAcroForm.GetAcroForm(doc, true).AddField(button, page);
142148
}
143-
ApplyDefaultFieldProperties(button);
144-
PdfAcroForm.GetAcroForm(doc, true).AddField(button, page);
149+
);
145150
WriteAcroFormFieldLangAttribute(doc);
146151
}
147152

itext/itext.html2pdf/itext/html2pdf/attach/impl/layout/form/renderer/InputFieldRenderer.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ protected internal override IRenderer CreateFlatRenderer() {
145145
*/
146146
protected internal override void ApplyAcroField(DrawContext drawContext) {
147147
font.SetSubset(false);
148-
String value = GetDefaultValue();
148+
bool password = IsPassword();
149+
String value = password ? "" : GetDefaultValue();
149150
String name = GetModelId();
150151
UnitValue fontSize = (UnitValue)this.GetPropertyAsUnitValue(Property.FONT_SIZE);
151152
if (!fontSize.IsPointValue()) {
@@ -157,19 +158,19 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
157158
PdfDocument doc = drawContext.GetDocument();
158159
Rectangle area = flatRenderer.GetOccupiedArea().GetBBox().Clone();
159160
PdfPage page = doc.GetPage(occupiedArea.GetPageNumber());
160-
bool password = IsPassword();
161-
if (password) {
162-
value = "";
163-
}
164-
PdfFormField inputField = PdfFormField.CreateText(doc, area, name, value, font, fontSize.GetValue());
165-
if (password) {
166-
inputField.SetFieldFlag(PdfFormField.FF_PASSWORD, true);
167-
}
168-
else {
169-
inputField.SetDefaultValue(new PdfString(value));
161+
float fontSizeValue = fontSize.GetValue();
162+
FormsMetaInfoStaticContainer.UseMetaInfoDuringTheAction(GetMetaInfo(), () => {
163+
PdfFormField inputField = PdfFormField.CreateText(doc, area, name, value, font, fontSizeValue);
164+
if (password) {
165+
inputField.SetFieldFlag(PdfFormField.FF_PASSWORD, true);
166+
}
167+
else {
168+
inputField.SetDefaultValue(new PdfString(value));
169+
}
170+
ApplyDefaultFieldProperties(inputField);
171+
PdfAcroForm.GetAcroForm(doc, true).AddField(inputField, page);
170172
}
171-
ApplyDefaultFieldProperties(inputField);
172-
PdfAcroForm.GetAcroForm(doc, true).AddField(inputField, page);
173+
);
173174
WriteAcroFormFieldLangAttribute(doc);
174175
}
175176

itext/itext.html2pdf/itext/html2pdf/attach/impl/layout/form/renderer/TextAreaRenderer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,16 @@ protected internal override void ApplyAcroField(DrawContext drawContext) {
161161
PdfDocument doc = drawContext.GetDocument();
162162
Rectangle area = flatRenderer.GetOccupiedArea().GetBBox().Clone();
163163
PdfPage page = doc.GetPage(occupiedArea.GetPageNumber());
164-
PdfFormField inputField = PdfFormField.CreateText(doc, area, name, value, font, fontSize.GetValue());
165-
inputField.SetFieldFlag(PdfFormField.FF_MULTILINE, true);
166-
inputField.SetDefaultValue(new PdfString(GetDefaultValue()));
167-
ApplyDefaultFieldProperties(inputField);
168-
PdfAcroForm.GetAcroForm(doc, true).AddField(inputField, page);
164+
float fontSizeValue = fontSize.GetValue();
165+
PdfString defaultValue = new PdfString(GetDefaultValue());
166+
FormsMetaInfoStaticContainer.UseMetaInfoDuringTheAction(GetMetaInfo(), () => {
167+
PdfFormField inputField = PdfFormField.CreateText(doc, area, name, value, font, fontSizeValue);
168+
inputField.SetFieldFlag(PdfFormField.FF_MULTILINE, true);
169+
inputField.SetDefaultValue(defaultValue);
170+
ApplyDefaultFieldProperties(inputField);
171+
PdfAcroForm.GetAcroForm(doc, true).AddField(inputField, page);
172+
}
173+
);
169174
WriteAcroFormFieldLangAttribute(doc);
170175
}
171176

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4d9f46dfe52855997ab2eccd21e5bdaba021e7fc
1+
026a00424b9ca71b2e0ad867f7ae7131ddd07a84

0 commit comments

Comments
 (0)