|
| 1 | +/* |
| 2 | +
|
| 3 | +This file is part of the iText (R) project. |
| 4 | +Copyright (c) 1998-2020 iText Group NV |
| 5 | +Authors: Bruno Lowagie, Paulo Soares, et al. |
| 6 | +
|
| 7 | +This program is free software; you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU Affero General Public License version 3 |
| 9 | +as published by the Free Software Foundation with the addition of the |
| 10 | +following permission added to Section 15 as permitted in Section 7(a): |
| 11 | +FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 12 | +ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 13 | +OF THIRD PARTY RIGHTS |
| 14 | +
|
| 15 | +This program is distributed in the hope that it will be useful, but |
| 16 | +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 | +or FITNESS FOR A PARTICULAR PURPOSE. |
| 18 | +See the GNU Affero General Public License for more details. |
| 19 | +You should have received a copy of the GNU Affero General Public License |
| 20 | +along with this program; if not, see http://www.gnu.org/licenses or write to |
| 21 | +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | +Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 23 | +http://itextpdf.com/terms-of-use/ |
| 24 | +
|
| 25 | +The interactive user interfaces in modified source and object code versions |
| 26 | +of this program must display Appropriate Legal Notices, as required under |
| 27 | +Section 5 of the GNU Affero General Public License. |
| 28 | +
|
| 29 | +In accordance with Section 7(b) of the GNU Affero General Public License, |
| 30 | +a covered work must retain the producer line in every PDF that is created |
| 31 | +or manipulated using iText. |
| 32 | +
|
| 33 | +You can be released from the requirements of the license by purchasing |
| 34 | +a commercial license. Buying such a license is mandatory as soon as you |
| 35 | +develop commercial activities involving the iText software without |
| 36 | +disclosing the source code of your own applications. |
| 37 | +These activities include: offering paid services to customers as an ASP, |
| 38 | +serving PDFs on the fly in a web application, shipping iText with a closed |
| 39 | +source product. |
| 40 | +
|
| 41 | +For more information, please contact iText Software Corp. at this |
| 42 | + |
| 43 | +*/ |
| 44 | +using System; |
| 45 | +using System.IO; |
| 46 | +using iText.Kernel; |
| 47 | +using iText.Kernel.Geom; |
| 48 | +using iText.Kernel.Pdf; |
| 49 | +using iText.Kernel.Pdf.Annot; |
| 50 | +using iText.Test; |
| 51 | +using iText.Test.Attributes; |
| 52 | + |
| 53 | +namespace iText.Kernel.Pdf.Action { |
| 54 | + public class PdfTargetTest : ExtendedITextTest { |
| 55 | + [NUnit.Framework.Test] |
| 56 | + public virtual void CreateInstanceTest() { |
| 57 | + PdfDictionary dictionary = new PdfDictionary(); |
| 58 | + PdfTarget target = PdfTarget.Create(dictionary); |
| 59 | + NUnit.Framework.Assert.AreEqual(dictionary, target.GetPdfObject()); |
| 60 | + } |
| 61 | + |
| 62 | + [NUnit.Framework.Test] |
| 63 | + public virtual void CreateParentInstanceTest() { |
| 64 | + PdfTarget target = PdfTarget.CreateParentTarget(); |
| 65 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 66 | + NUnit.Framework.Assert.AreEqual(PdfName.P, dictionary.Get(PdfName.R)); |
| 67 | + } |
| 68 | + |
| 69 | + [NUnit.Framework.Test] |
| 70 | + public virtual void CreateChildInstanceTest() { |
| 71 | + PdfTarget target = PdfTarget.CreateChildTarget(); |
| 72 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 73 | + NUnit.Framework.Assert.AreEqual(PdfName.C, dictionary.Get(PdfName.R)); |
| 74 | + } |
| 75 | + |
| 76 | + [NUnit.Framework.Test] |
| 77 | + public virtual void CreateChildInstanceWithEmbeddedFileTest() { |
| 78 | + String embeddedFileName = "EmbeddedFileName.file"; |
| 79 | + PdfTarget target = PdfTarget.CreateChildTarget(embeddedFileName); |
| 80 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 81 | + NUnit.Framework.Assert.AreEqual(PdfName.C, dictionary.Get(PdfName.R)); |
| 82 | + NUnit.Framework.Assert.AreEqual(new PdfString(embeddedFileName), dictionary.Get(PdfName.N)); |
| 83 | + } |
| 84 | + |
| 85 | + [NUnit.Framework.Test] |
| 86 | + public virtual void CreateChildInstanceWithNamedDestinationTest() { |
| 87 | + String namedDestination = "namedDestination"; |
| 88 | + String annotationIdentifier = "annotationIdentifier"; |
| 89 | + PdfTarget target = PdfTarget.CreateChildTarget(namedDestination, annotationIdentifier); |
| 90 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 91 | + NUnit.Framework.Assert.AreEqual(PdfName.C, dictionary.Get(PdfName.R)); |
| 92 | + NUnit.Framework.Assert.AreEqual(new PdfString(namedDestination), dictionary.Get(PdfName.P)); |
| 93 | + NUnit.Framework.Assert.AreEqual(new PdfString(annotationIdentifier), dictionary.Get(PdfName.A)); |
| 94 | + } |
| 95 | + |
| 96 | + [NUnit.Framework.Test] |
| 97 | + public virtual void CreateChildInstanceWithPageNumberTest() { |
| 98 | + int pageNumber = 23; |
| 99 | + int annotationIndex = 7; |
| 100 | + PdfTarget target = PdfTarget.CreateChildTarget(pageNumber, annotationIndex); |
| 101 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 102 | + NUnit.Framework.Assert.AreEqual(PdfName.C, dictionary.Get(PdfName.R)); |
| 103 | + NUnit.Framework.Assert.AreEqual(new PdfNumber(pageNumber - 1), dictionary.Get(PdfName.P)); |
| 104 | + NUnit.Framework.Assert.AreEqual(new PdfNumber(annotationIndex), dictionary.Get(PdfName.A)); |
| 105 | + } |
| 106 | + |
| 107 | + [NUnit.Framework.Test] |
| 108 | + public virtual void NamePropertyTest() { |
| 109 | + String name = "Name"; |
| 110 | + PdfTarget target = PdfTarget.Create(new PdfDictionary()); |
| 111 | + target.SetName(name); |
| 112 | + NUnit.Framework.Assert.AreEqual(name, target.GetName()); |
| 113 | + NUnit.Framework.Assert.AreEqual(new PdfString(name), target.GetPdfObject().Get(PdfName.N)); |
| 114 | + } |
| 115 | + |
| 116 | + [NUnit.Framework.Test] |
| 117 | + public virtual void TargetPropertyTest() { |
| 118 | + PdfDictionary oldDictionary = new PdfDictionary(); |
| 119 | + oldDictionary.Put(new PdfName("Id"), new PdfString("Old")); |
| 120 | + PdfDictionary newDictionary = new PdfDictionary(); |
| 121 | + newDictionary.Put(new PdfName("Id"), new PdfString("New")); |
| 122 | + PdfTarget target = PdfTarget.Create(oldDictionary); |
| 123 | + target.SetTarget(PdfTarget.Create(newDictionary)); |
| 124 | + NUnit.Framework.Assert.AreEqual(newDictionary, target.GetTarget().GetPdfObject()); |
| 125 | + NUnit.Framework.Assert.AreEqual(newDictionary, target.GetPdfObject().Get(PdfName.T)); |
| 126 | + } |
| 127 | + |
| 128 | + [NUnit.Framework.Test] |
| 129 | + public virtual void SetAnnotationTest() { |
| 130 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 131 | + PdfFileAttachmentAnnotation annotation0 = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20)); |
| 132 | + PdfFileAttachmentAnnotation annotation1 = new PdfFileAttachmentAnnotation(new Rectangle(1, 1, 21, 21)); |
| 133 | + PdfFileAttachmentAnnotation annotation2 = new PdfFileAttachmentAnnotation(new Rectangle(2, 2, 22, 22)); |
| 134 | + document.AddNewPage(); |
| 135 | + document.GetPage(1).AddAnnotation(annotation0); |
| 136 | + document.GetPage(1).AddAnnotation(annotation1); |
| 137 | + document.GetPage(1).AddAnnotation(annotation2); |
| 138 | + PdfTarget target = PdfTarget.Create(new PdfDictionary()); |
| 139 | + target.SetAnnotation(annotation2, document); |
| 140 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 141 | + NUnit.Framework.Assert.AreEqual(0, dictionary.GetAsNumber(PdfName.P).IntValue()); |
| 142 | + NUnit.Framework.Assert.AreEqual(2, dictionary.GetAsNumber(PdfName.A).IntValue()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + [NUnit.Framework.Test] |
| 147 | + public virtual void SetAnnotationWhichIsMissedOnThePageTest() { |
| 148 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 149 | + PdfFileAttachmentAnnotation annotation0 = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20)); |
| 150 | + PdfFileAttachmentAnnotation annotation1 = new PdfFileAttachmentAnnotation(new Rectangle(1, 1, 21, 21)); |
| 151 | + PdfFileAttachmentAnnotation annotation2 = new PdfFileAttachmentAnnotation(new Rectangle(2, 2, 22, 22)); |
| 152 | + document.AddNewPage(); |
| 153 | + document.GetPage(1).AddAnnotation(annotation0); |
| 154 | + document.GetPage(1).AddAnnotation(annotation1); |
| 155 | + // The page doesn't know about the annotation |
| 156 | + annotation2.SetPage(document.GetPage(1)); |
| 157 | + PdfTarget target = PdfTarget.Create(new PdfDictionary()); |
| 158 | + target.SetAnnotation(annotation2, document); |
| 159 | + PdfDictionary dictionary = target.GetPdfObject(); |
| 160 | + NUnit.Framework.Assert.AreEqual(0, dictionary.GetAsNumber(PdfName.P).IntValue()); |
| 161 | + NUnit.Framework.Assert.AreEqual(-1, dictionary.GetAsNumber(PdfName.A).IntValue()); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + [NUnit.Framework.Test] |
| 166 | + public virtual void SetAnnotationWithoutPageTest() { |
| 167 | + NUnit.Framework.Assert.That(() => { |
| 168 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 169 | + document.AddNewPage(); |
| 170 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20)); |
| 171 | + PdfTarget target = PdfTarget.Create(new PdfDictionary()); |
| 172 | + target.SetAnnotation(annotation, document); |
| 173 | + } |
| 174 | + } |
| 175 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo(PdfException.AnnotationShallHaveReferenceToPage)) |
| 176 | +; |
| 177 | + } |
| 178 | + |
| 179 | + [NUnit.Framework.Test] |
| 180 | + public virtual void GetAnnotationSetAsAnnotationTest() { |
| 181 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 182 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20)); |
| 183 | + document.AddNewPage(); |
| 184 | + document.GetPage(1).AddAnnotation(annotation); |
| 185 | + PdfDictionary content = new PdfDictionary(); |
| 186 | + content.Put(new PdfName("Key"), new PdfString("Value")); |
| 187 | + PdfTarget target = PdfTarget.Create(new PdfDictionary()); |
| 188 | + target.SetAnnotation(annotation, document); |
| 189 | + NUnit.Framework.Assert.AreEqual(annotation.GetPdfObject(), target.GetAnnotation(document).GetPdfObject()); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + [NUnit.Framework.Test] |
| 194 | + public virtual void GetAnnotationSetAsIntsTest() { |
| 195 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 196 | + int pageNumber = 1; |
| 197 | + int annotationIndex = 0; |
| 198 | + PdfTarget target = PdfTarget.CreateChildTarget(pageNumber, annotationIndex); |
| 199 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20)); |
| 200 | + document.AddNewPage(); |
| 201 | + document.GetPage(1).AddAnnotation(annotation); |
| 202 | + NUnit.Framework.Assert.AreEqual(annotation.GetPdfObject(), target.GetAnnotation(document).GetPdfObject()); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + [NUnit.Framework.Test] |
| 207 | + public virtual void GetAnnotationSetAsStringTest() { |
| 208 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 209 | + String namedDestination = "namedDestination"; |
| 210 | + String annotationIdentifier = "annotationIdentifier"; |
| 211 | + PdfTarget target = PdfTarget.CreateChildTarget(namedDestination, annotationIdentifier); |
| 212 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20)); |
| 213 | + annotation.SetName(new PdfString(annotationIdentifier)); |
| 214 | + document.AddNewPage(); |
| 215 | + document.GetPage(1).AddAnnotation(annotation); |
| 216 | + document.GetCatalog().GetNameTree(PdfName.Dests).AddEntry(namedDestination, new PdfArray(new PdfNumber(1)) |
| 217 | + ); |
| 218 | + PdfAnnotation retrievedAnnotation = target.GetAnnotation(document); |
| 219 | + NUnit.Framework.Assert.AreEqual(annotation.GetPdfObject(), retrievedAnnotation.GetPdfObject()); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + [NUnit.Framework.Test] |
| 224 | + [LogMessage(iText.IO.LogMessageConstant.SOME_TARGET_FIELDS_ARE_NOT_SET_OR_INCORRECT)] |
| 225 | + public virtual void GetAnnotationSetAsStringNotAvailableTest() { |
| 226 | + using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) { |
| 227 | + String namedDestination = "namedDestination"; |
| 228 | + String annotationIdentifier = "annotationIdentifier"; |
| 229 | + PdfTarget target = PdfTarget.CreateChildTarget(namedDestination, annotationIdentifier); |
| 230 | + document.AddNewPage(); |
| 231 | + document.GetCatalog().GetNameTree(PdfName.Dests).AddEntry(namedDestination, new PdfArray(new PdfNumber(1)) |
| 232 | + ); |
| 233 | + PdfAnnotation annotation = target.GetAnnotation(document); |
| 234 | + NUnit.Framework.Assert.IsNull(annotation); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + [NUnit.Framework.Test] |
| 239 | + public virtual void PutTest() { |
| 240 | + PdfName key1 = new PdfName("Key1"); |
| 241 | + PdfName key2 = new PdfName("Key2"); |
| 242 | + PdfDictionary dictionary = new PdfDictionary(); |
| 243 | + PdfTarget target = PdfTarget.Create(dictionary); |
| 244 | + target.Put(key1, new PdfNumber(23)).Put(key2, new PdfString("Hello, world!")); |
| 245 | + NUnit.Framework.Assert.AreEqual(23, dictionary.GetAsNumber(key1).IntValue()); |
| 246 | + NUnit.Framework.Assert.AreEqual("Hello, world!", dictionary.GetAsString(key2).GetValue()); |
| 247 | + } |
| 248 | + |
| 249 | + [NUnit.Framework.Test] |
| 250 | + public virtual void IsWrappedObjectMustBeIndirectTest() { |
| 251 | + PdfDictionary pdfObject = new PdfDictionary(); |
| 252 | + PdfTarget target = PdfTarget.Create(pdfObject); |
| 253 | + NUnit.Framework.Assert.IsFalse(target.IsWrappedObjectMustBeIndirect()); |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments