Skip to content

Commit 48c7527

Browse files
BlackEgoistiText-CI
authored andcommitted
Deprecate one of Canvas constructors
Remove references to deprecated constructor Add unit tests DEVSIX-3767 Autoported commit. Original commit hash: [77e4ac657]
1 parent c5dc949 commit 48c7527

File tree

11 files changed

+114
-25
lines changed

11 files changed

+114
-25
lines changed

itext.tests/itext.forms.tests/itext/forms/PdfFormFieldTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ public virtual void FillFormWithDefaultResourcesUpdateFont() {
489489
PdfFormField field = fields.Get("Text1");
490490
field.SetFont(PdfFontFactory.CreateFont(StandardFonts.COURIER));
491491
field.SetValue("New value size must be 8, but with different font.");
492-
new Canvas(new PdfCanvas(pdfDoc.GetFirstPage()), pdfDoc, new Rectangle(30, 500, 500, 200)).Add(new Paragraph
493-
("The text font after modification it via PDF viewer (e.g. Acrobat) shall be preserved."));
492+
new Canvas(new PdfCanvas(pdfDoc.GetFirstPage()), new Rectangle(30, 500, 500, 200)).Add(new Paragraph("The text font after modification it via PDF viewer (e.g. Acrobat) shall be preserved."
493+
));
494494
pdfDoc.Close();
495495
CompareTool compareTool = new CompareTool();
496496
String errorMessage = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "diff_");

itext.tests/itext.layout.tests/itext/layout/CanvasTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public virtual void CanvasNoPageLinkTest() {
7777
PdfCanvas pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);
7878
Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72
7979
, 50);
80-
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdf, rectangle);
80+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, rectangle);
8181
canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline
8282
().SetFontColor(ColorConstants.BLUE)));
8383
canvas.Close();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using iText.IO.Source;
24+
using iText.Kernel.Geom;
25+
using iText.Kernel.Pdf;
26+
using iText.Kernel.Pdf.Canvas;
27+
using iText.Test;
28+
29+
namespace iText.Layout {
30+
public class CanvasUnitTest : ExtendedITextTest {
31+
[NUnit.Framework.Test]
32+
public virtual void CanvasImmediateFlushConstructorTest() {
33+
PdfDocument pdf = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
34+
PdfPage page = pdf.AddNewPage();
35+
PdfCanvas pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);
36+
Rectangle rectangle = new Rectangle(0, 0);
37+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, rectangle, false);
38+
NUnit.Framework.Assert.AreEqual(pdfCanvas.GetDocument(), canvas.GetPdfDocument());
39+
NUnit.Framework.Assert.IsFalse(canvas.immediateFlush);
40+
}
41+
42+
[NUnit.Framework.Test]
43+
public virtual void CanvasImmediateFlushDeprecatedConstructorTest() {
44+
//TODO remove test after deprecated constructor is removed
45+
PdfDocument pdf = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
46+
PdfPage page = pdf.AddNewPage();
47+
Rectangle pageSize = page.GetPageSize();
48+
PdfCanvas pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);
49+
Rectangle rectangle = new Rectangle(0, 0);
50+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdf, rectangle, false);
51+
NUnit.Framework.Assert.AreEqual(pdfCanvas.GetDocument(), canvas.GetPdfDocument());
52+
NUnit.Framework.Assert.IsFalse(canvas.immediateFlush);
53+
}
54+
}
55+
}

itext.tests/itext.layout.tests/itext/layout/FloatTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ public virtual void FloatsOnCanvas() {
400400
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFile)).SetTagged();
401401
PdfPage page = pdfDoc.AddNewPage();
402402
PdfCanvas pdfCanvas = new PdfCanvas(page);
403-
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDoc, page.GetPageSize().ApplyMargins(36
404-
, 36, 36, 36, false));
403+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, page.GetPageSize().ApplyMargins(36, 36, 36
404+
, 36, false));
405405
canvas.EnableAutoTagging(page);
406406
Div div = new Div().SetBackgroundColor(ColorConstants.RED);
407407
Div fDiv = new Div().SetBackgroundColor(ColorConstants.BLUE).SetWidth(200).SetHeight(200);

itext.tests/itext.layout.tests/itext/layout/ImageTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ public virtual void PrecisionTest01() {
745745
PdfPage page = pdfDoc.AddNewPage();
746746
PdfCanvas currentPdfCanvas = new PdfCanvas(page);
747747
Rectangle rc = new Rectangle(56.6929131f, 649.13385f, 481.889771f, 136.062988f);
748-
iText.Layout.Canvas canvas = new iText.Layout.Canvas(currentPdfCanvas, pdfDoc, rc);
748+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(currentPdfCanvas, rc);
749749
Table table = new Table(UnitValue.CreatePointArray(new float[] { 158f }));
750750
table.SetTextAlignment(TextAlignment.LEFT);
751751
iText.Layout.Element.Image logoImage = new iText.Layout.Element.Image(ImageDataFactory.Create(imageFileName

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ public virtual void OverflowHiddenOnCanvasTest01() {
178178
String cmpFileName = sourceFolder + "cmp_overflowHiddenOnCanvasTest01.pdf";
179179
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
180180
PdfPage page = pdfDocument.AddNewPage();
181-
iText.Layout.Canvas canvas = new Canvas(new PdfCanvas(page), pdfDocument, page.GetPageSize().Clone().ApplyMargins
182-
(36, 36, 36, 36, false));
181+
iText.Layout.Canvas canvas = new Canvas(new PdfCanvas(page), page.GetPageSize().Clone().ApplyMargins(36, 36
182+
, 36, 36, false));
183183
AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.HIDDEN);
184184
canvas.Close();
185185
pdfDocument.Close();
@@ -208,8 +208,8 @@ public virtual void OverflowVisibleOnCanvasTest01() {
208208
String cmpFileName = sourceFolder + "cmp_overflowVisibleOnCanvasTest01.pdf";
209209
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
210210
PdfPage page = pdfDocument.AddNewPage();
211-
iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(page), pdfDocument, page.GetPageSize().
212-
Clone().ApplyMargins(36, 36, 36, 36, false));
211+
iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(page), page.GetPageSize().Clone().ApplyMargins
212+
(36, 36, 36, 36, false));
213213
AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.VISIBLE);
214214
canvas.Close();
215215
pdfDocument.Close();

itext.tests/itext.pdfa.tests/itext/pdfa/PdfA2LayoutOcgTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public virtual void CheckIfOcgForPdfA2Works() {
8080
iText.Layout.Element.Image image1 = new Image(ImageDataFactory.Create(sourceFolder + "images/manualTransparency_for_png.png"
8181
));
8282
PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc, 1);
83-
iText.Layout.Canvas canvas1 = new iText.Layout.Canvas(pdfCanvas, pdfDoc, new Rectangle(0, 0, 590, 420));
83+
iText.Layout.Canvas canvas1 = new iText.Layout.Canvas(pdfCanvas, new Rectangle(0, 0, 590, 420));
8484
PdfLayer imageLayer1 = new PdfLayer("*[email protected]", pdfDoc);
8585
imageLayer1.SetOn(true);
8686
pdfCanvas.BeginLayer(imageLayer1);

itext/itext.forms/itext/forms/fields/PdfFormField.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,8 +3083,8 @@ protected internal virtual void DrawTextAppearance(Rectangle rect, PdfFont font,
30833083
x = rect.GetWidth() / 2;
30843084
}
30853085
}
3086-
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, GetDocument(), new Rectangle(0, -height,
3087-
0, 2 * height));
3086+
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, new Rectangle(0, -height, 0, 2 * height)
3087+
);
30883088
modelCanvas.SetProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
30893089
Style paragraphStyle = new Style().SetFont(font).SetFontSize(fontSize);
30903090
paragraphStyle.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, 1));
@@ -3158,7 +3158,7 @@ protected internal virtual void DrawMultiLineTextAppearance(Rectangle rect, PdfF
31583158
DrawBorder(canvas, appearance, width, height);
31593159
canvas.BeginVariableText();
31603160
Rectangle areaRect = new Rectangle(0, 0, width, height);
3161-
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, GetDocument(), areaRect);
3161+
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, areaRect);
31623162
modelCanvas.SetProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
31633163
Paragraph paragraph = CreateParagraphForTextFieldValue(value).SetFont(font).SetMargin(0).SetPadding(3).SetMultipliedLeading
31643164
(1);
@@ -3200,8 +3200,8 @@ private void DrawChoiceAppearance(Rectangle rect, float fontSize, String value,
32003200
DrawBorder(canvas, appearance, width, height);
32013201
canvas.BeginVariableText().SaveState().Rectangle(3, 3, width - widthBorder, height - heightBorder).Clip().
32023202
EndPath();
3203-
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, GetDocument(), new Rectangle(3, 0, Math.
3204-
Max(0, width - widthBorder), Math.Max(0, height - heightBorder)));
3203+
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, new Rectangle(3, 0, Math.Max(0, width -
3204+
widthBorder), Math.Max(0, height - heightBorder)));
32053205
modelCanvas.SetProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
32063206
for (int index = 0; index < strings.Count; index++) {
32073207
bool? isFull = modelCanvas.GetRenderer().GetPropertyAsBoolean(Property.FULL);
@@ -3574,8 +3574,8 @@ protected internal virtual void DrawButton(PdfCanvas canvas, float x, float y, f
35743574
}
35753575
Paragraph paragraph = new Paragraph(text).SetFont(font).SetFontSize(fontSize).SetMargin(0).SetMultipliedLeading
35763576
(1).SetVerticalAlignment(VerticalAlignment.MIDDLE);
3577-
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, GetDocument(), new Rectangle(0, -height,
3578-
width, 2 * height));
3577+
iText.Layout.Canvas modelCanvas = new iText.Layout.Canvas(canvas, new Rectangle(0, -height, width, 2 * height
3578+
));
35793579
modelCanvas.SetProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
35803580
modelCanvas.ShowTextAligned(paragraph, width / 2, height / 2, TextAlignment.CENTER, VerticalAlignment.MIDDLE
35813581
);

itext/itext.layout/itext/layout/Canvas.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,26 @@ public class Canvas : RootElement<iText.Layout.Canvas> {
105105
/// </param>
106106
/// <param name="rootArea">the maximum area that the Canvas may write upon</param>
107107
public Canvas(PdfPage page, Rectangle rootArea)
108-
: this(InitPdfCanvasOrThrowIfPageIsFlushed(page), page.GetDocument(), rootArea) {
108+
: this(InitPdfCanvasOrThrowIfPageIsFlushed(page), rootArea) {
109109
this.EnableAutoTagging(page);
110110
this.isCanvasOfPage = true;
111111
}
112112

113+
/// <summary>
114+
/// Creates a new Canvas to manipulate a specific content stream, which might be for example a page
115+
/// or
116+
/// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/>
117+
/// stream.
118+
/// </summary>
119+
/// <param name="pdfCanvas">the low-level content stream writer</param>
120+
/// <param name="rootArea">the maximum area that the Canvas may write upon</param>
121+
public Canvas(PdfCanvas pdfCanvas, Rectangle rootArea)
122+
: base() {
123+
this.pdfDocument = pdfCanvas.GetDocument();
124+
this.pdfCanvas = pdfCanvas;
125+
this.rootArea = rootArea;
126+
}
127+
113128
/// <summary>
114129
/// Creates a new Canvas to manipulate a specific document and content stream, which might be for example a page
115130
/// or
@@ -118,7 +133,12 @@ public Canvas(PdfPage page, Rectangle rootArea)
118133
/// </summary>
119134
/// <param name="pdfCanvas">the low-level content stream writer</param>
120135
/// <param name="pdfDocument">the document that the resulting content stream will be written to</param>
121-
/// <param name="rootArea">the maximum area that the Canvas may write upon</param>
136+
/// <param name="rootArea">
137+
/// the maximum area that the Canvas may write upon
138+
/// To be removed in 7.2
139+
/// </param>
140+
[System.ObsoleteAttribute(@"use Canvas(iText.Kernel.Pdf.Canvas.PdfCanvas, iText.Kernel.Geom.Rectangle) instead."
141+
)]
122142
public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea)
123143
: base() {
124144
this.pdfDocument = pdfDocument;
@@ -128,11 +148,25 @@ public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea)
128148

129149
/// <summary>Creates a new Canvas to manipulate a specific document and page.</summary>
130150
/// <param name="pdfCanvas">The low-level content stream writer</param>
131-
/// <param name="pdfDocument">The document that the resulting content stream will be written to</param>
132151
/// <param name="rootArea">The maximum area that the Canvas may write upon</param>
133152
/// <param name="immediateFlush">Whether to flush the canvas immediately after operations, false otherwise</param>
153+
public Canvas(PdfCanvas pdfCanvas, Rectangle rootArea, bool immediateFlush)
154+
: this(pdfCanvas, rootArea) {
155+
this.immediateFlush = immediateFlush;
156+
}
157+
158+
/// <summary>Creates a new Canvas to manipulate a specific document and page.</summary>
159+
/// <param name="pdfCanvas">The low-level content stream writer</param>
160+
/// <param name="pdfDocument">The document that the resulting content stream will be written to</param>
161+
/// <param name="rootArea">The maximum area that the Canvas may write upon</param>
162+
/// <param name="immediateFlush">
163+
/// Whether to flush the canvas immediately after operations, false otherwise
164+
/// To be removed in 7.2
165+
/// </param>
166+
[System.ObsoleteAttribute(@"use Canvas(iText.Kernel.Pdf.Canvas.PdfCanvas, iText.Kernel.Geom.Rectangle, bool) instead."
167+
)]
134168
public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea, bool immediateFlush)
135-
: this(pdfCanvas, pdfDocument, rootArea) {
169+
: this(pdfCanvas, rootArea) {
136170
this.immediateFlush = immediateFlush;
137171
}
138172

@@ -143,7 +177,7 @@ public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea,
143177
/// <param name="formXObject">the form</param>
144178
/// <param name="pdfDocument">the document that the resulting content stream will be written to</param>
145179
public Canvas(PdfFormXObject formXObject, PdfDocument pdfDocument)
146-
: this(new PdfCanvas(formXObject, pdfDocument), pdfDocument, formXObject.GetBBox().ToRectangle()) {
180+
: this(new PdfCanvas(formXObject, pdfDocument), formXObject.GetBBox().ToRectangle()) {
147181
}
148182

149183
/// <summary>

itext/itext.sign/itext/signatures/PdfSignatureAppearance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ private void AddTextToCanvas(String text, PdfFont font, Rectangle dataRect) {
720720
PdfCanvas canvas;
721721
canvas = new PdfCanvas(n2, document);
722722
Paragraph paragraph = new Paragraph(text).SetFont(font).SetMargin(0).SetMultipliedLeading(0.9f);
723-
iText.Layout.Canvas layoutCanvas = new iText.Layout.Canvas(canvas, document, dataRect);
723+
iText.Layout.Canvas layoutCanvas = new iText.Layout.Canvas(canvas, dataRect);
724724
paragraph.SetFontColor(layer2FontColor);
725725
if (layer2FontSize == 0) {
726726
ApplyCopyFittingFontSize(paragraph, dataRect, layoutCanvas.GetRenderer());

0 commit comments

Comments
 (0)