|
| 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 | +} |
0 commit comments