@@ -25,6 +25,7 @@ This file is part of the iText (R) project.
2525import com .itextpdf .commons .utils .PlaceHolderTextUtil ;
2626import com .itextpdf .commons .utils .PlaceHolderTextUtil .PlaceHolderTextBy ;
2727import com .itextpdf .io .image .ImageDataFactory ;
28+ import com .itextpdf .io .source .ByteArrayOutputStream ;
2829import com .itextpdf .io .util .UrlUtil ;
2930import com .itextpdf .kernel .colors .Color ;
3031import com .itextpdf .kernel .colors .ColorConstants ;
@@ -35,6 +36,7 @@ This file is part of the iText (R) project.
3536import com .itextpdf .layout .Document ;
3637import com .itextpdf .layout .borders .Border ;
3738import com .itextpdf .layout .borders .SolidBorder ;
39+ import com .itextpdf .layout .exceptions .LayoutExceptionMessageConstant ;
3840import com .itextpdf .layout .logs .LayoutLogMessageConstant ;
3941import com .itextpdf .layout .properties .Background ;
4042import com .itextpdf .layout .properties .HorizontalAlignment ;
@@ -994,8 +996,89 @@ public void continuousColumContainerSetHeightSmaller() throws IOException, Inter
994996 });
995997 }
996998
999+ @ Test
1000+ public void paragraphWithColumnWidthTest () throws IOException , InterruptedException {
1001+ String outFileName = DESTINATION_FOLDER + "paragraphWithColumnWidthTest.pdf" ;
1002+ String cmpFileName = SOURCE_FOLDER + "cmp_paragraphWithColumnWidthTest.pdf" ;
1003+
1004+ try (Document document = new Document (new PdfDocument (new PdfWriter (outFileName )))) {
1005+ Div columnContainer = new MulticolContainer ();
1006+ columnContainer .setProperty (Property .COLUMN_WIDTH , 200.0f );
1007+ Paragraph paragraph = createDummyParagraph ();
1008+ columnContainer .add (paragraph );
1009+ document .add (columnContainer );
1010+ }
1011+ //expecting 2 columns with ~260px width each
1012+ Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , DESTINATION_FOLDER , "diff" ));
1013+ }
1014+
1015+ @ Test
1016+ public void paragraphWithColumnWidthAndColumnCountTest () throws IOException , InterruptedException {
1017+ String outFileName = DESTINATION_FOLDER + "paragraphWithColumnWidthAndColumnCountTest.pdf" ;
1018+ String cmpFileName = SOURCE_FOLDER + "cmp_paragraphWithColumnWidthAndColumnCountTest.pdf" ;
1019+
1020+ try (Document document = new Document (new PdfDocument (new PdfWriter (outFileName )))) {
1021+ Div columnContainer = new MulticolContainer ();
1022+ //column width is ignored in this case, because column-count requires higher width
1023+ columnContainer .setProperty (Property .COLUMN_WIDTH , 100.0f );
1024+ columnContainer .setProperty (Property .COLUMN_COUNT , 2 );
1025+ Paragraph paragraph = createDummyParagraph ();
1026+ columnContainer .add (paragraph );
1027+ document .add (columnContainer );
1028+ }
1029+ Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , DESTINATION_FOLDER , "diff" ));
1030+ }
1031+
1032+ @ Test
1033+ public void paragraphWithInvalidColumnValuesTest () {
1034+
1035+ try (Document document = new Document (new PdfDocument (new PdfWriter (new ByteArrayOutputStream ())))) {
1036+ Div columnContainer = new MulticolContainer ();
1037+ //column width is ignored in this case, because column-count requires higher width
1038+ columnContainer .setProperty (Property .COLUMN_WIDTH , -30.0f );
1039+ columnContainer .setProperty (Property .COLUMN_COUNT , -2 );
1040+ columnContainer .setProperty (Property .COLUMN_GAP , -20.0f );
1041+ Paragraph paragraph = createDummyParagraph ();
1042+ columnContainer .add (paragraph );
1043+ Throwable exception = Assert .assertThrows (IllegalStateException .class , () -> document .add (columnContainer ));
1044+ Assert .assertEquals (LayoutExceptionMessageConstant .INVALID_COLUMN_PROPERTIES , exception .getMessage ());
1045+ }
1046+ }
1047+
1048+ @ Test
1049+ public void paragraphWithColumnWidthAndGapTest () throws IOException , InterruptedException {
1050+ String outFileName = DESTINATION_FOLDER + "paragraphWithColumnWidthAndGapTest.pdf" ;
1051+ String cmpFileName = SOURCE_FOLDER + "cmp_paragraphWithColumnWidthAndGapTest.pdf" ;
1052+
1053+ try (Document document = new Document (new PdfDocument (new PdfWriter (outFileName )))) {
1054+ Div columnContainer = new MulticolContainer ();
1055+ columnContainer .setProperty (Property .COLUMN_WIDTH , 100.0f );
1056+ columnContainer .setProperty (Property .COLUMN_GAP , 100.0f );
1057+ Paragraph paragraph = createDummyParagraph ();
1058+ columnContainer .add (paragraph );
1059+ document .add (columnContainer );
1060+ }
1061+ Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , DESTINATION_FOLDER , "diff" ));
1062+ }
1063+
1064+ @ Test
1065+ public void paragraphWithColumnCountAndGapTest () throws IOException , InterruptedException {
1066+ String outFileName = DESTINATION_FOLDER + "paragraphWithColumnCountAndGapTest.pdf" ;
1067+ String cmpFileName = SOURCE_FOLDER + "cmp_paragraphWithColumnCountAndGapTest.pdf" ;
1068+
1069+ try (Document document = new Document (new PdfDocument (new PdfWriter (outFileName )))) {
1070+ Div columnContainer = new MulticolContainer ();
1071+ columnContainer .setProperty (Property .COLUMN_COUNT , 5 );
1072+ columnContainer .setProperty (Property .COLUMN_GAP , 50.0f );
1073+ Paragraph paragraph = createDummyParagraph ();
1074+ columnContainer .add (paragraph );
1075+ document .add (columnContainer );
1076+ }
1077+ Assert .assertNull (new CompareTool ().compareByContent (outFileName , cmpFileName , DESTINATION_FOLDER , "diff" ));
1078+ }
1079+
9971080
998- private < T extends IBlockElement > void executeTest (String testName , T container , Consumer <T > executor )
1081+ private void executeTest (String testName , MulticolContainer container , Consumer <MulticolContainer > executor )
9991082 throws IOException , InterruptedException {
10001083 String filename = DESTINATION_FOLDER + testName + ".pdf" ;
10011084 String cmpName = SOURCE_FOLDER + "cmp_" + testName + ".pdf" ;
@@ -1012,6 +1095,35 @@ private <T extends IBlockElement> void executeTest(String testName, T container,
10121095 Assert .assertNull (compareTool .compareByContent (filename , cmpName , DESTINATION_FOLDER , "diff_" ));
10131096 }
10141097
1098+ private static Paragraph createDummyParagraph () {
1099+ return new Paragraph ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
1100+ "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
1101+ "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute " +
1102+ "irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
1103+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim " +
1104+ "id est laborum." );
1105+ }
1106+
1107+ private static String generateLongString (int amountOfWords ) {
1108+ StringBuilder sb = new StringBuilder ();
1109+ int random = 1 ;
1110+ for (int i = 0 ; i < amountOfWords ; i ++) {
1111+ random = getPseudoRandomInt (i + random );
1112+ for (int j = 1 ; j <= random ; j ++) {
1113+ sb .append ('a' );
1114+ }
1115+ sb .append (' ' );
1116+ }
1117+ return sb .toString ();
1118+ }
1119+
1120+ private static int getPseudoRandomInt (int prev ) {
1121+ final int first = 93840 ;
1122+ final int second = 1929 ;
1123+ final int max = 7 ;
1124+ return (prev * first + second ) % max ;
1125+ }
1126+
10151127 private static Div createFirstPageFiller () {
10161128 Div firstPageFiller = new Div ();
10171129 firstPageFiller .setProperty (Property .MARGIN_TOP , UnitValue .createPointValue (50 ));
0 commit comments