@@ -25,6 +25,9 @@ You should have received a copy of the GNU Affero General Public License
25
25
using iText . IO . Image ;
26
26
using iText . Kernel . Font ;
27
27
using iText . Kernel . Geom ;
28
+ using iText . Kernel . Pdf ;
29
+ using iText . Kernel . Utils ;
30
+ using iText . Layout ;
28
31
using iText . Layout . Element ;
29
32
using iText . Layout . Layout ;
30
33
using iText . Layout . Properties ;
@@ -33,6 +36,17 @@ You should have received a copy of the GNU Affero General Public License
33
36
34
37
namespace iText . Layout . Renderer {
35
38
public class TargetCounterHandlerTest : ExtendedITextTest {
39
+ public static readonly String SOURCE_FOLDER = iText . Test . TestUtil . GetParentProjectDirectory ( NUnit . Framework . TestContext
40
+ . CurrentContext . TestDirectory ) + "/resources/itext/layout/renderer/TargetCounterHandlerTest/" ;
41
+
42
+ public static readonly String DESTINATION_FOLDER = NUnit . Framework . TestContext . CurrentContext . TestDirectory
43
+ + "/test/itext/layout/renderer/TargetCounterHandlerTest/" ;
44
+
45
+ [ NUnit . Framework . OneTimeSetUp ]
46
+ public static void BeforeClass ( ) {
47
+ CreateDestinationFolder ( DESTINATION_FOLDER ) ;
48
+ }
49
+
36
50
[ NUnit . Framework . Test ]
37
51
public virtual void BlockRendererAddByIDTest ( ) {
38
52
DocumentRenderer documentRenderer = new DocumentRenderer ( null ) ;
@@ -117,5 +131,62 @@ public virtual void LineRendererAddByIDTest() {
117
131
documentRenderer . GetTargetCounterHandler ( ) . PrepareHandlerToRelayout ( ) ;
118
132
NUnit . Framework . Assert . AreEqual ( ( int ? ) 4 , TargetCounterHandler . GetPageByID ( lineRenderer , id ) ) ;
119
133
}
134
+
135
+ [ NUnit . Framework . Test ]
136
+ public virtual void TargetCounterHandlerEndToEndLayoutTest ( ) {
137
+ String targetPdf = DESTINATION_FOLDER + "targetCounterHandlerEndToEndLayoutTest.pdf" ;
138
+ String cmpPdf = SOURCE_FOLDER + "cmp_targetCounterHandlerEndToEndLayoutTest.pdf" ;
139
+ Document document = new Document ( new PdfDocument ( new PdfWriter ( targetPdf ) ) , PageSize . A4 , false ) ;
140
+ Text pageNumPlaceholder = new Text ( "x" ) ;
141
+ String id = "1" ;
142
+ pageNumPlaceholder . SetProperty ( Property . ID , id ) ;
143
+ pageNumPlaceholder . SetNextRenderer ( new TargetCounterHandlerTest . TargetCounterAwareTextRenderer ( pageNumPlaceholder
144
+ ) ) ;
145
+ Paragraph intro = new Paragraph ( "The paragraph is on page " ) . Add ( pageNumPlaceholder ) ;
146
+ document . Add ( intro ) ;
147
+ document . Add ( new AreaBreak ( ) ) ;
148
+ Paragraph text = new Paragraph ( "This is main text" ) ;
149
+ text . SetProperty ( Property . ID , id ) ;
150
+ text . SetNextRenderer ( new TargetCounterHandlerTest . TargetCounterAwareParagraphRenderer ( text ) ) ;
151
+ document . Add ( text ) ;
152
+ document . Relayout ( ) ;
153
+ document . Close ( ) ;
154
+ NUnit . Framework . Assert . IsNull ( new CompareTool ( ) . CompareByContent ( targetPdf , cmpPdf , DESTINATION_FOLDER , "diff"
155
+ ) ) ;
156
+ }
157
+
158
+ private class TargetCounterAwareTextRenderer : TextRenderer {
159
+ public TargetCounterAwareTextRenderer ( Text link )
160
+ : base ( link ) {
161
+ }
162
+
163
+ public override LayoutResult Layout ( LayoutContext layoutContext ) {
164
+ int ? targetPageNumber = TargetCounterHandler . GetPageByID ( this , this . GetProperty < String > ( Property . ID ) ) ;
165
+ if ( targetPageNumber != null ) {
166
+ SetText ( targetPageNumber . ToString ( ) ) ;
167
+ }
168
+ return base . Layout ( layoutContext ) ;
169
+ }
170
+
171
+ public override IRenderer GetNextRenderer ( ) {
172
+ return new TargetCounterHandlerTest . TargetCounterAwareTextRenderer ( ( Text ) GetModelElement ( ) ) ;
173
+ }
174
+ }
175
+
176
+ private class TargetCounterAwareParagraphRenderer : ParagraphRenderer {
177
+ public TargetCounterAwareParagraphRenderer ( Paragraph modelElement )
178
+ : base ( modelElement ) {
179
+ }
180
+
181
+ public override IRenderer GetNextRenderer ( ) {
182
+ return new TargetCounterHandlerTest . TargetCounterAwareParagraphRenderer ( ( Paragraph ) modelElement ) ;
183
+ }
184
+
185
+ public override LayoutResult Layout ( LayoutContext layoutContext ) {
186
+ LayoutResult result = base . Layout ( layoutContext ) ;
187
+ TargetCounterHandler . AddPageByID ( this ) ;
188
+ return result ;
189
+ }
190
+ }
120
191
}
121
192
}
0 commit comments