Skip to content

Commit 9f0cfd2

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add tests for absolutely positioned elements added to Canvas
DEVSIX-4835 (cherry picked from commit 54b0b000fdffec55012fed10dc775ca1e362601f) Autoported commit. Original commit hash: [0784a4e78]
1 parent 6b14690 commit 9f0cfd2

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

itext.tests/itext.layout.tests/itext/layout/CanvasTest.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,69 @@ public virtual void CanvasWithPageEnableTaggingTest02() {
145145
pdf.Close();
146146
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
147147
}
148+
149+
[NUnit.Framework.Test]
150+
public virtual void ElementWithAbsolutePositioningInCanvasTest() {
151+
String testName = "elementWithAbsolutePositioningInCanvas";
152+
String @out = destinationFolder + testName + ".pdf";
153+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
154+
using (PdfDocument pdf = new PdfDocument(new PdfWriter(@out))) {
155+
pdf.AddNewPage();
156+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(pdf.GetFirstPage()), new Rectangle(120,
157+
650, 60, 80));
158+
Div notFittingDiv = new Div().SetWidth(100).Add(new Paragraph("Paragraph in Div with Not set position"));
159+
canvas.Add(notFittingDiv);
160+
Div divWithPosition = new Div().SetFixedPosition(120, 300, 80);
161+
divWithPosition.Add(new Paragraph("Paragraph in Div with set position"));
162+
canvas.Add(divWithPosition);
163+
canvas.Close();
164+
}
165+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
166+
}
167+
168+
[NUnit.Framework.Test]
169+
//TODO: DEVSIX-4820 (discuss the displaying of element with absolute position)
170+
[LogMessage(iText.IO.LogMessageConstant.CANVAS_ALREADY_FULL_ELEMENT_WILL_BE_SKIPPED)]
171+
public virtual void ParentElemWithAbsolPositionKidNotSuitCanvasTest() {
172+
String testName = "parentElemWithAbsolPositionKidNotSuitCanvas";
173+
String @out = destinationFolder + testName + ".pdf";
174+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
175+
using (PdfDocument pdf = new PdfDocument(new PdfWriter(@out))) {
176+
pdf.AddNewPage();
177+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(pdf.GetFirstPage()), new Rectangle(120,
178+
650, 55, 80));
179+
Div notFittingDiv = new Div().SetWidth(100).Add(new Paragraph("Paragraph in Div with Not set position"));
180+
canvas.Add(notFittingDiv);
181+
Div divWithPosition = new Div().SetFixedPosition(120, 300, 80);
182+
divWithPosition.Add(new Paragraph("Paragraph in Div with set position"));
183+
canvas.Add(divWithPosition);
184+
canvas.Close();
185+
}
186+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
187+
}
188+
189+
[NUnit.Framework.Test]
190+
public virtual void NestedElementWithAbsolutePositioningInCanvasTest() {
191+
NUnit.Framework.Assert.That(() => {
192+
//TODO: DEVSIX-4820 (NullPointerException on processing absolutely positioned elements in small canvas area)
193+
String testName = "nestedElementWithAbsolutePositioningInCanvas";
194+
String @out = destinationFolder + testName + ".pdf";
195+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
196+
using (PdfDocument pdf = new PdfDocument(new PdfWriter(@out))) {
197+
pdf.AddNewPage();
198+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(pdf.GetFirstPage()), new Rectangle(120,
199+
650, 55, 80));
200+
Div notFittingDiv = new Div().SetWidth(100).Add(new Paragraph("Paragraph in Div with Not set position"));
201+
Div divWithPosition = new Div().SetFixedPosition(50, 20, 80);
202+
divWithPosition.Add(new Paragraph("Paragraph in Div with set position"));
203+
notFittingDiv.Add(divWithPosition);
204+
canvas.Add(notFittingDiv);
205+
canvas.Close();
206+
}
207+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
208+
}
209+
, NUnit.Framework.Throws.InstanceOf<NullReferenceException>())
210+
;
211+
}
148212
}
149213
}

itext/itext.io/itext/io/LogMessageConstant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public sealed class LogMessageConstant {
8080

8181
public const String CANNOT_RESOLVE_ROLE_TOO_MUCH_TRANSITIVE_MAPPINGS = "Cannot resolve \"{0}\" role mapping to standard role, because of the too much transitive mappings.";
8282

83+
public const String CANVAS_ALREADY_FULL_ELEMENT_WILL_BE_SKIPPED = "Canvas is already full. Element will be skipped.";
84+
8385
public const String CHARACTER_DIRECTION_HAS_NOT_BEEN_DETECTED = "The direction for this character has not been detected: code point {0}. The Other Neutrals algorithm will be used.";
8486

8587
public const String CLIP_ELEMENT = "Element content was clipped because some height properties are set.";

itext/itext.layout/itext/layout/renderer/CanvasRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public CanvasRenderer(Canvas canvas, bool immediateFlush) {
8888

8989
public override void AddChild(IRenderer renderer) {
9090
if (true.Equals(GetPropertyAsBoolean(Property.FULL))) {
91-
LogManager.GetLogger(typeof(iText.Layout.Renderer.CanvasRenderer)).Warn("Canvas is already full. Element will be skipped."
91+
LogManager.GetLogger(typeof(iText.Layout.Renderer.CanvasRenderer)).Warn(iText.IO.LogMessageConstant.CANVAS_ALREADY_FULL_ELEMENT_WILL_BE_SKIPPED
9292
);
9393
}
9494
else {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
86693c07db09b5d3691d2cc47d65878a5006b6f5
1+
0784a4e78069c3c2d5201e89284f30e6fb6257d1

0 commit comments

Comments
 (0)