|
| 1 | +package com.itextpdf.layout; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.geom.Rectangle; |
| 4 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 5 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 6 | +import com.itextpdf.kernel.utils.CompareTool; |
| 7 | +import com.itextpdf.layout.element.Cell; |
| 8 | +import com.itextpdf.layout.element.ListItem; |
| 9 | +import com.itextpdf.layout.element.Table; |
| 10 | +import com.itextpdf.layout.layout.LayoutArea; |
| 11 | +import com.itextpdf.layout.layout.LayoutResult; |
| 12 | +import com.itextpdf.layout.layout.RootLayoutArea; |
| 13 | +import com.itextpdf.layout.renderer.DocumentRenderer; |
| 14 | +import com.itextpdf.test.ExtendedITextTest; |
| 15 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.experimental.categories.Category; |
| 22 | + |
| 23 | +@Category(IntegrationTest.class) |
| 24 | +public class CustomCurrentAreaTest extends ExtendedITextTest { |
| 25 | + public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/CustomCurrentAreaTest/"; |
| 26 | + public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/CustomCurrentAreaTest/"; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void beforeClass() { |
| 30 | + createDestinationFolder(DESTINATION_FOLDER); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void longListItemTest() throws IOException, InterruptedException { |
| 35 | + String outFileName = DESTINATION_FOLDER + "longListItemTest.pdf"; |
| 36 | + String cmpFileName = SOURCE_FOLDER + "cmp_longListItemTest.pdf"; |
| 37 | + Rectangle customArea = new Rectangle(0, 15, 586, 723); |
| 38 | + try(PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));Document document = new Document(pdf)) { |
| 39 | + ClauseRenderer renderer = new ClauseRenderer(document, customArea); |
| 40 | + document.setRenderer(renderer); |
| 41 | + |
| 42 | + com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List(); |
| 43 | + list.setListSymbol("1."); |
| 44 | + list.add(new ListItem( |
| 45 | + "It is a long established fact that a reader will be distracted by the readable content of a page" |
| 46 | + + " when looking at its layout.")); |
| 47 | + document.add(new Table(1).addCell(new Cell().add(list))); |
| 48 | + } |
| 49 | + Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, DESTINATION_FOLDER, "diff")); |
| 50 | + } |
| 51 | + |
| 52 | + private static class ClauseRenderer extends DocumentRenderer { |
| 53 | + protected Rectangle column; |
| 54 | + |
| 55 | + public ClauseRenderer(Document document, Rectangle rect) { |
| 56 | + super(document, false); |
| 57 | + this.column = rect; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected LayoutArea updateCurrentArea(LayoutResult overflowResult) { |
| 62 | + super.updateCurrentArea(overflowResult); |
| 63 | + return (currentArea = new RootLayoutArea(currentArea.getPageNumber(), column.clone())); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments