| 1 |
/* |
| 2 |
* Created on 17 set 2015 |
| 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; |
| 20 |
|
| 21 |
import static org.junit.Assert.assertEquals; |
| 22 |
import static org.junit.Assert.assertFalse; |
| 23 |
import static org.junit.Assert.assertNotNull; |
| 24 |
import static org.junit.Assert.assertNull; |
| 25 |
import static org.junit.Assert.assertTrue; |
| 26 |
|
| 27 |
import java.io.IOException; |
| 28 |
|
| 29 |
import org.junit.After; |
| 30 |
import org.junit.Before; |
| 31 |
import org.junit.Test; |
| 32 |
import org.sejda.commons.LookupTable; |
| 33 |
import org.sejda.commons.util.IOUtils; |
| 34 |
import org.sejda.io.SeekableSources; |
| 35 |
import org.sejda.model.pdf.form.AcroFormPolicy; |
| 36 |
import org.sejda.sambox.cos.COSName; |
| 37 |
import org.sejda.sambox.input.PDFParser; |
| 38 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 39 |
import org.sejda.sambox.pdmodel.PDPage; |
| 40 |
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation; |
| 41 |
import org.sejda.sambox.pdmodel.interactive.form.PDAcroForm; |
| 42 |
import org.sejda.sambox.pdmodel.interactive.form.PDField; |
| 43 |
import org.sejda.sambox.pdmodel.interactive.form.PDNonTerminalField; |
| 44 |
import org.sejda.sambox.pdmodel.interactive.form.PDRadioButton; |
| 45 |
import org.sejda.sambox.pdmodel.interactive.form.PDTerminalField; |
| 46 |
|
| 47 |
/** |
| 48 |
* @author Andrea Vacondio |
| 49 |
* |
| 50 |
*/ |
| 51 |
public class AcroFormsMergerTest { |
| 52 |
private PDDocument document; |
| 53 |
private LookupTable<PDPage> mapping = new LookupTable<>(); |
| 54 |
private LookupTable<PDAnnotation> annotationsLookup; |
| 55 |
|
| 56 |
@Before |
| 57 |
public void setUp() throws IOException { |
| 58 |
document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 59 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form.pdf"))); |
| 60 |
for (PDPage current : document.getPages()) { |
| 61 |
mapping.addLookupEntry(current, new PDPage()); |
| 62 |
annotationsLookup = new AnnotationsDistiller(document).retainRelevantAnnotations(mapping); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
@After |
| 67 |
public void tearDown() { |
| 68 |
IOUtils.closeQuietly(document); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void mergeNull() { |
| 73 |
PDDocument destination = new PDDocument(); |
| 74 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 75 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 76 |
victim.mergeForm(null, annotationsLookup); |
| 77 |
assertTrue(victim.getForm().getFields().isEmpty()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void mergeNoRelevant() { |
| 82 |
PDDocument destination = new PDDocument(); |
| 83 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 84 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 85 |
annotationsLookup.clear(); |
| 86 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 87 |
assertTrue(victim.getForm().getFields().isEmpty()); |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void mergeWithXFA() { |
| 92 |
PDDocument destination = new PDDocument(); |
| 93 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 94 |
PDAcroForm form = document.getDocumentCatalog().getAcroForm(); |
| 95 |
assertNotNull(form); |
| 96 |
form.getCOSObject().setString(COSName.XFA, "Something"); |
| 97 |
victim.mergeForm(form, annotationsLookup); |
| 98 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void discard() { |
| 103 |
PDDocument destination = new PDDocument(); |
| 104 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.DISCARD, destination); |
| 105 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 106 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 107 |
assertTrue(victim.getForm().getFields().isEmpty()); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void merge() { |
| 112 |
PDDocument destination = new PDDocument(); |
| 113 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 114 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 115 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 116 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 117 |
assertTrue(victim.getForm().getCalculationOrder().isEmpty()); |
| 118 |
assertNull(victim.getForm().getCOSObject().getItem(COSName.CO)); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void mergeWithHierarchy() throws IOException { |
| 123 |
PDDocument destination = new PDDocument(); |
| 124 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 125 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 126 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 127 |
mapping.clear(); |
| 128 |
annotationsLookup.clear(); |
| 129 |
|
| 130 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 131 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_hierarchy.pdf")))) { |
| 132 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 133 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 134 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 135 |
|
| 136 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 137 |
PDAcroForm form = victim.getForm(); |
| 138 |
assertNotNull(form.getField("node.leaf")); |
| 139 |
assertEquals(5, form.getFields().size()); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void mergeWithHierarchySameRootNodeName() throws IOException { |
| 145 |
PDDocument destination = new PDDocument(); |
| 146 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 147 |
mapping.clear(); |
| 148 |
annotationsLookup.clear(); |
| 149 |
|
| 150 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 151 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_hierarchy.pdf")))) { |
| 152 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 153 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 154 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 155 |
} |
| 156 |
mapping.clear(); |
| 157 |
annotationsLookup.clear(); |
| 158 |
|
| 159 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(getClass() |
| 160 |
.getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_hierarchy_same_root.pdf")))) { |
| 161 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 162 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 163 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 164 |
} |
| 165 |
|
| 166 |
PDAcroForm form = victim.getForm(); |
| 167 |
assertFalse(form.getFields().isEmpty()); |
| 168 |
assertNotNull(form.getField("node.leaf")); |
| 169 |
assertNotNull(form.getField("node.text")); |
| 170 |
assertNotNull(form.getField("node.anothertext")); |
| 171 |
assertEquals(1, form.getFields().size()); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void mergeRenamingWithHierarchy() throws IOException { |
| 176 |
PDDocument destination = new PDDocument(); |
| 177 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE_RENAMING_EXISTING_FIELDS, destination); |
| 178 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 179 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 180 |
mapping.clear(); |
| 181 |
annotationsLookup.clear(); |
| 182 |
|
| 183 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 184 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_hierarchy.pdf")))) { |
| 185 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 186 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 187 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 188 |
mapping.clear(); |
| 189 |
annotationsLookup.clear(); |
| 190 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 191 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 192 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 193 |
|
| 194 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 195 |
PDAcroForm form = victim.getForm(); |
| 196 |
assertNotNull(form.getField("node.leaf")); |
| 197 |
assertEquals(6, form.getFields().size()); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void mergeHalfFormWithAnnotations() throws IOException { |
| 203 |
PDDocument destination = new PDDocument(); |
| 204 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 205 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 206 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 207 |
mapping.clear(); |
| 208 |
annotationsLookup.clear(); |
| 209 |
|
| 210 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 211 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/two_pages_form.pdf")))) { |
| 212 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 213 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 214 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 215 |
|
| 216 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 217 |
assertNull(destination.getDocumentCatalog().getAcroForm()); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
@Test |
| 222 |
// AcroForm fields array is empty. Widget and Field dictionaries are merged and there is no fields hierarchy |
| 223 |
public void mergeOrphansFieldsMergedWidgetNoHierarchy() throws IOException { |
| 224 |
PDDocument destination = new PDDocument(); |
| 225 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 226 |
mapping.clear(); |
| 227 |
annotationsLookup.clear(); |
| 228 |
|
| 229 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(getClass() |
| 230 |
.getClassLoader().getResourceAsStream("pdf/forms/form_orphan_fields_merged_dic_no_hierarchy.pdf")))) { |
| 231 |
PDPage destPage = new PDPage(); |
| 232 |
mapping.addLookupEntry(anotherDoc.getPage(0), destPage); |
| 233 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 234 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 235 |
|
| 236 |
assertEquals(2, victim.getForm().getFields().size()); |
| 237 |
assertEquals(4, destPage.getAnnotations().size()); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
@Test |
| 242 |
// AcroForm fields array is empty. Widget is a kid of the Field dictionaries and there is no fields hierarchy |
| 243 |
public void mergeOrphansFieldsWithWidgetKidsNoHierarchy() throws IOException { |
| 244 |
PDDocument destination = new PDDocument(); |
| 245 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 246 |
mapping.clear(); |
| 247 |
annotationsLookup.clear(); |
| 248 |
|
| 249 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 250 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/form_orphan_fields_no_hierarchy.pdf")))) { |
| 251 |
PDPage destPage = new PDPage(); |
| 252 |
mapping.addLookupEntry(anotherDoc.getPage(0), destPage); |
| 253 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 254 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 255 |
|
| 256 |
assertEquals(2, victim.getForm().getFields().size()); |
| 257 |
assertEquals(4, destPage.getAnnotations().size()); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
// AcroForm fields array is empty. Widget is a kid of the Field dictionaries and there is a fields hierarchy |
| 263 |
public void mergeOrphansFieldsWithWidgetKidsWithHierarchy() throws IOException { |
| 264 |
PDDocument destination = new PDDocument(); |
| 265 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 266 |
mapping.clear(); |
| 267 |
annotationsLookup.clear(); |
| 268 |
|
| 269 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 270 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/form_orphan_fields_with_hierarchy.pdf")))) { |
| 271 |
PDPage destPage = new PDPage(); |
| 272 |
mapping.addLookupEntry(anotherDoc.getPage(0), destPage); |
| 273 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 274 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 275 |
|
| 276 |
assertEquals(1, victim.getForm().getFields().size()); |
| 277 |
PDField field = victim.getForm().getFields().get(0); |
| 278 |
assertFalse(field.isTerminal()); |
| 279 |
assertEquals(2, ((PDNonTerminalField) field).getChildren().size()); |
| 280 |
assertEquals(4, destPage.getAnnotations().size()); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
@Test |
| 285 |
// AcroForm fields array is empty. Widget and Field dictionaries are merged and there is a fields hierarchy |
| 286 |
public void mergeOrphansFieldsMergedWidgetWithHierarchy() throws IOException { |
| 287 |
PDDocument destination = new PDDocument(); |
| 288 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 289 |
mapping.clear(); |
| 290 |
annotationsLookup.clear(); |
| 291 |
|
| 292 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(getClass() |
| 293 |
.getClassLoader().getResourceAsStream("pdf/forms/form_orphan_fields_merged_dic_with_hierarchy.pdf")))) { |
| 294 |
PDPage destPage = new PDPage(); |
| 295 |
mapping.addLookupEntry(anotherDoc.getPage(0), destPage); |
| 296 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 297 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 298 |
|
| 299 |
assertEquals(1, victim.getForm().getFields().size()); |
| 300 |
PDField field = victim.getForm().getFields().get(0); |
| 301 |
assertFalse(field.isTerminal()); |
| 302 |
assertEquals(2, ((PDNonTerminalField) field).getChildren().size()); |
| 303 |
assertEquals(4, destPage.getAnnotations().size()); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
@Test |
| 308 |
public void mergeSame() throws IOException { |
| 309 |
PDDocument destination = new PDDocument(); |
| 310 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 311 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 312 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 313 |
mapping.clear(); |
| 314 |
annotationsLookup.clear(); |
| 315 |
|
| 316 |
PDDocument sameDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 317 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form.pdf"))); |
| 318 |
for (PDPage current : sameDoc.getPages()) { |
| 319 |
mapping.addLookupEntry(current, new PDPage()); |
| 320 |
annotationsLookup = new AnnotationsDistiller(sameDoc).retainRelevantAnnotations(mapping); |
| 321 |
} |
| 322 |
victim.mergeForm(sameDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 323 |
|
| 324 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 325 |
|
| 326 |
assertEquals(4, victim.getForm().getFields().size()); |
| 327 |
for (PDField field : victim.getForm().getFieldTree()) { |
| 328 |
if (field.isTerminal()) { |
| 329 |
// System.out.println(field.getFullyQualifiedName() + " " + field.getWidgets().size() + " " + field.getClass().getSimpleName()); |
| 330 |
if (field.getFullyQualifiedName().startsWith("Choice_Caption_")) { |
| 331 |
assertEquals(4, field.getWidgets().size()); |
| 332 |
} else { |
| 333 |
assertEquals(2, field.getWidgets().size()); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
assertNull(destination.getDocumentCatalog().getAcroForm()); |
| 338 |
} |
| 339 |
|
| 340 |
@Test |
| 341 |
public void mergeRenaming() throws IOException { |
| 342 |
PDDocument destination = new PDDocument(); |
| 343 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE_RENAMING_EXISTING_FIELDS, destination); |
| 344 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 345 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 346 |
mapping.clear(); |
| 347 |
annotationsLookup.clear(); |
| 348 |
|
| 349 |
PDDocument sameDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 350 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form.pdf"))); |
| 351 |
for (PDPage current : sameDoc.getPages()) { |
| 352 |
mapping.addLookupEntry(current, new PDPage()); |
| 353 |
annotationsLookup = new AnnotationsDistiller(sameDoc).retainRelevantAnnotations(mapping); |
| 354 |
} |
| 355 |
victim.mergeForm(sameDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 356 |
|
| 357 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 358 |
assertEquals(8, victim.getForm().getFields().size()); |
| 359 |
for (PDField field : victim.getForm().getFieldTree()) { |
| 360 |
if (field instanceof PDRadioButton) { |
| 361 |
assertEquals(field.getPartialName(), 2, ((PDTerminalField) field).getWidgets().size()); |
| 362 |
} else { |
| 363 |
assertEquals(1, ((PDTerminalField) field).getWidgets().size()); |
| 364 |
} |
| 365 |
} |
| 366 |
assertNull(destination.getDocumentCatalog().getAcroForm()); |
| 367 |
} |
| 368 |
|
| 369 |
@Test |
| 370 |
public void mergeWithSignatureRemovesSignatureValue() throws IOException { |
| 371 |
PDDocument destination = new PDDocument(); |
| 372 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 373 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 374 |
victim.mergeForm(document.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 375 |
mapping.clear(); |
| 376 |
annotationsLookup.clear(); |
| 377 |
|
| 378 |
PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 379 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_signature_signed.pdf"))); |
| 380 |
for (PDPage current : anotherDoc.getPages()) { |
| 381 |
mapping.addLookupEntry(current, new PDPage()); |
| 382 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 383 |
} |
| 384 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 385 |
|
| 386 |
assertFalse(victim.getForm().getFields().isEmpty()); |
| 387 |
|
| 388 |
PDAcroForm form = victim.getForm(); |
| 389 |
assertNotNull(form); |
| 390 |
PDField signature = null; |
| 391 |
for (PDField current : form.getFieldTree()) { |
| 392 |
if (current.getFieldType() == COSName.SIG.getName()) { |
| 393 |
signature = current; |
| 394 |
} |
| 395 |
} |
| 396 |
assertNotNull(signature); |
| 397 |
assertEquals("", signature.getValueAsString()); |
| 398 |
assertTrue(form.isSignaturesExist()); |
| 399 |
} |
| 400 |
|
| 401 |
@Test |
| 402 |
public void calculationOrderIsKept() throws IOException { |
| 403 |
PDDocument destination = new PDDocument(); |
| 404 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE, destination); |
| 405 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 406 |
mapping.clear(); |
| 407 |
|
| 408 |
try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 409 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/test_form_with_calc.pdf")))) { |
| 410 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 411 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 412 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 413 |
|
| 414 |
assertEquals(2, victim.getForm().getFields().size()); |
| 415 |
assertEquals(1, victim.getForm().getCalculationOrder().size()); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
@Test |
| 420 |
public void mergeRnamingSameCalculationOrderIsMerged() throws IOException { |
| 421 |
PDDocument destination = new PDDocument(); |
| 422 |
AcroFormsMerger victim = new AcroFormsMerger(AcroFormPolicy.MERGE_RENAMING_EXISTING_FIELDS, destination); |
| 423 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 424 |
assertNotNull(document.getDocumentCatalog().getAcroForm()); |
| 425 |
mapping.clear(); |
| 426 |
|
| 427 |
PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 428 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/test_form_with_calc.pdf"))); |
| 429 |
mapping.addLookupEntry(doc.getPage(0), new PDPage()); |
| 430 |
annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(mapping); |
| 431 |
victim.mergeForm(doc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 432 |
mapping.clear(); |
| 433 |
|
| 434 |
PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 435 |
getClass().getClassLoader().getResourceAsStream("pdf/forms/test_form_with_calc.pdf"))); |
| 436 |
mapping.addLookupEntry(anotherDoc.getPage(0), new PDPage()); |
| 437 |
annotationsLookup = new AnnotationsDistiller(anotherDoc).retainRelevantAnnotations(mapping); |
| 438 |
victim.mergeForm(anotherDoc.getDocumentCatalog().getAcroForm(), annotationsLookup); |
| 439 |
|
| 440 |
assertEquals(4, victim.getForm().getFields().size()); |
| 441 |
assertEquals(2, victim.getForm().getCalculationOrder().size()); |
| 442 |
} |
| 443 |
} |