|
| 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 | +package com.itextpdf.kernel.pdf.action; |
| 45 | + |
| 46 | +import com.itextpdf.io.LogMessageConstant; |
| 47 | +import com.itextpdf.kernel.PdfException; |
| 48 | +import com.itextpdf.kernel.geom.Rectangle; |
| 49 | +import com.itextpdf.kernel.pdf.PdfArray; |
| 50 | +import com.itextpdf.kernel.pdf.PdfDictionary; |
| 51 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 52 | +import com.itextpdf.kernel.pdf.PdfName; |
| 53 | +import com.itextpdf.kernel.pdf.PdfNumber; |
| 54 | +import com.itextpdf.kernel.pdf.PdfString; |
| 55 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 56 | +import com.itextpdf.kernel.pdf.annot.PdfAnnotation; |
| 57 | +import com.itextpdf.kernel.pdf.annot.PdfFileAttachmentAnnotation; |
| 58 | +import com.itextpdf.test.ExtendedITextTest; |
| 59 | +import com.itextpdf.test.annotations.LogMessage; |
| 60 | +import com.itextpdf.test.annotations.LogMessages; |
| 61 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 62 | + |
| 63 | +import java.io.ByteArrayOutputStream; |
| 64 | +import org.junit.Assert; |
| 65 | +import org.junit.Rule; |
| 66 | +import org.junit.Test; |
| 67 | +import org.junit.experimental.categories.Category; |
| 68 | +import org.junit.rules.ExpectedException; |
| 69 | + |
| 70 | +@Category(UnitTest.class) |
| 71 | +public class PdfTargetTest extends ExtendedITextTest { |
| 72 | + @Rule |
| 73 | + public ExpectedException junitExpectedException = ExpectedException.none(); |
| 74 | + |
| 75 | + @Test |
| 76 | + public void createInstanceTest() { |
| 77 | + PdfDictionary dictionary = new PdfDictionary(); |
| 78 | + PdfTarget target = PdfTarget.create(dictionary); |
| 79 | + |
| 80 | + Assert.assertEquals(dictionary, target.getPdfObject()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void createParentInstanceTest() { |
| 85 | + PdfTarget target = PdfTarget.createParentTarget(); |
| 86 | + PdfDictionary dictionary = target.getPdfObject(); |
| 87 | + Assert.assertEquals(PdfName.P, dictionary.get(PdfName.R)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void createChildInstanceTest() { |
| 92 | + PdfTarget target = PdfTarget.createChildTarget(); |
| 93 | + PdfDictionary dictionary = target.getPdfObject(); |
| 94 | + Assert.assertEquals(PdfName.C, dictionary.get(PdfName.R)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void createChildInstanceWithEmbeddedFileTest() { |
| 99 | + final String embeddedFileName = "EmbeddedFileName.file"; |
| 100 | + |
| 101 | + PdfTarget target = PdfTarget.createChildTarget(embeddedFileName); |
| 102 | + PdfDictionary dictionary = target.getPdfObject(); |
| 103 | + Assert.assertEquals(PdfName.C, dictionary.get(PdfName.R)); |
| 104 | + Assert.assertEquals(new PdfString(embeddedFileName), dictionary.get(PdfName.N)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void createChildInstanceWithNamedDestinationTest() { |
| 109 | + final String namedDestination = "namedDestination"; |
| 110 | + final String annotationIdentifier = "annotationIdentifier"; |
| 111 | + |
| 112 | + PdfTarget target = PdfTarget.createChildTarget(namedDestination, annotationIdentifier); |
| 113 | + |
| 114 | + PdfDictionary dictionary = target.getPdfObject(); |
| 115 | + Assert.assertEquals(PdfName.C, dictionary.get(PdfName.R)); |
| 116 | + Assert.assertEquals(new PdfString(namedDestination), dictionary.get(PdfName.P)); |
| 117 | + Assert.assertEquals(new PdfString(annotationIdentifier), dictionary.get(PdfName.A)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void createChildInstanceWithPageNumberTest() { |
| 122 | + final int pageNumber = 23; |
| 123 | + final int annotationIndex = 7; |
| 124 | + |
| 125 | + PdfTarget target = PdfTarget.createChildTarget(pageNumber, annotationIndex); |
| 126 | + |
| 127 | + PdfDictionary dictionary = target.getPdfObject(); |
| 128 | + Assert.assertEquals(PdfName.C, dictionary.get(PdfName.R)); |
| 129 | + Assert.assertEquals(new PdfNumber(pageNumber - 1), dictionary.get(PdfName.P)); |
| 130 | + Assert.assertEquals(new PdfNumber(annotationIndex), dictionary.get(PdfName.A)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void namePropertyTest() { |
| 135 | + final String name = "Name"; |
| 136 | + |
| 137 | + PdfTarget target = PdfTarget.create(new PdfDictionary()); |
| 138 | + target.setName(name); |
| 139 | + |
| 140 | + Assert.assertEquals(name, target.getName()); |
| 141 | + Assert.assertEquals(new PdfString(name), target.getPdfObject().get(PdfName.N)); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void targetPropertyTest() { |
| 147 | + |
| 148 | + final PdfDictionary oldDictionary = new PdfDictionary(); |
| 149 | + oldDictionary.put(new PdfName("Id"), new PdfString("Old")); |
| 150 | + |
| 151 | + final PdfDictionary newDictionary = new PdfDictionary(); |
| 152 | + newDictionary.put(new PdfName("Id"), new PdfString("New")); |
| 153 | + |
| 154 | + PdfTarget target = PdfTarget.create(oldDictionary); |
| 155 | + |
| 156 | + target.setTarget(PdfTarget.create(newDictionary)); |
| 157 | + Assert.assertEquals(newDictionary, target.getTarget().getPdfObject()); |
| 158 | + Assert.assertEquals(newDictionary, target.getPdfObject().get(PdfName.T)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void setAnnotationTest() { |
| 163 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 164 | + PdfFileAttachmentAnnotation annotation0 = new PdfFileAttachmentAnnotation( |
| 165 | + new Rectangle(0,0, 20, 20)); |
| 166 | + PdfFileAttachmentAnnotation annotation1 = new PdfFileAttachmentAnnotation( |
| 167 | + new Rectangle(1,1, 21, 21)); |
| 168 | + PdfFileAttachmentAnnotation annotation2 = new PdfFileAttachmentAnnotation( |
| 169 | + new Rectangle(2,2, 22, 22)); |
| 170 | + |
| 171 | + document.addNewPage(); |
| 172 | + document.getPage(1).addAnnotation(annotation0); |
| 173 | + document.getPage(1).addAnnotation(annotation1); |
| 174 | + document.getPage(1).addAnnotation(annotation2); |
| 175 | + |
| 176 | + PdfTarget target = PdfTarget.create(new PdfDictionary()); |
| 177 | + target.setAnnotation(annotation2, document); |
| 178 | + |
| 179 | + PdfDictionary dictionary = target.getPdfObject(); |
| 180 | + Assert.assertEquals(0, dictionary.getAsNumber(PdfName.P).intValue()); |
| 181 | + Assert.assertEquals(2, dictionary.getAsNumber(PdfName.A).intValue()); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void setAnnotationWhichIsMissedOnThePageTest() { |
| 187 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 188 | + PdfFileAttachmentAnnotation annotation0 = new PdfFileAttachmentAnnotation( |
| 189 | + new Rectangle(0,0, 20, 20)); |
| 190 | + PdfFileAttachmentAnnotation annotation1 = new PdfFileAttachmentAnnotation( |
| 191 | + new Rectangle(1,1, 21, 21)); |
| 192 | + PdfFileAttachmentAnnotation annotation2 = new PdfFileAttachmentAnnotation( |
| 193 | + new Rectangle(2,2, 22, 22)); |
| 194 | + |
| 195 | + document.addNewPage(); |
| 196 | + document.getPage(1).addAnnotation(annotation0); |
| 197 | + document.getPage(1).addAnnotation(annotation1); |
| 198 | + // The page doesn't know about the annotation |
| 199 | + annotation2.setPage(document.getPage(1)); |
| 200 | + |
| 201 | + PdfTarget target = PdfTarget.create(new PdfDictionary()); |
| 202 | + target.setAnnotation(annotation2, document); |
| 203 | + |
| 204 | + PdfDictionary dictionary = target.getPdfObject(); |
| 205 | + Assert.assertEquals(0, dictionary.getAsNumber(PdfName.P).intValue()); |
| 206 | + Assert.assertEquals(-1, dictionary.getAsNumber(PdfName.A).intValue()); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void setAnnotationWithoutPageTest() { |
| 212 | + junitExpectedException.expect(PdfException.class); |
| 213 | + junitExpectedException.expectMessage(PdfException.AnnotationShallHaveReferenceToPage); |
| 214 | + |
| 215 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 216 | + document.addNewPage(); |
| 217 | + |
| 218 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation( |
| 219 | + new Rectangle(0,0, 20, 20)); |
| 220 | + |
| 221 | + PdfTarget target = PdfTarget.create(new PdfDictionary()); |
| 222 | + target.setAnnotation(annotation, document); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + public void getAnnotationSetAsAnnotationTest() { |
| 228 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 229 | + |
| 230 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation( |
| 231 | + new Rectangle(0,0, 20, 20)); |
| 232 | + document.addNewPage(); |
| 233 | + document.getPage(1).addAnnotation(annotation); |
| 234 | + |
| 235 | + PdfDictionary content = new PdfDictionary(); |
| 236 | + content.put(new PdfName("Key"), new PdfString("Value")); |
| 237 | + |
| 238 | + PdfTarget target = PdfTarget.create(new PdfDictionary()); |
| 239 | + target.setAnnotation(annotation, document); |
| 240 | + |
| 241 | + Assert.assertEquals(annotation.getPdfObject(), target.getAnnotation(document).getPdfObject()); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + public void getAnnotationSetAsIntsTest() { |
| 247 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 248 | + final int pageNumber = 1; |
| 249 | + final int annotationIndex = 0; |
| 250 | + |
| 251 | + PdfTarget target = PdfTarget.createChildTarget(pageNumber, annotationIndex); |
| 252 | + |
| 253 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation( |
| 254 | + new Rectangle(0,0, 20, 20)); |
| 255 | + document.addNewPage(); |
| 256 | + document.getPage(1).addAnnotation(annotation); |
| 257 | + |
| 258 | + Assert.assertEquals(annotation.getPdfObject(), target.getAnnotation(document).getPdfObject()); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void getAnnotationSetAsStringTest() { |
| 264 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 265 | + final String namedDestination = "namedDestination"; |
| 266 | + final String annotationIdentifier = "annotationIdentifier"; |
| 267 | + |
| 268 | + PdfTarget target = PdfTarget.createChildTarget(namedDestination, annotationIdentifier); |
| 269 | + |
| 270 | + PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation( |
| 271 | + new Rectangle(0,0, 20, 20)); |
| 272 | + annotation.setName(new PdfString(annotationIdentifier)); |
| 273 | + |
| 274 | + document.addNewPage(); |
| 275 | + document.getPage(1).addAnnotation(annotation); |
| 276 | + document.getCatalog().getNameTree(PdfName.Dests).addEntry(namedDestination, |
| 277 | + new PdfArray(new PdfNumber(1))); |
| 278 | + |
| 279 | + PdfAnnotation retrievedAnnotation = target.getAnnotation(document); |
| 280 | + |
| 281 | + Assert.assertEquals(annotation.getPdfObject(), retrievedAnnotation.getPdfObject()); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + @LogMessages(messages = { |
| 287 | + @LogMessage(messageTemplate = LogMessageConstant.SOME_TARGET_FIELDS_ARE_NOT_SET_OR_INCORRECT) |
| 288 | + }) |
| 289 | + public void getAnnotationSetAsStringNotAvailableTest() { |
| 290 | + try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { |
| 291 | + final String namedDestination = "namedDestination"; |
| 292 | + final String annotationIdentifier = "annotationIdentifier"; |
| 293 | + |
| 294 | + PdfTarget target = PdfTarget.createChildTarget(namedDestination, annotationIdentifier); |
| 295 | + |
| 296 | + document.addNewPage(); |
| 297 | + document.getCatalog().getNameTree(PdfName.Dests).addEntry(namedDestination, |
| 298 | + new PdfArray(new PdfNumber(1))); |
| 299 | + PdfAnnotation annotation = target.getAnnotation(document); |
| 300 | + |
| 301 | + Assert.assertNull(annotation); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + @Test |
| 306 | + public void putTest() { |
| 307 | + final PdfName key1 = new PdfName("Key1"); |
| 308 | + final PdfName key2 = new PdfName("Key2"); |
| 309 | + final PdfDictionary dictionary = new PdfDictionary(); |
| 310 | + |
| 311 | + PdfTarget target = PdfTarget.create(dictionary); |
| 312 | + target.put(key1, new PdfNumber(23)) |
| 313 | + .put(key2, new PdfString("Hello, world!")); |
| 314 | + Assert.assertEquals(23, dictionary.getAsNumber(key1).intValue()); |
| 315 | + Assert.assertEquals("Hello, world!", dictionary.getAsString(key2).getValue()); |
| 316 | + } |
| 317 | + |
| 318 | + |
| 319 | + @Test |
| 320 | + public void isWrappedObjectMustBeIndirectTest() { |
| 321 | + PdfDictionary pdfObject = new PdfDictionary(); |
| 322 | + PdfTarget target = PdfTarget.create(pdfObject); |
| 323 | + |
| 324 | + Assert.assertFalse(target.isWrappedObjectMustBeIndirect()); |
| 325 | + } |
| 326 | +} |
0 commit comments