|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2019 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 System; |
| 24 | +using System.IO; |
| 25 | +using iText.IO.Image; |
| 26 | +using iText.IO.Source; |
| 27 | +using iText.IO.Util; |
| 28 | +using iText.Kernel.Pdf; |
| 29 | +using iText.Kernel.Utils; |
| 30 | +using iText.Test; |
| 31 | +using iText.Test.Attributes; |
| 32 | + |
| 33 | +namespace iText.Kernel.Pdf.Canvas { |
| 34 | + public class PdfCanvasInlineImagesTest : ExtendedITextTest { |
| 35 | + public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory |
| 36 | + + "/test/itext/kernel/pdf/canvas/PdfCanvasInlineImagesTest/"; |
| 37 | + |
| 38 | + public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 39 | + .CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/canvas/PdfCanvasInlineImagesTest/"; |
| 40 | + |
| 41 | + [NUnit.Framework.OneTimeSetUp] |
| 42 | + public static void BeforeClass() { |
| 43 | + CreateOrClearDestinationFolder(destinationFolder); |
| 44 | + } |
| 45 | + |
| 46 | + [NUnit.Framework.Test] |
| 47 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_JBIG2DECODE_FILTER)] |
| 48 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_JPXDECODE_FILTER)] |
| 49 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_MASK)] |
| 50 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_SIZE_CANNOT_BE_MORE_4KB)] |
| 51 | + public virtual void InlineImagesTest01() { |
| 52 | + String filename = "inlineImages01.pdf"; |
| 53 | + PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); |
| 54 | + PdfPage page = document.AddNewPage(); |
| 55 | + PdfCanvas canvas = new PdfCanvas(page); |
| 56 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "Desert.jpg"), 36, 700, 100, true); |
| 57 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "bulb.gif"), 36, 600, 100, true); |
| 58 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "smpl.bmp"), 36, 500, 100, true); |
| 59 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "itext.png"), 36, 460, 100, true); |
| 60 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "0047478.jpg"), 36, 300, 100, true); |
| 61 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "map.jp2"), 36, 200, 100, true); |
| 62 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "amb.jb2"), 36, 30, 100, true); |
| 63 | + document.Close(); |
| 64 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder |
| 65 | + + "cmp_" + filename, destinationFolder)); |
| 66 | + } |
| 67 | + |
| 68 | + [NUnit.Framework.Test] |
| 69 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_JBIG2DECODE_FILTER)] |
| 70 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_JPXDECODE_FILTER)] |
| 71 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_MASK)] |
| 72 | + [LogMessage(iText.IO.LogMessageConstant.IMAGE_SIZE_CANNOT_BE_MORE_4KB)] |
| 73 | + public virtual void InlineImagesTest02() { |
| 74 | + String filename = "inlineImages02.pdf"; |
| 75 | + PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); |
| 76 | + PdfPage page = document.AddNewPage(); |
| 77 | + PdfCanvas canvas = new PdfCanvas(page); |
| 78 | + Stream stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "Desert.jpg")); |
| 79 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 80 | + StreamUtil.TransferBytes(stream, baos); |
| 81 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 700, 100, true); |
| 82 | + stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "bulb.gif")); |
| 83 | + baos = new ByteArrayOutputStream(); |
| 84 | + StreamUtil.TransferBytes(stream, baos); |
| 85 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 600, 100, true); |
| 86 | + stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "smpl.bmp")); |
| 87 | + baos = new ByteArrayOutputStream(); |
| 88 | + StreamUtil.TransferBytes(stream, baos); |
| 89 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 500, 100, true); |
| 90 | + stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "itext.png")); |
| 91 | + baos = new ByteArrayOutputStream(); |
| 92 | + StreamUtil.TransferBytes(stream, baos); |
| 93 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 460, 100, true); |
| 94 | + stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "0047478.jpg")); |
| 95 | + baos = new ByteArrayOutputStream(); |
| 96 | + StreamUtil.TransferBytes(stream, baos); |
| 97 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 300, 100, true); |
| 98 | + stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "map.jp2")); |
| 99 | + baos = new ByteArrayOutputStream(); |
| 100 | + StreamUtil.TransferBytes(stream, baos); |
| 101 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 200, 100, true); |
| 102 | + stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "amb.jb2")); |
| 103 | + baos = new ByteArrayOutputStream(); |
| 104 | + StreamUtil.TransferBytes(stream, baos); |
| 105 | + canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 30, 100, true); |
| 106 | + document.Close(); |
| 107 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder |
| 108 | + + "cmp_" + filename, destinationFolder)); |
| 109 | + } |
| 110 | + |
| 111 | + [NUnit.Framework.Test] |
| 112 | + public virtual void InlineImagesTest03() { |
| 113 | + String filename = "inlineImages03.pdf"; |
| 114 | + PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename, new WriterProperties(). |
| 115 | + SetPdfVersion(PdfVersion.PDF_2_0)).SetCompressionLevel(CompressionConstants.NO_COMPRESSION)); |
| 116 | + PdfPage page = document.AddNewPage(); |
| 117 | + PdfCanvas canvas = new PdfCanvas(page); |
| 118 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "bulb.gif"), 36, 600, 100, true); |
| 119 | + document.Close(); |
| 120 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder |
| 121 | + + "cmp_" + filename, destinationFolder)); |
| 122 | + } |
| 123 | + |
| 124 | + [NUnit.Framework.Test] |
| 125 | + public virtual void InlineImagesPngTest() { |
| 126 | + //TODO update cmp-files after DEVSIX-3564 will be fixed |
| 127 | + String filename = "inlineImagePng.pdf"; |
| 128 | + PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); |
| 129 | + PdfPage page = document.AddNewPage(); |
| 130 | + PdfCanvas canvas = new PdfCanvas(page); |
| 131 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "5.png"), 36, 700, 100, true); |
| 132 | + document.Close(); |
| 133 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder |
| 134 | + + "cmp_" + filename, destinationFolder)); |
| 135 | + } |
| 136 | + |
| 137 | + [NUnit.Framework.Test] |
| 138 | + public virtual void InlineImagesPngErrorWhileOpenTest() { |
| 139 | + //TODO update cmp-files after DEVSIX-3564 will be fixed |
| 140 | + String filename = "inlineImagePngErrorWhileOpen.pdf"; |
| 141 | + PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); |
| 142 | + PdfPage page = document.AddNewPage(); |
| 143 | + PdfCanvas canvas = new PdfCanvas(page); |
| 144 | + canvas.AddImage(ImageDataFactory.Create(sourceFolder + "3.png"), 36, 700, 100, true); |
| 145 | + document.Close(); |
| 146 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder |
| 147 | + + "cmp_" + filename, destinationFolder)); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments