Skip to content

Commit 0784a4e

Browse files
author
Kate Ivanova
committed
Add tests for absolutely positioned elements added to Canvas
DEVSIX-4835 (cherry picked from commit 54b0b000fdffec55012fed10dc775ca1e362601f)
1 parent 86693c0 commit 0784a4e

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public final class LogMessageConstant {
6565
public static final String CANNOT_REPLACE_FINISHED_HINT = "Layout tagging hints modification failed: cannot replace a kid hint that is already marked as finished.";
6666
public static final String CANNOT_RESOLVE_ROLE_IN_NAMESPACE_TOO_MUCH_TRANSITIVE_MAPPINGS = "Cannot resolve \"{0}\" role in {1} namespace mapping to standard role, because of the too much transitive mappings.";
6767
public static final String CANNOT_RESOLVE_ROLE_TOO_MUCH_TRANSITIVE_MAPPINGS = "Cannot resolve \"{0}\" role mapping to standard role, because of the too much transitive mappings.";
68+
public static final String CANVAS_ALREADY_FULL_ELEMENT_WILL_BE_SKIPPED = "Canvas is already full. Element will be skipped.";
6869
public static final 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.";
6970
public static final String CLIP_ELEMENT = "Element content was clipped because some height properties are set.";
7071
public static final String COLLECTION_DICTIONARY_ALREADY_EXISTS_IT_WILL_BE_MODIFIED = "Collection dictionary already exists. It will be modified.";

layout/src/main/java/com/itextpdf/layout/renderer/CanvasRenderer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.layout.renderer;
4545

46+
import com.itextpdf.io.LogMessageConstant;
4647
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
4748
import com.itextpdf.layout.Canvas;
4849
import com.itextpdf.layout.layout.LayoutArea;
@@ -83,7 +84,8 @@ public CanvasRenderer(Canvas canvas, boolean immediateFlush) {
8384
@Override
8485
public void addChild(IRenderer renderer) {
8586
if (Boolean.TRUE.equals(getPropertyAsBoolean(Property.FULL))) {
86-
LoggerFactory.getLogger(CanvasRenderer.class).warn("Canvas is already full. Element will be skipped.");
87+
LoggerFactory.getLogger(CanvasRenderer.class).warn(
88+
LogMessageConstant.CANVAS_ALREADY_FULL_ELEMENT_WILL_BE_SKIPPED);
8789
} else {
8890
super.addChild(renderer);
8991
}

layout/src/test/java/com/itextpdf/layout/CanvasTest.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This file is part of the iText (R) project.
5151
import com.itextpdf.kernel.pdf.action.PdfAction;
5252
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
5353
import com.itextpdf.kernel.utils.CompareTool;
54+
import com.itextpdf.layout.element.Div;
5455
import com.itextpdf.layout.element.Link;
5556
import com.itextpdf.layout.element.Paragraph;
5657
import com.itextpdf.test.ExtendedITextTest;
@@ -61,8 +62,10 @@ This file is part of the iText (R) project.
6162
import java.io.IOException;
6263
import org.junit.Assert;
6364
import org.junit.BeforeClass;
65+
import org.junit.Rule;
6466
import org.junit.Test;
6567
import org.junit.experimental.categories.Category;
68+
import org.junit.rules.ExpectedException;
6669

6770
@Category(IntegrationTest.class)
6871
public class CanvasTest extends ExtendedITextTest {
@@ -75,6 +78,9 @@ public static void beforeClass() {
7578
createOrClearDestinationFolder(destinationFolder);
7679
}
7780

81+
@Rule
82+
public ExpectedException junitExpectedException = ExpectedException.none();
83+
7884
@Test
7985
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_APPLY_PAGE_DEPENDENT_PROP_UNKNOWN_PAGE_ON_WHICH_ELEMENT_IS_DRAWN))
8086
public void canvasNoPageLinkTest() throws IOException, InterruptedException {
@@ -192,4 +198,85 @@ public void canvasWithPageEnableTaggingTest02() throws IOException, InterruptedE
192198

193199
Assert.assertNull(new CompareTool().compareByContent(out, cmp, destinationFolder));
194200
}
201+
202+
@Test
203+
public void elementWithAbsolutePositioningInCanvasTest() throws IOException, InterruptedException {
204+
String testName = "elementWithAbsolutePositioningInCanvas";
205+
String out = destinationFolder + testName + ".pdf";
206+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
207+
208+
try (PdfDocument pdf = new PdfDocument(new PdfWriter(out))) {
209+
pdf.addNewPage();
210+
Canvas canvas = new Canvas(new PdfCanvas(pdf.getFirstPage()),
211+
new Rectangle(120, 650, 60, 80));
212+
213+
Div notFittingDiv = new Div().setWidth(100)
214+
.add(new Paragraph("Paragraph in Div with Not set position"));
215+
canvas.add(notFittingDiv);
216+
217+
Div divWithPosition = new Div().setFixedPosition(120, 300, 80);
218+
divWithPosition.add(new Paragraph("Paragraph in Div with set position"));
219+
canvas.add(divWithPosition);
220+
221+
canvas.close();
222+
}
223+
224+
Assert.assertNull(new CompareTool().compareByContent(out, cmp, destinationFolder));
225+
}
226+
227+
@Test
228+
//TODO: DEVSIX-4820 (discuss the displaying of element with absolute position)
229+
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.CANVAS_ALREADY_FULL_ELEMENT_WILL_BE_SKIPPED)})
230+
public void parentElemWithAbsolPositionKidNotSuitCanvasTest() throws IOException, InterruptedException {
231+
String testName = "parentElemWithAbsolPositionKidNotSuitCanvas";
232+
String out = destinationFolder + testName + ".pdf";
233+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
234+
235+
try (PdfDocument pdf = new PdfDocument(new PdfWriter(out))) {
236+
pdf.addNewPage();
237+
238+
Canvas canvas = new Canvas(new PdfCanvas(pdf.getFirstPage()),
239+
new Rectangle(120, 650, 55, 80));
240+
241+
Div notFittingDiv = new Div().setWidth(100).add(new Paragraph("Paragraph in Div with Not set position"));
242+
canvas.add(notFittingDiv);
243+
244+
Div divWithPosition = new Div().setFixedPosition(120, 300, 80);
245+
divWithPosition.add(new Paragraph("Paragraph in Div with set position"));
246+
canvas.add(divWithPosition);
247+
248+
canvas.close();
249+
}
250+
251+
Assert.assertNull(new CompareTool().compareByContent(out, cmp, destinationFolder));
252+
}
253+
254+
@Test
255+
//TODO: DEVSIX-4820 (NullPointerException on processing absolutely positioned elements in small canvas area)
256+
public void nestedElementWithAbsolutePositioningInCanvasTest() throws IOException, InterruptedException {
257+
junitExpectedException.expect(NullPointerException.class);
258+
259+
String testName = "nestedElementWithAbsolutePositioningInCanvas";
260+
String out = destinationFolder + testName + ".pdf";
261+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
262+
263+
try (PdfDocument pdf = new PdfDocument(new PdfWriter(out))) {
264+
pdf.addNewPage();
265+
266+
Canvas canvas = new Canvas(new PdfCanvas(pdf.getFirstPage()),
267+
new Rectangle(120, 650, 55, 80));
268+
269+
Div notFittingDiv = new Div().setWidth(100).add(new Paragraph("Paragraph in Div with Not set position"));
270+
271+
Div divWithPosition = new Div().setFixedPosition(50, 20, 80);
272+
divWithPosition.add(new Paragraph("Paragraph in Div with set position"));
273+
274+
notFittingDiv.add(divWithPosition);
275+
canvas.add(notFittingDiv);
276+
277+
canvas.close();
278+
}
279+
280+
Assert.assertNull(new CompareTool().compareByContent(out, cmp, destinationFolder));
281+
}
195282
}

0 commit comments

Comments
 (0)