Skip to content

Commit 848914c

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Introduce new constructor Canvas(PdfPage, Rectangle)
Using this constructor allows to add link annotations and destinations via Canvas. DEVSIX-2486 Autoported commit. Original commit hash: [4da833d26]
1 parent 69d63ef commit 848914c

16 files changed

+304
-8
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using iText.Kernel.Colors;
3+
using iText.Kernel.Geom;
4+
using iText.Kernel.Pdf;
5+
using iText.Kernel.Pdf.Action;
6+
using iText.Kernel.Pdf.Canvas;
7+
using iText.Kernel.Utils;
8+
using iText.Layout.Element;
9+
using iText.Test;
10+
using iText.Test.Attributes;
11+
12+
namespace iText.Layout {
13+
public class CanvasTest : ExtendedITextTest {
14+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
15+
.CurrentContext.TestDirectory) + "/resources/itext/layout/CanvasTest/";
16+
17+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
18+
+ "/test/itext/layout/CanvasTest/";
19+
20+
[NUnit.Framework.OneTimeSetUp]
21+
public static void BeforeClass() {
22+
CreateOrClearDestinationFolder(destinationFolder);
23+
}
24+
25+
/// <exception cref="System.IO.IOException"/>
26+
/// <exception cref="System.Exception"/>
27+
[NUnit.Framework.Test]
28+
[LogMessage(iText.IO.LogMessageConstant.UNABLE_TO_APPLY_PAGE_DEPENDENT_PROP_UNKNOWN_PAGE_ON_WHICH_ELEMENT_IS_DRAWN
29+
)]
30+
public virtual void CanvasNoPageLinkTest() {
31+
String testName = "canvasNoPageLinkTest";
32+
String @out = destinationFolder + testName + ".pdf";
33+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
34+
PdfDocument pdf = new PdfDocument(new PdfWriter(@out));
35+
PdfPage page = pdf.AddNewPage();
36+
Rectangle pageSize = page.GetPageSize();
37+
PdfCanvas pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);
38+
Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72
39+
, 50);
40+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdf, rectangle);
41+
canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline
42+
().SetFontColor(ColorConstants.BLUE)));
43+
canvas.Close();
44+
pdf.Close();
45+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
46+
}
47+
48+
/// <exception cref="System.IO.IOException"/>
49+
/// <exception cref="System.Exception"/>
50+
[NUnit.Framework.Test]
51+
public virtual void CanvasWithPageLinkTest() {
52+
String testName = "canvasWithPageLinkTest";
53+
String @out = destinationFolder + testName + ".pdf";
54+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
55+
PdfDocument pdf = new PdfDocument(new PdfWriter(@out));
56+
PdfPage page = pdf.AddNewPage();
57+
Rectangle pageSize = page.GetPageSize();
58+
Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72
59+
, 50);
60+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, rectangle);
61+
canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline
62+
().SetFontColor(ColorConstants.BLUE)));
63+
canvas.Close();
64+
pdf.Close();
65+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
66+
}
67+
68+
/// <exception cref="System.IO.IOException"/>
69+
/// <exception cref="System.Exception"/>
70+
[NUnit.Framework.Test]
71+
public virtual void CanvasWithPageEnableTaggingTest01() {
72+
String testName = "canvasWithPageEnableTaggingTest01";
73+
String @out = destinationFolder + testName + ".pdf";
74+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
75+
PdfDocument pdf = new PdfDocument(new PdfWriter(@out));
76+
pdf.SetTagged();
77+
PdfPage page = pdf.AddNewPage();
78+
Rectangle pageSize = page.GetPageSize();
79+
Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72
80+
, 50);
81+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, rectangle);
82+
canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline
83+
().SetFontColor(ColorConstants.BLUE)));
84+
canvas.Close();
85+
pdf.Close();
86+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
87+
}
88+
89+
/// <exception cref="System.IO.IOException"/>
90+
/// <exception cref="System.Exception"/>
91+
[NUnit.Framework.Test]
92+
[LogMessage(iText.IO.LogMessageConstant.UNABLE_TO_APPLY_PAGE_DEPENDENT_PROP_UNKNOWN_PAGE_ON_WHICH_ELEMENT_IS_DRAWN
93+
)]
94+
[LogMessage(iText.IO.LogMessageConstant.PASSED_PAGE_SHALL_BE_ON_WHICH_CANVAS_WILL_BE_RENDERED)]
95+
public virtual void CanvasWithPageEnableTaggingTest02() {
96+
String testName = "canvasWithPageEnableTaggingTest02";
97+
String @out = destinationFolder + testName + ".pdf";
98+
String cmp = sourceFolder + "cmp_" + testName + ".pdf";
99+
PdfDocument pdf = new PdfDocument(new PdfWriter(@out));
100+
pdf.SetTagged();
101+
PdfPage page = pdf.AddNewPage();
102+
Rectangle pageSize = page.GetPageSize();
103+
Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72
104+
, 50);
105+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, rectangle);
106+
// This will disable tagging and also prevent annotations addition. Created tagged document is invalid. Expected log message.
107+
canvas.EnableAutoTagging(null);
108+
canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline
109+
().SetFontColor(ColorConstants.BLUE)));
110+
canvas.Close();
111+
pdf.Close();
112+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder));
113+
}
114+
}
115+
}

itext.tests/itext.layout.tests/itext/layout/OverflowTest.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ source product.
4747
using iText.Kernel.Colors;
4848
using iText.Kernel.Font;
4949
using iText.Kernel.Pdf;
50+
using iText.Kernel.Pdf.Canvas;
5051
using iText.Kernel.Utils;
5152
using iText.Layout.Borders;
5253
using iText.Layout.Element;
@@ -183,6 +184,88 @@ public virtual void AlignedInlineContentOverflowHiddenTest02() {
183184
, "diff"));
184185
}
185186

187+
/// <exception cref="System.IO.IOException"/>
188+
/// <exception cref="System.Exception"/>
189+
[NUnit.Framework.Test]
190+
public virtual void OverflowHiddenOnCanvasTest01() {
191+
String outFileName = destinationFolder + "overflowHiddenOnCanvasTest01.pdf";
192+
String cmpFileName = sourceFolder + "cmp_overflowHiddenOnCanvasTest01.pdf";
193+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
194+
PdfPage page = pdfDocument.AddNewPage();
195+
iText.Layout.Canvas canvas = new Canvas(new PdfCanvas(page), pdfDocument, page.GetPageSize().Clone().ApplyMargins
196+
(36, 36, 36, 36, false));
197+
AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.HIDDEN);
198+
canvas.Close();
199+
pdfDocument.Close();
200+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
201+
, "diff"));
202+
}
203+
204+
/// <exception cref="System.IO.IOException"/>
205+
/// <exception cref="System.Exception"/>
206+
[NUnit.Framework.Test]
207+
public virtual void OverflowHiddenOnCanvasTest02() {
208+
String outFileName = destinationFolder + "overflowHiddenOnCanvasTest02.pdf";
209+
String cmpFileName = sourceFolder + "cmp_overflowHiddenOnCanvasTest02.pdf";
210+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
211+
PdfPage page = pdfDocument.AddNewPage();
212+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, page.GetPageSize().Clone().ApplyMargins(36, 36,
213+
36, 36, false));
214+
AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.HIDDEN);
215+
canvas.Close();
216+
pdfDocument.Close();
217+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
218+
, "diff"));
219+
}
220+
221+
/// <exception cref="System.IO.IOException"/>
222+
/// <exception cref="System.Exception"/>
223+
[NUnit.Framework.Test]
224+
public virtual void OverflowVisibleOnCanvasTest01() {
225+
String outFileName = destinationFolder + "overflowVisibleOnCanvasTest01.pdf";
226+
String cmpFileName = sourceFolder + "cmp_overflowVisibleOnCanvasTest01.pdf";
227+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
228+
PdfPage page = pdfDocument.AddNewPage();
229+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(page), pdfDocument, page.GetPageSize().
230+
Clone().ApplyMargins(36, 36, 36, 36, false));
231+
AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.VISIBLE);
232+
canvas.Close();
233+
pdfDocument.Close();
234+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
235+
, "diff"));
236+
}
237+
238+
/// <exception cref="System.IO.IOException"/>
239+
/// <exception cref="System.Exception"/>
240+
[NUnit.Framework.Test]
241+
public virtual void OverflowVisibleOnCanvasTest02() {
242+
String outFileName = destinationFolder + "overflowVisibleOnCanvasTest02.pdf";
243+
String cmpFileName = sourceFolder + "cmp_overflowVisibleOnCanvasTest02.pdf";
244+
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
245+
PdfPage page = pdfDocument.AddNewPage();
246+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, page.GetPageSize().Clone().ApplyMargins(36, 36,
247+
36, 36, false));
248+
AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.VISIBLE);
249+
canvas.Close();
250+
pdfDocument.Close();
251+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
252+
, "diff"));
253+
}
254+
255+
/// <exception cref="System.UriFormatException"/>
256+
private static void AddParaWithImgSetOverflowX(iText.Layout.Canvas canvas, OverflowPropertyValue? overflowX
257+
) {
258+
String imgPath = sourceFolder + "itis.jpg";
259+
iText.Layout.Element.Image img = new iText.Layout.Element.Image(ImageDataFactory.Create(imgPath));
260+
Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.CENTER).SetHeight(150f).SetWidth(150f).SetBorder
261+
(new SolidBorder(5f));
262+
p.SetProperty(Property.OVERFLOW_X, overflowX);
263+
p.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.HIDDEN);
264+
img.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
265+
img.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE);
266+
canvas.Add(p.Add(img));
267+
}
268+
186269
/// <exception cref="System.IO.IOException"/>
187270
/// <exception cref="System.Exception"/>
188271
[NUnit.Framework.Test]

0 commit comments

Comments
 (0)