Debian Codegraph @

sejda-sambox/src/test/java/org/sejda/impl/sambox/component/split/PageCopierTest.java

raw
1 /*
2 * Created on 27 giu 2016
3 * Copyright 2015 by Andrea Vacondio ([email protected]).
4 * This file is part of Sejda.
5 *
6 * Sejda is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Sejda is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Sejda. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.sejda.impl.sambox.component.split;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.util.Arrays;
30
31 import org.junit.Test;
32 import org.sejda.io.SeekableSources;
33 import org.sejda.sambox.cos.COSArray;
34 import org.sejda.sambox.cos.COSName;
35 import org.sejda.sambox.cos.COSStream;
36 import org.sejda.sambox.input.PDFParser;
37 import org.sejda.sambox.pdmodel.PDDocument;
38 import org.sejda.sambox.pdmodel.PDPage;
39 import org.sejda.sambox.pdmodel.interactive.action.PDActionGoTo;
40 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation;
41 import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink;
42 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDDestination;
43 import org.sejda.sambox.pdmodel.interactive.pagenavigation.PDThreadBead;
44
45 /**
46 * @author Andrea Vacondio
47 *
48 */
49 public class PageCopierTest {
50
51 @Test
52 public void existingPage() throws IOException {
53 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
54 getClass().getClassLoader().getResourceAsStream("pdf/shared_resource_dic_w_images.pdf")))) {
55 PDPage page = document.getPage(0);
56 PDPage copy = new PageCopier(false).copyOf(page);
57 PDPage optimizedCopy = new PageCopier(true).copyOf(page);
58 assertEquals(page.getMediaBox(), copy.getMediaBox());
59 assertEquals(page.getCropBox(), copy.getCropBox());
60 assertEquals(page.getRotation(), copy.getRotation());
61 assertEquals(page.getResources(), copy.getResources());
62 assertNotEquals(page.getResources(), optimizedCopy.getResources());
63 }
64 }
65
66 @Test
67 public void discardBeads() throws IOException {
68 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
69 getClass().getClassLoader().getResourceAsStream("pdf/shared_resource_dic_w_images.pdf")))) {
70 PDPage page = document.getPage(0);
71 page.setThreadBeads(Arrays.asList(new PDThreadBead()));
72 assertFalse(page.getThreadBeads().isEmpty());
73 PDPage copy = new PageCopier(false).copyOf(page);
74 assertTrue(copy.getThreadBeads().isEmpty());
75 }
76 }
77
78 @Test
79 public void removesParentAndPopup() throws IOException {
80 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
81 getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) {
82 PDPage page = document.getPage(0);
83 PDPage copy = new PageCopier(false).copyOf(page);
84 copy.getAnnotations().stream().map(PDAnnotation::getCOSObject).forEach(d -> {
85 assertFalse(d.containsKey(COSName.PARENT));
86 assertFalse(d.containsKey(COSName.getPDFName("Popup")));
87 });
88 }
89 }
90
91 @Test
92 public void pageWithAnnots() throws IOException {
93 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
94 getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_full_dic.pdf")))) {
95 PDPage page = document.getPage(0);
96 PDPage copy = new PageCopier(false).copyOf(page);
97 assertEquals(page.getCOSObject().getDictionaryObject(COSName.ANNOTS),
98 page.getCOSObject().getDictionaryObject(COSName.ANNOTS));
99 assertNotEquals(page.getCOSObject().getDictionaryObject(COSName.ANNOTS),
100 copy.getCOSObject().getDictionaryObject(COSName.ANNOTS));
101 copy.getAnnotations().stream().map(PDAnnotation::getCOSObject).forEach(d -> {
102 assertFalse(d.containsKey(COSName.P));
103 assertFalse(d.containsKey(COSName.DEST));
104 });
105 }
106 }
107
108 @Test
109 public void removeActionWithDestination() throws IOException {
110 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
111 getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_full_dic.pdf")))) {
112 PDPage page = document.getPage(0);
113 PDAnnotationLink link = new PDAnnotationLink();
114 PDActionGoTo action = new PDActionGoTo();
115 action.setDestination(PDDestination.create(COSName.C));
116 link.setAction(action);
117 page.setAnnotations(Arrays.asList(link));
118 PDPage copy = new PageCopier(false).copyOf(page);
119 assertEquals(page.getCOSObject().getDictionaryObject(COSName.ANNOTS),
120 page.getCOSObject().getDictionaryObject(COSName.ANNOTS));
121 assertNotEquals(page.getCOSObject().getDictionaryObject(COSName.ANNOTS),
122 copy.getCOSObject().getDictionaryObject(COSName.ANNOTS));
123 copy.getAnnotations().stream().map(PDAnnotation::getCOSObject).forEach(d -> {
124 assertFalse(d.containsKey(COSName.P));
125 assertFalse(d.containsKey(COSName.DEST));
126 assertFalse(d.containsKey(COSName.A));
127 });
128 }
129 }
130
131 @Test
132 public void doesntRemoveActionWithoutDestination() throws IOException {
133 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
134 getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_full_dic.pdf")))) {
135 PDPage page = document.getPage(0);
136 PDAnnotationLink link = new PDAnnotationLink();
137 PDActionGoTo action = new PDActionGoTo();
138 link.setAction(action);
139 page.setAnnotations(Arrays.asList(link));
140 PDPage copy = new PageCopier(false).copyOf(page);
141 assertEquals(page.getCOSObject().getDictionaryObject(COSName.ANNOTS),
142 page.getCOSObject().getDictionaryObject(COSName.ANNOTS));
143 assertNotEquals(page.getCOSObject().getDictionaryObject(COSName.ANNOTS),
144 copy.getCOSObject().getDictionaryObject(COSName.ANNOTS));
145 copy.getAnnotations().stream().map(PDAnnotation::getCOSObject).forEach(d -> {
146 assertFalse(d.containsKey(COSName.P));
147 assertFalse(d.containsKey(COSName.DEST));
148 assertTrue(d.containsKey(COSName.A));
149 });
150 }
151 }
152
153 @Test
154 public void copyStreamIsSanitized() throws IOException {
155 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
156 getClass().getClassLoader().getResourceAsStream("pdf/annots_in_stream.pdf")))) {
157 PDPage page = document.getPage(0);
158 assertNotNull(
159 page.getCOSObject().getDictionaryObject(COSName.CONTENTS, COSStream.class).getItem(COSName.ANNOTS));
160 PDPage copy = new PageCopier(false).copyOf(page);
161 assertNull(
162 copy.getCOSObject().getDictionaryObject(COSName.CONTENTS, COSStream.class).getItem(COSName.ANNOTS));
163 }
164 }
165
166 @Test
167 public void copyStreamInArrayIsSanitized() throws IOException {
168 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
169 getClass().getClassLoader().getResourceAsStream("pdf/annots_in_contents_array_stream.pdf")))) {
170 PDPage page = document.getPage(0);
171 assertNotNull(page.getCOSObject().getDictionaryObject(COSName.CONTENTS, COSArray.class)
172 .getObject(0, COSStream.class).getItem(COSName.ANNOTS));
173 PDPage copy = new PageCopier(false).copyOf(page);
174 assertNull(copy.getCOSObject().getDictionaryObject(COSName.CONTENTS, COSArray.class)
175 .getObject(0, COSStream.class).getItem(COSName.ANNOTS));
176 }
177 }
178
179 }