@@ -49,6 +49,7 @@ This file is part of the iText (R) project.
49
49
import com .itextpdf .layout .property .ParagraphWidowsControl ;
50
50
import com .itextpdf .layout .property .Property ;
51
51
import com .itextpdf .layout .property .UnitValue ;
52
+ import com .itextpdf .layout .renderer .DocumentRenderer ;
52
53
import com .itextpdf .layout .renderer .ParagraphRenderer ;
53
54
54
55
import java .io .FileNotFoundException ;
@@ -510,6 +511,114 @@ public static void produceOrphansWidowsUnexpectedWidthOfNextAreaTestCase(String
510
511
document .close ();
511
512
}
512
513
514
+ public static void produceOrphansOrWidowsTestCase (String outPdf , int linesLeft , boolean orphans ,
515
+ Paragraph testPara ) throws FileNotFoundException {
516
+ Document doc = new Document (new PdfDocument (new PdfWriter (outPdf )));
517
+
518
+ PageSize pageSize = new PageSize (PageSize .A4 .getWidth (), PageSize .A5 .getHeight ());
519
+ doc .getPdfDocument ().setDefaultPageSize (pageSize );
520
+
521
+ testPara .setMargin (0 ).setBackgroundColor (new DeviceRgb (232 , 232 , 232 ));
522
+ testPara .add (PARA_TEXT );
523
+
524
+ String orphansOrWidows = orphans ? "orphans" : "widows" ;
525
+ String description = "Test " + orphansOrWidows + ".\n " + " This block height is adjusted in"
526
+ + " such way as to leave " + (String .valueOf (linesLeft )) + " line(s) on area break.\n "
527
+ + " Configuration is identified by the file name.\n Reference example"
528
+ + " without " + orphansOrWidows + " control can be found on the next page." ;
529
+
530
+ float effectiveWidth ;
531
+ float effectiveHeight ;
532
+
533
+ doc .setRenderer (new DocumentRenderer (doc ));
534
+
535
+ Rectangle effectiveArea = doc .getPageEffectiveArea (pageSize );
536
+ effectiveWidth = effectiveArea .getWidth ();
537
+ effectiveHeight = effectiveArea .getHeight ();
538
+
539
+ float linesHeight = calculateHeightForLinesNum (doc , testPara , effectiveWidth , linesLeft , orphans );
540
+ float adjustmentHeight = effectiveHeight - linesHeight - LINES_SPACE_EPS ;
541
+
542
+ doc .add (new Paragraph ()
543
+ .add (new Text (description ))
544
+ .setMargin (0 )
545
+ .setBorder (new SolidBorder (1 ))
546
+ .setHeight (adjustmentHeight ));
547
+
548
+ doc .add (testPara );
549
+ doc .add (new AreaBreak (AreaBreakType .NEXT_PAGE ));
550
+
551
+ doc .add (new Paragraph ("Reference example without orphans/widows control." )
552
+ .setMargin (0 )
553
+ .setBorder (new SolidBorder (1 ))
554
+ .setHeight (adjustmentHeight ));
555
+
556
+ doc .add (new Paragraph (PARA_TEXT ).setMargin (0 ).setBackgroundColor (new DeviceRgb (232 , 232 , 232 )));
557
+
558
+ doc .close ();
559
+ }
560
+
561
+ public static void produceOrphansAndWidowsTestCase (String outPdf , Paragraph testPara ) throws FileNotFoundException {
562
+ Document doc = new Document (new PdfDocument (new PdfWriter (outPdf )));
563
+
564
+ PageSize pageSize = new PageSize (PageSize .A4 .getWidth (), PageSize .A5 .getHeight ());
565
+ doc .getPdfDocument ().setDefaultPageSize (pageSize );
566
+
567
+ Rectangle [] columns = initUniformColumns (pageSize , 2 );
568
+ doc .setRenderer (new ColumnDocumentRenderer (doc , columns ));
569
+
570
+ String paraText = "A one line string\n " ;
571
+
572
+ testPara .setMargin (0 ).setBackgroundColor (new DeviceRgb (232 , 232 , 232 ));
573
+ testPara .add (paraText );
574
+
575
+ float linesHeight = calculateHeightForLinesNum (doc , testPara , columns [1 ].getWidth (), 1 , true );
576
+ float adjustmentHeight = columns [0 ].getHeight () - linesHeight - LINES_SPACE_EPS ;
577
+
578
+ String description = "Test orphans and widows case at once. This block height"
579
+ + " is adjusted in such way that both orphans and widows cases occur.\n "
580
+ + "The following paragraph contains as many fitting in one line text strings as needed"
581
+ + " to reproduce the case with both orphans and widows\n "
582
+ + "Reference example without orphans and widows"
583
+ + " control can be found on the next page" ;
584
+
585
+ doc .add (new Paragraph (description )
586
+ .setMargin (0 )
587
+ .setBorder (new SolidBorder (1 ))
588
+ .setHeight (adjustmentHeight ));
589
+
590
+ Paragraph tempPara = new Paragraph ().setMargin (0 );
591
+ for (int i = 0 ; i < 50 ; i ++) {
592
+ tempPara .add (paraText );
593
+ }
594
+
595
+ ParagraphRenderer renderer = (ParagraphRenderer ) tempPara .createRendererSubTree ().setParent (doc .getRenderer ());
596
+ LayoutResult layoutRes = renderer .layout (new LayoutContext (new LayoutArea
597
+ (1 , new Rectangle ( columns [1 ].getWidth (), columns [1 ].getHeight ()))));
598
+ int numberOfLines = ((ParagraphRenderer ) layoutRes .getSplitRenderer ()).getLines ().size ();
599
+ for (int i = 0 ; i <= numberOfLines ; i ++) {
600
+ testPara .add (paraText );
601
+ }
602
+ doc .add (testPara );
603
+ doc .add (new AreaBreak (AreaBreakType .NEXT_PAGE ));
604
+
605
+ doc .add (new Paragraph ("Reference example without orphans and widows control." )
606
+ .setMargin (0 )
607
+ .setBorder (new SolidBorder (1 ))
608
+ .setHeight (adjustmentHeight ));
609
+
610
+ Paragraph paragraph = new Paragraph ();
611
+ for (int i = 0 ; i <= numberOfLines + 1 ; i ++) {
612
+ paragraph .add (paraText );
613
+ }
614
+ paragraph .setMargin (0 ).setBackgroundColor (new DeviceRgb (232 , 232 , 232 ));
615
+ doc .add (paragraph );
616
+
617
+ doc .add (new Paragraph (paraText ).setMargin (0 ).setBackgroundColor (new DeviceRgb (232 , 232 , 232 )));
618
+
619
+ doc .close ();
620
+ }
621
+
513
622
public static float calculateHeightForLinesNum (Document doc , Paragraph p , float width , float linesNum ,
514
623
boolean orphans ) {
515
624
ParagraphRenderer renderer = (ParagraphRenderer ) p .createRendererSubTree ().setParent (doc .getRenderer ());
0 commit comments