| 1 |
/* |
| 2 |
* Created on 04/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; |
| 20 |
|
| 21 |
import org.apache.commons.lang3.StringUtils; |
| 22 |
import org.junit.Test; |
| 23 |
import org.sejda.core.Sejda; |
| 24 |
import org.sejda.core.service.BaseTaskTest; |
| 25 |
import org.sejda.impl.sambox.component.DocBuilder; |
| 26 |
import org.sejda.impl.sambox.component.PdfTextExtractorByArea; |
| 27 |
import org.sejda.model.exception.InvalidTaskParametersException; |
| 28 |
import org.sejda.model.exception.TaskIOException; |
| 29 |
import org.sejda.model.input.ImageMergeInput; |
| 30 |
import org.sejda.model.input.MergeInput; |
| 31 |
import org.sejda.model.input.PdfMergeInput; |
| 32 |
import org.sejda.model.outline.CatalogPageLabelsPolicy; |
| 33 |
import org.sejda.model.outline.OutlinePolicy; |
| 34 |
import org.sejda.model.output.ExistingOutputPolicy; |
| 35 |
import org.sejda.model.parameter.MergeParameters; |
| 36 |
import org.sejda.model.pdf.PdfVersion; |
| 37 |
import org.sejda.model.pdf.form.AcroFormPolicy; |
| 38 |
import org.sejda.model.pdf.page.PageRange; |
| 39 |
import org.sejda.model.rotation.Rotation; |
| 40 |
import org.sejda.model.task.Task; |
| 41 |
import org.sejda.model.toc.ToCPolicy; |
| 42 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 43 |
import org.sejda.sambox.pdmodel.PDPage; |
| 44 |
import org.sejda.sambox.pdmodel.PageNotFoundException; |
| 45 |
import org.sejda.sambox.pdmodel.common.PDPageLabelRange; |
| 46 |
import org.sejda.sambox.pdmodel.common.PDPageLabels; |
| 47 |
import org.sejda.sambox.pdmodel.common.PDRectangle; |
| 48 |
import org.sejda.sambox.pdmodel.interactive.form.PDField; |
| 49 |
import org.sejda.sambox.pdmodel.interactive.form.PDTextField; |
| 50 |
import org.sejda.sambox.text.PDFTextStripperByArea; |
| 51 |
|
| 52 |
import java.awt.Rectangle; |
| 53 |
import java.io.IOException; |
| 54 |
import java.util.ArrayList; |
| 55 |
import java.util.Arrays; |
| 56 |
import java.util.Collections; |
| 57 |
import java.util.List; |
| 58 |
import java.util.function.Consumer; |
| 59 |
import java.util.stream.Collectors; |
| 60 |
|
| 61 |
import static org.hamcrest.CoreMatchers.is; |
| 62 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 63 |
import static org.hamcrest.Matchers.containsString; |
| 64 |
import static org.junit.Assert.assertEquals; |
| 65 |
import static org.junit.Assert.assertFalse; |
| 66 |
import static org.junit.Assert.assertNull; |
| 67 |
import static org.junit.Assert.assertTrue; |
| 68 |
import static org.junit.Assert.fail; |
| 69 |
import static org.sejda.TestUtils.encryptedAtRest; |
| 70 |
import static org.sejda.core.service.TestUtils.assertPageLabelIndexesAre; |
| 71 |
import static org.sejda.core.service.TestUtils.assertPageLabelRangeIs; |
| 72 |
|
| 73 |
/** |
| 74 |
* @author Andrea Vacondio |
| 75 |
* |
| 76 |
*/ |
| 77 |
public class MergeSamboxTaskTest extends BaseTaskTest<MergeParameters> { |
| 78 |
@Override |
| 79 |
public Task<MergeParameters> getTask() { |
| 80 |
return new MergeTask(); |
| 81 |
} |
| 82 |
|
| 83 |
private MergeParameters setUpParameters(List<? extends MergeInput> input) { |
| 84 |
MergeParameters parameters = new MergeParameters(); |
| 85 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 86 |
parameters.setCompress(false); |
| 87 |
parameters.setVersion(PdfVersion.VERSION_1_6); |
| 88 |
for (MergeInput current : input) { |
| 89 |
parameters.addInput(current); |
| 90 |
} |
| 91 |
parameters.setOutlinePolicy(OutlinePolicy.RETAIN); |
| 92 |
return parameters; |
| 93 |
} |
| 94 |
|
| 95 |
private List<PdfMergeInput> getInputWithOutline() { |
| 96 |
List<PdfMergeInput> input = new ArrayList<PdfMergeInput>(); |
| 97 |
input.add(new PdfMergeInput(largeOutlineInput())); |
| 98 |
input.add(new PdfMergeInput(largeInput())); |
| 99 |
return input; |
| 100 |
} |
| 101 |
|
| 102 |
private List<PdfMergeInput> getInputWithEncrypted() { |
| 103 |
List<PdfMergeInput> input = new ArrayList<PdfMergeInput>(); |
| 104 |
input.add(new PdfMergeInput(stronglyEncryptedInput())); |
| 105 |
input.add(new PdfMergeInput(largeInput())); |
| 106 |
return input; |
| 107 |
} |
| 108 |
|
| 109 |
private List<PdfMergeInput> getInput() { |
| 110 |
List<PdfMergeInput> input = new ArrayList<PdfMergeInput>(); |
| 111 |
input.add(new PdfMergeInput(regularInput())); |
| 112 |
input.add(new PdfMergeInput(customInput("pdf/attachments_as_annots.pdf", "attachments_as_annots.pdf"))); |
| 113 |
return input; |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void executeMergeAllWithOutlineRetainingOutline() throws IOException { |
| 118 |
doExecuteMergeAll(true, 311, setUpParameters(getInputWithOutline())); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void executeMergeAllWithEncryptedRetainingOutline() throws IOException { |
| 123 |
doExecuteMergeAll(true, 310, setUpParameters(getInputWithEncrypted())); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void executeMergeAllRetainingOutline() throws IOException { |
| 128 |
doExecuteMergeAll(false, 14, setUpParameters(getInput())); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void executeMergeAllRetainingOutlineTocNames() throws IOException { |
| 133 |
MergeParameters parameters = setUpParameters(getInput()); |
| 134 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf"))); |
| 135 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 136 |
doExecuteMergeAll(false, 19, parameters); |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void executeMergeAllRetainingOutlineTocNamesWhenNamesAreVeryLong() throws IOException { |
| 141 |
MergeParameters parameters = setUpParameters(getInput()); |
| 142 |
String longFilename = "This is a file that has a very long name and should not be truncated so that the version is visible at the end (but when applied to the footer the name should be truncated not to cover the page number) v7"; |
| 143 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf", longFilename + ".pdf"))); |
| 144 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 145 |
parameters.setFilenameFooter(true); |
| 146 |
doExecuteMergeAll(false, 19, parameters); |
| 147 |
|
| 148 |
testContext.forPdfOutput(d -> { |
| 149 |
assertPageTextContains(d.getPage(0), longFilename); |
| 150 |
assertFooterHasText(d.getPage(16), longFilename.substring(0, 124) + " 17"); |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void executeMergeRotatedTocPage() throws IOException { |
| 156 |
MergeParameters parameters = new MergeParameters(); |
| 157 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 158 |
parameters.setCompress(false); |
| 159 |
parameters.setVersion(PdfVersion.VERSION_1_6); |
| 160 |
parameters.setOutlinePolicy(OutlinePolicy.RETAIN); |
| 161 |
parameters.addInput(new PdfMergeInput(customInput("pdf/rotated_pages.pdf", "name.pdf"))); |
| 162 |
parameters.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES); |
| 163 |
testContext.pdfOutputTo(parameters); |
| 164 |
execute(parameters); |
| 165 |
testContext.assertTaskCompleted(); |
| 166 |
testContext.forPdfOutput(d -> { |
| 167 |
assertEquals(new PDRectangle(0, 0, 595, 842).rotate(90).toString(), d.getPage(0).getMediaBox().toString()); |
| 168 |
}); |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void executeMergeAllRetainingOutlineTocTitles() throws IOException { |
| 173 |
MergeParameters parameters = setUpParameters(getInput()); |
| 174 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf"))); |
| 175 |
parameters.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES); |
| 176 |
doExecuteMergeAll(false, 19, parameters); |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
public void executeMergeAllRetainingOutlineTocNamesUTF() throws IOException { |
| 181 |
MergeParameters parameters = setUpParameters(getInput()); |
| 182 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf", "αυτό είναι ένα τεστ.pdf"))); |
| 183 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 184 |
doExecuteMergeAll(false, 19, parameters); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void executeMergeAllRetainingOutlineTocNamesUTFThaiAndHindi() throws IOException { |
| 189 |
MergeParameters parameters = setUpParameters(getInput()); |
| 190 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf", "นี่คือการทดสอบ.pdf"))); |
| 191 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf", "यह एक परीक्षण है.pdf"))); |
| 192 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 193 |
parameters.setFilenameFooter(true); |
| 194 |
doExecuteMergeAll(false, 23, parameters); |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void executeMergeAllTocNamesNoFont() throws IOException { |
| 199 |
MergeParameters parameters = setUpParameters(getInput()); |
| 200 |
parameters.addInput(new PdfMergeInput(customInput("pdf/with_meta.pdf", "հայերէն.pdf"))); |
| 201 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 202 |
doExecuteMergeAll(false, 18, parameters); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void executeMergeAllWithOutlineDiscardingOutline() throws IOException { |
| 207 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 208 |
parameters.setOutlinePolicy(OutlinePolicy.DISCARD); |
| 209 |
doExecuteMergeAll(false, 311, parameters); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void executeMergeAllDiscardingOutline() throws IOException { |
| 214 |
MergeParameters parameters = setUpParameters(getInput()); |
| 215 |
parameters.setOutlinePolicy(OutlinePolicy.DISCARD); |
| 216 |
doExecuteMergeAll(false, 14, parameters); |
| 217 |
} |
| 218 |
|
| 219 |
@Test |
| 220 |
public void executeMergeAllWithEncryptedDiscardingOutline() throws IOException { |
| 221 |
MergeParameters parameters = setUpParameters(getInputWithEncrypted()); |
| 222 |
parameters.setOutlinePolicy(OutlinePolicy.DISCARD); |
| 223 |
doExecuteMergeAll(false, 310, parameters); |
| 224 |
} |
| 225 |
|
| 226 |
@Test |
| 227 |
public void executeMergeAllWithOutlineOnePerDoc() throws IOException { |
| 228 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 229 |
parameters.setOutlinePolicy(OutlinePolicy.ONE_ENTRY_EACH_DOC); |
| 230 |
doExecuteMergeAll(true, 311, parameters); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void executeMergeAllOnePerDoc() throws IOException { |
| 235 |
MergeParameters parameters = setUpParameters(getInput()); |
| 236 |
parameters.setOutlinePolicy(OutlinePolicy.ONE_ENTRY_EACH_DOC); |
| 237 |
doExecuteMergeAll(true, 14, parameters); |
| 238 |
} |
| 239 |
|
| 240 |
@Test |
| 241 |
public void executeMergeAllWithEncryptedOnePerDoc() throws IOException { |
| 242 |
MergeParameters parameters = setUpParameters(getInputWithEncrypted()); |
| 243 |
parameters.setOutlinePolicy(OutlinePolicy.ONE_ENTRY_EACH_DOC); |
| 244 |
doExecuteMergeAll(true, 310, parameters); |
| 245 |
} |
| 246 |
|
| 247 |
void doExecuteMergeAll(boolean hasBookmarks, int pages, MergeParameters parameters) throws IOException { |
| 248 |
testContext.pdfOutputTo(parameters); |
| 249 |
execute(parameters); |
| 250 |
testContext.assertTaskCompleted(); |
| 251 |
testContext.assertCreator().assertVersion(PdfVersion.VERSION_1_6).assertPages(pages) |
| 252 |
.assertHasOutline(hasBookmarks); |
| 253 |
} |
| 254 |
|
| 255 |
@Test |
| 256 |
public void testExecuteMergeAllFields() throws IOException { |
| 257 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 258 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form.pdf"))); |
| 259 |
parameters.setOutlinePolicy(OutlinePolicy.DISCARD); |
| 260 |
parameters.setAcroFormPolicy(AcroFormPolicy.MERGE); |
| 261 |
testContext.pdfOutputTo(parameters); |
| 262 |
execute(parameters); |
| 263 |
testContext.assertTaskCompleted(); |
| 264 |
testContext.assertCreator().assertPages(312).assertVersion(PdfVersion.VERSION_1_6).assertHasOutline(false) |
| 265 |
.assertHasAcroforms(true); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void testExecuteMergeFieldsWithDots() throws IOException { |
| 270 |
MergeParameters parameters = new MergeParameters(); |
| 271 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 272 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form_with_dot_partial_name.pdf"))); |
| 273 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form_with_dot_partial_name.pdf"))); |
| 274 |
parameters.setOutlinePolicy(OutlinePolicy.RETAIN); |
| 275 |
parameters.setAcroFormPolicy(AcroFormPolicy.MERGE_RENAMING_EXISTING_FIELDS); |
| 276 |
|
| 277 |
testContext.pdfOutputTo(parameters); |
| 278 |
execute(parameters); |
| 279 |
testContext.assertTaskCompleted(); |
| 280 |
testContext.assertHasAcroforms(true); |
| 281 |
testContext.forEachPdfOutput(new Consumer<PDDocument>() { |
| 282 |
@Override |
| 283 |
public void accept(PDDocument doc) { |
| 284 |
// original field |
| 285 |
assertEquals(1, findFieldsNamedExact("Choice_Caption_0wUBrGuJDKIWD9g7kWcKpg.withdot", doc).size()); |
| 286 |
// renamed field |
| 287 |
assertEquals(1, findFieldsMatching("Choice_Caption_0wUBrGuJDKIWD9g7kWcKpgwithdot", doc).size()); |
| 288 |
} |
| 289 |
}); |
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void testExecuteMergeFieldsWithSameNameDifferentKinds() throws IOException { |
| 294 |
MergeParameters parameters = new MergeParameters(); |
| 295 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 296 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/form_field_checkbox.pdf"))); |
| 297 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/form_field_text.pdf"))); |
| 298 |
parameters.setAcroFormPolicy(AcroFormPolicy.MERGE); |
| 299 |
|
| 300 |
testContext.pdfOutputTo(parameters); |
| 301 |
execute(parameters); |
| 302 |
testContext.assertTaskCompleted(); |
| 303 |
testContext.assertHasAcroforms(true); |
| 304 |
|
| 305 |
testContext.forEachPdfOutput(new Consumer<PDDocument>() { |
| 306 |
@Override |
| 307 |
public void accept(PDDocument doc) { |
| 308 |
// original field |
| 309 |
assertEquals(1, findFieldsNamedExact("form_field", doc).size()); |
| 310 |
// renamed field |
| 311 |
assertEquals(2, findFieldsMatching("form_field", doc).size()); |
| 312 |
} |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
@Test |
| 317 |
public void testExecuteMergeDiscardForms() throws IOException { |
| 318 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 319 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form.pdf"))); |
| 320 |
|
| 321 |
parameters.setOutlinePolicy(OutlinePolicy.DISCARD); |
| 322 |
parameters.setAcroFormPolicy(AcroFormPolicy.DISCARD); |
| 323 |
testContext.pdfOutputTo(parameters); |
| 324 |
execute(parameters); |
| 325 |
testContext.assertTaskCompleted(); |
| 326 |
testContext.assertCreator().assertPages(312).assertVersion(PdfVersion.VERSION_1_6).assertHasOutline(false) |
| 327 |
.assertHasAcroforms(false); |
| 328 |
} |
| 329 |
|
| 330 |
@Test |
| 331 |
public void testExecuteMergeFlattenForms() throws IOException { |
| 332 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 333 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form.pdf"))); |
| 334 |
|
| 335 |
parameters.setOutlinePolicy(OutlinePolicy.DISCARD); |
| 336 |
parameters.setAcroFormPolicy(AcroFormPolicy.FLATTEN); |
| 337 |
testContext.pdfOutputTo(parameters); |
| 338 |
execute(parameters); |
| 339 |
testContext.assertTaskCompleted(); |
| 340 |
testContext.assertCreator().assertPages(312).assertVersion(PdfVersion.VERSION_1_6).assertHasOutline(false) |
| 341 |
.assertHasAcroforms(false); |
| 342 |
} |
| 343 |
|
| 344 |
@Test |
| 345 |
public void testExecuteMergeFlattenFormsWithUnicodeValues() throws IOException { |
| 346 |
MergeParameters parameters = setUpParameters( |
| 347 |
Collections.singletonList(new PdfMergeInput(customInput("pdf/forms/simple_form_unicode_values.pdf")))); |
| 348 |
|
| 349 |
parameters.setAcroFormPolicy(AcroFormPolicy.FLATTEN); |
| 350 |
testContext.pdfOutputTo(parameters); |
| 351 |
execute(parameters); |
| 352 |
testContext.assertTaskCompleted(); |
| 353 |
testContext.assertPages(1).assertHasAcroforms(false); |
| 354 |
testContext.forEachPdfOutput(doc -> { |
| 355 |
assertPageTextContains(doc.getPage(0), "ጩ"); |
| 356 |
}); |
| 357 |
} |
| 358 |
|
| 359 |
@Test |
| 360 |
public void executeMergeRangesMergeForms() throws IOException { |
| 361 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 362 |
for (PdfMergeInput input : parameters.getPdfInputList()) { |
| 363 |
input.addPageRange(new PageRange(3, 10)); |
| 364 |
input.addPageRange(new PageRange(20, 23)); |
| 365 |
input.addPageRange(new PageRange(80, 90)); |
| 366 |
} |
| 367 |
parameters.setAcroFormPolicy(AcroFormPolicy.MERGE); |
| 368 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form.pdf"))); |
| 369 |
doExecuteMergeRanges(parameters); |
| 370 |
} |
| 371 |
|
| 372 |
@Test |
| 373 |
public void executeMergeRanges() throws IOException { |
| 374 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 375 |
for (PdfMergeInput input : parameters.getPdfInputList()) { |
| 376 |
input.addPageRange(new PageRange(3, 10)); |
| 377 |
input.addPageRange(new PageRange(20, 23)); |
| 378 |
input.addPageRange(new PageRange(80, 90)); |
| 379 |
} |
| 380 |
parameters.setAcroFormPolicy(AcroFormPolicy.DISCARD); |
| 381 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/simple_form.pdf"))); |
| 382 |
doExecuteMergeRanges(parameters); |
| 383 |
} |
| 384 |
|
| 385 |
public void doExecuteMergeRanges(MergeParameters parameters) throws IOException { |
| 386 |
testContext.pdfOutputTo(parameters); |
| 387 |
execute(parameters); |
| 388 |
testContext.assertTaskCompleted(); |
| 389 |
testContext.assertCreator().assertPages(27).assertVersion(PdfVersion.VERSION_1_6) |
| 390 |
.assertOutlineContains("Bookmark27").assertOutlineDoesntContain("Bookmark1"); |
| 391 |
} |
| 392 |
|
| 393 |
@Test |
| 394 |
public void testExecuteMergeRangesWithBlankPage() throws IOException { |
| 395 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 396 |
testContext.pdfOutputTo(parameters); |
| 397 |
for (PdfMergeInput input : parameters.getPdfInputList()) { |
| 398 |
input.addPageRange(new PageRange(2, 4)); |
| 399 |
} |
| 400 |
parameters.setBlankPageIfOdd(true); |
| 401 |
execute(parameters); |
| 402 |
PDDocument document = testContext.assertTaskCompleted(); |
| 403 |
testContext.assertCreator().assertPages(8).assertVersion(PdfVersion.VERSION_1_6); |
| 404 |
assertEquals(document.getPage(2).getCropBox().getWidth(), document.getPage(3).getCropBox().getWidth(), 0); |
| 405 |
assertEquals(document.getPage(2).getCropBox().getHeight(), document.getPage(3).getCropBox().getHeight(), 0); |
| 406 |
assertEquals(document.getPage(6).getCropBox().getWidth(), document.getPage(7).getCropBox().getWidth(), 0); |
| 407 |
assertEquals(document.getPage(6).getCropBox().getHeight(), document.getPage(7).getCropBox().getHeight(), 0); |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
@Test |
| 412 |
public void testExecuteMergeRangesWithBlankPagesAndToc() throws IOException { |
| 413 |
MergeParameters parameters = setUpParameters(getInputWithOutline()); |
| 414 |
testContext.pdfOutputTo(parameters); |
| 415 |
for (PdfMergeInput input : parameters.getPdfInputList()) { |
| 416 |
input.addPageRange(new PageRange(2, 4)); |
| 417 |
} |
| 418 |
parameters.setBlankPageIfOdd(true); |
| 419 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 420 |
execute(parameters); |
| 421 |
testContext.assertTaskCompleted(); |
| 422 |
testContext.assertCreator().assertPages(10); |
| 423 |
} |
| 424 |
|
| 425 |
@Test |
| 426 |
public void testExecuteMergeRangesWithFlattenForms() throws IOException { |
| 427 |
List<PdfMergeInput> inputs = new ArrayList<PdfMergeInput>(); |
| 428 |
inputs.add(new PdfMergeInput(customInput("pdf/forms/simple_form_with_values.pdf"))); |
| 429 |
MergeParameters parameters = setUpParameters(inputs); |
| 430 |
parameters.setAcroFormPolicy(AcroFormPolicy.FLATTEN); |
| 431 |
testContext.pdfOutputTo(parameters); |
| 432 |
execute(parameters); |
| 433 |
PDDocument document = testContext.assertTaskCompleted(); |
| 434 |
PDPage page = document.getPage(0); |
| 435 |
PDFTextStripperByArea stripper = new PDFTextStripperByArea(); |
| 436 |
stripper.addRegion("completePage", |
| 437 |
new Rectangle((int) page.getCropBox().getWidth(), (int) page.getCropBox().getHeight())); |
| 438 |
stripper.extractRegions(page); |
| 439 |
String pageText = stripper.getTextForRegion("completePage"); |
| 440 |
|
| 441 |
assertThat(pageText, containsString("TextFieldValue")); |
| 442 |
} |
| 443 |
|
| 444 |
@Test |
| 445 |
public void executeMergeFieldsWithSameNamesDifferentValues() throws IOException { |
| 446 |
MergeParameters parameters = new MergeParameters(); |
| 447 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 448 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/test_form_one.pdf"))); |
| 449 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/test_form_two.pdf"))); |
| 450 |
parameters.setAcroFormPolicy(AcroFormPolicy.MERGE); |
| 451 |
|
| 452 |
testContext.pdfOutputTo(parameters); |
| 453 |
execute(parameters); |
| 454 |
testContext.assertTaskCompleted(); |
| 455 |
testContext.assertHasAcroforms(true); |
| 456 |
|
| 457 |
testContext.forEachPdfOutput(new Consumer<PDDocument>() { |
| 458 |
@Override |
| 459 |
public void accept(PDDocument doc) { |
| 460 |
assertEquals(1, findFieldsNamedExact("text_field", doc).size()); |
| 461 |
assertEquals(2, findFieldsMatching("text_field", doc).size()); |
| 462 |
|
| 463 |
// 1 widget each |
| 464 |
assertEquals(Arrays.asList(1, 1), findFieldsMatching("text_field", doc).stream() |
| 465 |
.map(f -> f.getWidgets().size()).collect(Collectors.toList())); |
| 466 |
} |
| 467 |
}); |
| 468 |
} |
| 469 |
|
| 470 |
@Test |
| 471 |
public void executeMergeFieldsWithSameNamesSameValues() throws IOException { |
| 472 |
MergeParameters parameters = new MergeParameters(); |
| 473 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 474 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/test_form_one.pdf"))); |
| 475 |
parameters.addInput(new PdfMergeInput(customInput("pdf/forms/test_form_one.pdf"))); |
| 476 |
parameters.setAcroFormPolicy(AcroFormPolicy.MERGE); |
| 477 |
|
| 478 |
testContext.pdfOutputTo(parameters); |
| 479 |
execute(parameters); |
| 480 |
testContext.assertTaskCompleted(); |
| 481 |
testContext.assertHasAcroforms(true); |
| 482 |
|
| 483 |
testContext.forEachPdfOutput(new Consumer<PDDocument>() { |
| 484 |
@Override |
| 485 |
public void accept(PDDocument doc) { |
| 486 |
assertEquals(1, findFieldsNamedExact("text_field", doc).size()); |
| 487 |
assertEquals(1, findFieldsMatching("text_field", doc).size()); |
| 488 |
|
| 489 |
// 2 widgets, 1 field |
| 490 |
assertEquals(Arrays.asList(2), findFieldsMatching("text_field", doc).stream() |
| 491 |
.map(f -> f.getWidgets().size()).collect(Collectors.toList())); |
| 492 |
} |
| 493 |
}); |
| 494 |
} |
| 495 |
|
| 496 |
@Test |
| 497 |
public void executeMergeMissingPageNonLenient() throws IOException { |
| 498 |
MergeParameters parameters = new MergeParameters(); |
| 499 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 500 |
parameters.setCompress(false); |
| 501 |
parameters.setVersion(PdfVersion.VERSION_1_6); |
| 502 |
parameters.setOutlinePolicy(OutlinePolicy.RETAIN); |
| 503 |
parameters.addInput(new PdfMergeInput(customInput("pdf/missing_page_ref.pdf", "name.pdf"))); |
| 504 |
testContext.pdfOutputTo(parameters); |
| 505 |
execute(parameters); |
| 506 |
testContext.assertTaskFailed().assertFailedSource("name.pdf"); |
| 507 |
} |
| 508 |
|
| 509 |
@Test |
| 510 |
public void executeMergeMissingPageLenient() throws IOException { |
| 511 |
MergeParameters parameters = new MergeParameters(); |
| 512 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 513 |
parameters.setCompress(false); |
| 514 |
parameters.setLenient(true); |
| 515 |
parameters.setVersion(PdfVersion.VERSION_1_6); |
| 516 |
parameters.setOutlinePolicy(OutlinePolicy.RETAIN); |
| 517 |
parameters.addInput(new PdfMergeInput(customInput("pdf/missing_page_ref.pdf", "name.pdf"))); |
| 518 |
testContext.pdfOutputTo(parameters); |
| 519 |
execute(parameters); |
| 520 |
testContext.assertTaskCompleted(); |
| 521 |
testContext.assertCreator().assertPages(3).assertVersion(PdfVersion.VERSION_1_6); |
| 522 |
} |
| 523 |
|
| 524 |
@Test(expected = PageNotFoundException.class) |
| 525 |
public void executeMergeMissingPageEagerAssertions() throws IOException { |
| 526 |
try { |
| 527 |
System.setProperty(Sejda.PERFORM_EAGER_ASSERTIONS_PROPERTY_NAME, "true"); |
| 528 |
|
| 529 |
MergeParameters parameters = new MergeParameters(); |
| 530 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 531 |
parameters.addInput(new PdfMergeInput(customInput("pdf/missing_page_ref.pdf", "name.pdf"))); |
| 532 |
testContext.pdfOutputTo(parameters); |
| 533 |
execute(parameters); |
| 534 |
testContext.assertTaskFailed().assertFailedSource("name.pdf"); |
| 535 |
} finally { |
| 536 |
System.setProperty(Sejda.PERFORM_EAGER_ASSERTIONS_PROPERTY_NAME, ""); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
@Test |
| 541 |
public void normalizePageSizes_FirstPagePortrait() throws IOException { |
| 542 |
MergeParameters parameters = new MergeParameters(); |
| 543 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 544 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A4Portrait.pdf"))); |
| 545 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A3Landscape.pdf"))); |
| 546 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A3Portrait.pdf"))); |
| 547 |
parameters.addInput(new PdfMergeInput(customInput("pdf/landscape_by_rotation.pdf"))); |
| 548 |
parameters.addInput(new PdfMergeInput(customInput("pdf/potrait_by_rotation.pdf"))); |
| 549 |
parameters.setNormalizePageSizes(true); |
| 550 |
|
| 551 |
testContext.pdfOutputTo(parameters); |
| 552 |
execute(parameters); |
| 553 |
|
| 554 |
testContext.assertTaskCompleted(); |
| 555 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 556 |
assertEquals(595, widthOfCropBox(d.getPage(0)), 1); |
| 557 |
|
| 558 |
// landscape should be handled in a special case |
| 559 |
assertEquals(595, heightOfCropBox(d.getPage(1)), 1); |
| 560 |
assertEquals(840, widthOfCropBox(d.getPage(1)), 1); |
| 561 |
|
| 562 |
assertEquals(595, widthOfCropBox(d.getPage(2)), 1); |
| 563 |
|
| 564 |
assertEquals(840, widthOfCropBox(d.getPage(3)), 1); |
| 565 |
assertEquals(595, widthOfCropBox(d.getPage(4)), 1); |
| 566 |
}); |
| 567 |
} |
| 568 |
|
| 569 |
@Test |
| 570 |
public void normalizePageSizes_FirstPageLandscape() throws IOException { |
| 571 |
MergeParameters parameters = new MergeParameters(); |
| 572 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 573 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A3Landscape.pdf"))); |
| 574 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A4Portrait.pdf"))); |
| 575 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A3Portrait.pdf"))); |
| 576 |
parameters.addInput(new PdfMergeInput(customInput("pdf/landscape_by_rotation.pdf"))); |
| 577 |
parameters.addInput(new PdfMergeInput(customInput("pdf/potrait_by_rotation.pdf"))); |
| 578 |
parameters.setNormalizePageSizes(true); |
| 579 |
|
| 580 |
testContext.pdfOutputTo(parameters); |
| 581 |
execute(parameters); |
| 582 |
|
| 583 |
testContext.assertTaskCompleted(); |
| 584 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 585 |
assertEquals(1190, widthOfCropBox(d.getPage(0)), 1); |
| 586 |
|
| 587 |
// landscape should be handled in a special case |
| 588 |
assertEquals(1190, heightOfCropBox(d.getPage(1)), 1); |
| 589 |
assertEquals(841, widthOfCropBox(d.getPage(1)), 1); |
| 590 |
|
| 591 |
assertEquals(841, widthOfCropBox(d.getPage(2)), 1); |
| 592 |
|
| 593 |
assertEquals(1190, widthOfCropBox(d.getPage(3)), 1); |
| 594 |
assertEquals(841, widthOfCropBox(d.getPage(4)), 1); |
| 595 |
}); |
| 596 |
} |
| 597 |
|
| 598 |
@Test |
| 599 |
public void normalizePageSizes_AllLandscape() throws IOException { |
| 600 |
MergeParameters parameters = new MergeParameters(); |
| 601 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 602 |
parameters.addInput(new PdfMergeInput(customInput("pdf/A3Landscape.pdf"))); |
| 603 |
parameters.addInput(new PdfMergeInput(customInput("pdf/landscape_by_rotation.pdf"))); |
| 604 |
parameters.setNormalizePageSizes(true); |
| 605 |
|
| 606 |
testContext.pdfOutputTo(parameters); |
| 607 |
execute(parameters); |
| 608 |
|
| 609 |
testContext.assertTaskCompleted(); |
| 610 |
testContext.assertPages(2).forEachPdfOutput(d -> { |
| 611 |
assertEquals(1190, widthOfCropBox(d.getPage(0)), 1); |
| 612 |
assertEquals(1190, widthOfCropBox(d.getPage(1)), 1); |
| 613 |
}); |
| 614 |
} |
| 615 |
|
| 616 |
@Test |
| 617 |
public void pageFooter() throws IOException { |
| 618 |
MergeParameters parameters = setUpParameters(getInput()); |
| 619 |
parameters.setTableOfContentsPolicy(ToCPolicy.NONE); |
| 620 |
parameters.setFilenameFooter(true); |
| 621 |
testContext.pdfOutputTo(parameters); |
| 622 |
execute(parameters); |
| 623 |
testContext.assertTaskCompleted(); |
| 624 |
testContext.assertPages(14).forEachPdfOutput(d -> { |
| 625 |
assertFooterHasText(d.getPage(0), "test-file 1"); |
| 626 |
assertFooterHasText(d.getPage(11), "attachments_as_annots 12"); |
| 627 |
}); |
| 628 |
} |
| 629 |
|
| 630 |
@Test |
| 631 |
public void pageFooterAndToc() throws IOException { |
| 632 |
MergeParameters parameters = setUpParameters(getInput()); |
| 633 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 634 |
parameters.setFilenameFooter(true); |
| 635 |
testContext.pdfOutputTo(parameters); |
| 636 |
execute(parameters); |
| 637 |
testContext.assertTaskCompleted(); |
| 638 |
testContext.assertPages(15).forEachPdfOutput(d -> { |
| 639 |
assertFooterHasText(d.getPage(1), "test-file 2"); |
| 640 |
assertFooterHasText(d.getPage(12), "attachments_as_annots 13"); |
| 641 |
}); |
| 642 |
} |
| 643 |
|
| 644 |
@Test |
| 645 |
public void pageFooterAndTocwithDocTitles() throws IOException { |
| 646 |
List<PdfMergeInput> input = new ArrayList<PdfMergeInput>(); |
| 647 |
input.add(new PdfMergeInput(regularInput())); |
| 648 |
input.add(new PdfMergeInput(customInput("pdf/test_file.pdf", "my_file.pdf"))); |
| 649 |
MergeParameters parameters = setUpParameters(input); |
| 650 |
parameters.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES); |
| 651 |
parameters.setFilenameFooter(true); |
| 652 |
testContext.pdfOutputTo(parameters); |
| 653 |
execute(parameters); |
| 654 |
testContext.assertTaskCompleted(); |
| 655 |
testContext.assertPages(16).forEachPdfOutput(d -> { |
| 656 |
assertFooterHasText(d.getPage(1), "test-file 2"); |
| 657 |
assertFooterHasText(d.getPage(12), "my_file 13"); |
| 658 |
}); |
| 659 |
} |
| 660 |
|
| 661 |
@Test |
| 662 |
public void pageFooterAndTocAddBlank() throws IOException { |
| 663 |
MergeParameters parameters = setUpParameters(getInput()); |
| 664 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 665 |
parameters.setFilenameFooter(true); |
| 666 |
parameters.setBlankPageIfOdd(true); |
| 667 |
testContext.pdfOutputTo(parameters); |
| 668 |
execute(parameters); |
| 669 |
testContext.assertTaskCompleted(); |
| 670 |
testContext.assertPages(18).forEachPdfOutput(d -> { |
| 671 |
try { |
| 672 |
assertFalse(isBlankPage(d.getPage(0))); |
| 673 |
assertTrue(isBlankPage(d.getPage(1))); |
| 674 |
assertFooterHasText(d.getPage(2), "test-file 3"); |
| 675 |
assertFooterHasText(d.getPage(15), "attachments_as_annots 16"); |
| 676 |
} catch (TaskIOException e) { |
| 677 |
fail(e.getMessage()); |
| 678 |
} |
| 679 |
}); |
| 680 |
} |
| 681 |
|
| 682 |
@Test |
| 683 |
public void mergeImagesAndPdfs() throws IOException { |
| 684 |
MergeParameters parameters = new MergeParameters(); |
| 685 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 686 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png"))); |
| 687 |
parameters.addInput(new PdfMergeInput(customInput("pdf/test-pdf.pdf"))); |
| 688 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png"))); |
| 689 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/large.jpg"))); |
| 690 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.tiff"))); |
| 691 |
parameters.addInput(new PdfMergeInput(customInput("pdf/test-pdf.pdf"))); |
| 692 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png"))); |
| 693 |
|
| 694 |
testContext.pdfOutputTo(parameters); |
| 695 |
execute(parameters); |
| 696 |
|
| 697 |
testContext.assertTaskCompleted(); |
| 698 |
testContext.assertPages(1 + 11 + 3 + 11 + 1).forEachPdfOutput(d -> { |
| 699 |
assertEquals(Arrays.asList(1, 13, 14, 15, 27), getPagesContainingImages(d)); |
| 700 |
}); |
| 701 |
} |
| 702 |
|
| 703 |
@Test |
| 704 |
public void mergeImagesAndPdfsWithSpecificPageSize() throws IOException { |
| 705 |
MergeParameters parameters = new MergeParameters(); |
| 706 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 707 |
|
| 708 |
ImageMergeInput image = new ImageMergeInput(customNonPdfInput("image/draft.png")); |
| 709 |
image.setShouldPageSizeMatchImageSize(true); |
| 710 |
|
| 711 |
parameters.addInput(image); |
| 712 |
parameters.addInput(new PdfMergeInput(customInput("pdf/test-pdf.pdf"))); |
| 713 |
|
| 714 |
testContext.pdfOutputTo(parameters); |
| 715 |
execute(parameters); |
| 716 |
|
| 717 |
testContext.assertTaskCompleted(); |
| 718 |
testContext.forEachPdfOutput(d -> { |
| 719 |
assertEquals(d.getPage(0).getMediaBox().getWidth(), 248, 0.0); |
| 720 |
}); |
| 721 |
} |
| 722 |
|
| 723 |
@Test |
| 724 |
public void mergeImagesWithTocAndFooter() throws IOException { |
| 725 |
MergeParameters parameters = new MergeParameters(); |
| 726 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 727 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.png"))); |
| 728 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/large.jpg", "large.jpg"))); |
| 729 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.tiff", "draft.tiff"))); |
| 730 |
|
| 731 |
parameters.setTableOfContentsPolicy(ToCPolicy.DOC_TITLES); |
| 732 |
parameters.setFilenameFooter(true); |
| 733 |
|
| 734 |
testContext.pdfOutputTo(parameters); |
| 735 |
execute(parameters); |
| 736 |
|
| 737 |
testContext.assertTaskCompleted(); |
| 738 |
testContext.assertPages(1 + 3).forEachPdfOutput(d -> { |
| 739 |
assertPageText(d.getPage(0), "draft2large3draft4"); |
| 740 |
|
| 741 |
assertFooterHasText(d.getPage(1), "draft 2"); |
| 742 |
assertFooterHasText(d.getPage(2), "large 3"); |
| 743 |
assertFooterHasText(d.getPage(3), "draft 4"); |
| 744 |
}); |
| 745 |
} |
| 746 |
|
| 747 |
@Test |
| 748 |
public void mergeImagesWithWrongFileExtension() throws IOException { |
| 749 |
MergeParameters parameters = new MergeParameters(); |
| 750 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 751 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/large.jpg", "large.png"))); |
| 752 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.jpeg"))); |
| 753 |
|
| 754 |
testContext.pdfOutputTo(parameters); |
| 755 |
execute(parameters); |
| 756 |
|
| 757 |
testContext.assertTaskCompleted(); |
| 758 |
testContext.assertPages(2); |
| 759 |
} |
| 760 |
|
| 761 |
@Test |
| 762 |
public void mergeWithUnreadableImageThows() throws IOException { |
| 763 |
MergeParameters parameters = new MergeParameters(); |
| 764 |
parameters.setLenient(false); |
| 765 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 766 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.png"))); |
| 767 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/corrupt.png", "corrupt.png"))); |
| 768 |
|
| 769 |
testContext.pdfOutputTo(parameters); |
| 770 |
execute(parameters); |
| 771 |
|
| 772 |
// TODO: friendlier error message |
| 773 |
testContext.assertTaskFailed("An error occurred creating PDImageXObject from file source: corrupt.png") |
| 774 |
.assertFailedSource("corrupt.png"); |
| 775 |
} |
| 776 |
|
| 777 |
@Test |
| 778 |
public void mergeImagesWithValidation() throws IOException { |
| 779 |
MergeParameters parameters = new MergeParameters(); |
| 780 |
parameters.setLenient(false); |
| 781 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 782 |
ImageMergeInput input1 = new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.png")); |
| 783 |
input1.setPageSize(null); |
| 784 |
parameters.addInput(input1); |
| 785 |
|
| 786 |
testContext.pdfOutputTo(parameters); |
| 787 |
executeWithValidation(parameters); |
| 788 |
testContext.assertTaskFailed(InvalidTaskParametersException.class); |
| 789 |
} |
| 790 |
|
| 791 |
@Test |
| 792 |
public void mergeWithUnreadableImageWarning() throws IOException { |
| 793 |
MergeParameters parameters = new MergeParameters(); |
| 794 |
parameters.setLenient(true); |
| 795 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 796 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.png"))); |
| 797 |
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/corrupt.png", "corrupt.png"))); |
| 798 |
|
| 799 |
testContext.pdfOutputTo(parameters); |
| 800 |
execute(parameters); |
| 801 |
|
| 802 |
testContext.assertTaskCompleted(); |
| 803 |
testContext.assertPages(1); |
| 804 |
testContext.assertTaskWarning("Image corrupt.png was skipped, could not be processed"); |
| 805 |
} |
| 806 |
|
| 807 |
@Test |
| 808 |
public void mergeKeepingPageLabels() throws IOException { |
| 809 |
MergeParameters parameters = new MergeParameters(); |
| 810 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 811 |
PDDocument doc1 = new DocBuilder().withPages(2).withPageLabelRange(0, "r", "Intro:").get(); |
| 812 |
PDDocument doc2 = new DocBuilder().withPages(3).withPageLabelRange(0, "D").get(); |
| 813 |
parameters.addInput(new PdfMergeInput(customInput(doc1, "doc1.pdf"))); |
| 814 |
parameters.addInput(new PdfMergeInput(customInput(doc2, "doc2.pdf"))); |
| 815 |
parameters.setCatalogPageLabelsPolicy(CatalogPageLabelsPolicy.RETAIN); |
| 816 |
testContext.pdfOutputTo(parameters); |
| 817 |
execute(parameters); |
| 818 |
|
| 819 |
testContext.assertTaskCompleted(); |
| 820 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 821 |
try { |
| 822 |
PDPageLabels mergedLabels = d.getDocumentCatalog().getPageLabels(); |
| 823 |
assertPageLabelIndexesAre(mergedLabels, 0, 2); |
| 824 |
assertPageLabelRangeIs(mergedLabels, 0, new PDPageLabelRange("r", "Intro:", null)); |
| 825 |
assertPageLabelRangeIs(mergedLabels, 2, new PDPageLabelRange("D", null, null)); |
| 826 |
} catch (IOException e) { |
| 827 |
throw new RuntimeException(e); |
| 828 |
} |
| 829 |
}); |
| 830 |
} |
| 831 |
|
| 832 |
@Test |
| 833 |
public void mergeKeepingPageLabelsButDiscardingDecimalsStart() throws IOException { |
| 834 |
MergeParameters parameters = new MergeParameters(); |
| 835 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 836 |
PDDocument doc1 = new DocBuilder().withPages(2).withPageLabelRange(0, "D", null, 2).get(); |
| 837 |
PDDocument doc2 = new DocBuilder().withPages(3).withPageLabelRange(0, "D", null, 7).get(); |
| 838 |
|
| 839 |
parameters.addInput(new PdfMergeInput(customInput(doc1, "doc1.pdf"))); |
| 840 |
parameters.addInput(new PdfMergeInput(customInput(doc2, "doc2.pdf"))); |
| 841 |
parameters.setCatalogPageLabelsPolicy(CatalogPageLabelsPolicy.RETAIN); |
| 842 |
testContext.pdfOutputTo(parameters); |
| 843 |
execute(parameters); |
| 844 |
|
| 845 |
testContext.assertTaskCompleted(); |
| 846 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 847 |
try { |
| 848 |
PDPageLabels mergedLabels = d.getDocumentCatalog().getPageLabels(); |
| 849 |
assertPageLabelIndexesAre(mergedLabels, 0, 2); |
| 850 |
assertPageLabelRangeIs(mergedLabels, 0, new PDPageLabelRange("D", null, null)); |
| 851 |
assertPageLabelRangeIs(mergedLabels, 2, new PDPageLabelRange("D", null, null)); |
| 852 |
} catch (IOException e) { |
| 853 |
throw new RuntimeException(e); |
| 854 |
} |
| 855 |
}); |
| 856 |
} |
| 857 |
|
| 858 |
@Test |
| 859 |
public void mergeDiscardingPageLabels() throws IOException { |
| 860 |
MergeParameters parameters = new MergeParameters(); |
| 861 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 862 |
|
| 863 |
PDDocument doc1 = new DocBuilder().withPages(2).withPageLabelRange(0, "r", "Intro:").get(); |
| 864 |
PDDocument doc2 = new DocBuilder().withPages(3).withPageLabelRange(0, "D").get(); |
| 865 |
|
| 866 |
parameters.addInput(new PdfMergeInput(customInput(doc1, "doc1.pdf"))); |
| 867 |
parameters.addInput(new PdfMergeInput(customInput(doc2, "doc2.pdf"))); |
| 868 |
|
| 869 |
parameters.setCatalogPageLabelsPolicy(CatalogPageLabelsPolicy.DISCARD); |
| 870 |
|
| 871 |
testContext.pdfOutputTo(parameters); |
| 872 |
execute(parameters); |
| 873 |
|
| 874 |
testContext.assertTaskCompleted(); |
| 875 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 876 |
try { |
| 877 |
assertNull(d.getDocumentCatalog().getPageLabels()); |
| 878 |
} catch (IOException e) { |
| 879 |
throw new RuntimeException(e); |
| 880 |
} |
| 881 |
}); |
| 882 |
} |
| 883 |
|
| 884 |
@Test |
| 885 |
public void withCoverPage() throws IOException { |
| 886 |
List<PdfMergeInput> inputs = new ArrayList<PdfMergeInput>(); |
| 887 |
inputs.add(new PdfMergeInput(shortInput())); // 4 pages, cover/title doc |
| 888 |
inputs.add(new PdfMergeInput(regularInput())); // 11 pages |
| 889 |
inputs.add(new PdfMergeInput(customInput("pdf/attachments_as_annots.pdf", "attachments_as_annots.pdf"))); // 3 pages |
| 890 |
|
| 891 |
MergeParameters parameters = setUpParameters(inputs); |
| 892 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 893 |
parameters.setFilenameFooter(true); |
| 894 |
parameters.setFirstInputCoverTitle(true); |
| 895 |
|
| 896 |
testContext.pdfOutputTo(parameters); |
| 897 |
|
| 898 |
execute(parameters); |
| 899 |
|
| 900 |
testContext.assertTaskCompleted(); |
| 901 |
testContext.assertPages(19).forEachPdfOutput(d -> { |
| 902 |
// first 4 pages are the cover/title doc - short input |
| 903 |
assertFooterHasText(d.getPage(0), "short-test-file 1"); |
| 904 |
assertFooterHasText(d.getPage(3), "short-test-file 4"); |
| 905 |
|
| 906 |
// the TOC |
| 907 |
assertFooterHasText(d.getPage(4), ""); |
| 908 |
assertPageTextExactLines(d.getPage(4), "test-file 6\nattachments_as_annots 17\n"); |
| 909 |
|
| 910 |
// next 11 pages are the regular input |
| 911 |
assertFooterHasText(d.getPage(5), "test-file 6"); |
| 912 |
assertFooterHasText(d.getPage(15), "test-file 16"); |
| 913 |
|
| 914 |
// next 3 pages are attachments_as_annots doc |
| 915 |
assertFooterHasText(d.getPage(16), "attachments_as_annots 17"); |
| 916 |
assertFooterHasText(d.getPage(18), "attachments_as_annots 19"); |
| 917 |
}); |
| 918 |
} |
| 919 |
|
| 920 |
@Test |
| 921 |
public void withoutRotations() throws IOException { |
| 922 |
List<MergeInput> inputs = new ArrayList<MergeInput>(); |
| 923 |
inputs.add(new PdfMergeInput(shortInput())); // 4 pages, cover/title doc |
| 924 |
inputs.add(new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.png"))); |
| 925 |
|
| 926 |
MergeParameters parameters = setUpParameters(inputs); |
| 927 |
|
| 928 |
testContext.pdfOutputTo(parameters); |
| 929 |
|
| 930 |
execute(parameters); |
| 931 |
|
| 932 |
testContext.assertTaskCompleted(); |
| 933 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 934 |
assertEquals(d.getPage(0).getRotation(), 0); |
| 935 |
assertEquals(d.getPage(1).getRotation(), 0); |
| 936 |
assertEquals(d.getPage(2).getRotation(), 0); |
| 937 |
assertEquals(d.getPage(3).getRotation(), 0); |
| 938 |
|
| 939 |
assertEquals(d.getPage(4).getRotation(), 0); |
| 940 |
}); |
| 941 |
} |
| 942 |
|
| 943 |
@Test |
| 944 |
public void withRotations() throws IOException { |
| 945 |
List<MergeInput> inputs = new ArrayList<MergeInput>(); |
| 946 |
inputs.add(new PdfMergeInput(shortInput())); // 4 pages, cover/title doc |
| 947 |
inputs.add(new ImageMergeInput(customNonPdfInput("image/draft.png", "draft.png"))); |
| 948 |
|
| 949 |
MergeParameters parameters = setUpParameters(inputs); |
| 950 |
parameters.setRotations(Arrays.asList(Rotation.DEGREES_90, Rotation.DEGREES_180)); |
| 951 |
|
| 952 |
testContext.pdfOutputTo(parameters); |
| 953 |
|
| 954 |
execute(parameters); |
| 955 |
|
| 956 |
testContext.assertTaskCompleted(); |
| 957 |
testContext.assertPages(5).forEachPdfOutput(d -> { |
| 958 |
assertEquals(d.getPage(0).getRotation(), 90); |
| 959 |
assertEquals(d.getPage(1).getRotation(), 90); |
| 960 |
assertEquals(d.getPage(2).getRotation(), 90); |
| 961 |
assertEquals(d.getPage(3).getRotation(), 90); |
| 962 |
|
| 963 |
assertEquals(d.getPage(4).getRotation(), 180); |
| 964 |
}); |
| 965 |
} |
| 966 |
|
| 967 |
@Test |
| 968 |
public void atRestEncryptionTest() throws IOException { |
| 969 |
MergeParameters parameters = new MergeParameters(); |
| 970 |
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); |
| 971 |
parameters.addInput(new ImageMergeInput(encryptedAtRest(customNonPdfInput("image/draft.png")))); |
| 972 |
parameters.addInput(new ImageMergeInput(encryptedAtRest(customNonPdfInput("image/draft.tiff")))); |
| 973 |
parameters.addInput(new PdfMergeInput(encryptedAtRest(customInput("pdf/test-pdf.pdf")))); |
| 974 |
|
| 975 |
testContext.pdfOutputTo(parameters); |
| 976 |
execute(parameters); |
| 977 |
|
| 978 |
testContext.assertTaskCompleted(); |
| 979 |
testContext.assertNoTaskWarnings(); |
| 980 |
testContext.assertPages(13); |
| 981 |
} |
| 982 |
|
| 983 |
@Test |
| 984 |
public void withLargeTocItemsThatWrapAndGenerateMultipleTocPages() throws IOException { |
| 985 |
List<String> entries = new ArrayList<>(); |
| 986 |
for(int i = 1; i <= 26; i++) { |
| 987 |
if(i % 3 == 0) { |
| 988 |
entries.add("Attachment " + i + " - Sample file name - longer item that might wrap on the next line"); |
| 989 |
} else { |
| 990 |
entries.add("Attachment " + i + " - Sample file name - shorter"); |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
List<PdfMergeInput> inputs = new ArrayList<PdfMergeInput>(entries.size()); |
| 995 |
for(String entry: entries) { |
| 996 |
inputs.add(new PdfMergeInput(customInput("pdf/one_page.pdf", entry + ".pdf"))); // 1 page |
| 997 |
} |
| 998 |
|
| 999 |
MergeParameters parameters = setUpParameters(inputs); |
| 1000 |
parameters.setTableOfContentsPolicy(ToCPolicy.FILE_NAMES); |
| 1001 |
parameters.setFilenameFooter(true); |
| 1002 |
|
| 1003 |
testContext.pdfOutputTo(parameters); |
| 1004 |
|
| 1005 |
execute(parameters); |
| 1006 |
|
| 1007 |
testContext.assertTaskCompleted(); |
| 1008 |
testContext.assertPages(entries.size() + 2).forEachPdfOutput(d -> { |
| 1009 |
// the TOC |
| 1010 |
assertPageTextExactLines(d.getPage(0), "Attachment 1 - Sample file name - shorter 3\nAttachment 2 - Sample file name - shorter 4\nAttachment 3 - Sample file name - longer item that might wrap on\nthe next line 5\nAttachment 4 - Sample file name - shorter 6\nAttachment 5 - Sample file name - shorter 7\nAttachment 6 - Sample file name - longer item that might wrap on\nthe next line 8\nAttachment 7 - Sample file name - shorter 9\nAttachment 8 - Sample file name - shorter 10\nAttachment 9 - Sample file name - longer item that might wrap on\nthe next line 11\nAttachment 10 - Sample file name - shorter 12\nAttachment 11 - Sample file name - shorter 13\nAttachment 12 - Sample file name - longer item that might wrap\non the next line 14\nAttachment 13 - Sample file name - shorter 15\nAttachment 14 - Sample file name - shorter 16\nAttachment 15 - Sample file name - longer item that might wrap\non the next line 17\nAttachment 16 - Sample file name - shorter 18\nAttachment 17 - Sample file name - shorter 19\nAttachment 18 - Sample file name - longer item that might wrap\non the next line 20\nAttachment 19 - Sample file name - shorter 21\nAttachment 20 - Sample file name - shorter 22\nAttachment 21 - Sample file name - longer item that might wrap\non the next line 23\nAttachment 22 - Sample file name - shorter 24\nAttachment 23 - Sample file name - shorter 25\n"); |
| 1011 |
assertPageTextExactLines(d.getPage(1), "Attachment 24 - Sample file name - longer item that might wrap\non the next line 26\nAttachment 25 - Sample file name - shorter 27\nAttachment 26 - Sample file name - shorter 28\n"); |
| 1012 |
|
| 1013 |
// next pages are the merged inputs |
| 1014 |
assertFooterHasText(d.getPage(2), "Attachment 1 - Sample file name - shorter 3"); |
| 1015 |
assertFooterHasText(d.getPage(3), "Attachment 2 - Sample file name - shorter 4"); |
| 1016 |
assertFooterHasText(d.getPage(4), "Attachment 3 - Sample file name - longer item that might wrap on the next line 5"); |
| 1017 |
assertFooterHasText(d.getPage(27), "Attachment 26 - Sample file name - shorter 28"); |
| 1018 |
}); |
| 1019 |
} |
| 1020 |
|
| 1021 |
private float widthOfCropBox(PDPage page) { |
| 1022 |
return page.getCropBox().rotate(page.getRotation()).getWidth(); |
| 1023 |
} |
| 1024 |
|
| 1025 |
private float heightOfCropBox(PDPage page) { |
| 1026 |
return page.getCropBox().getHeight(); |
| 1027 |
} |
| 1028 |
|
| 1029 |
private void assertFooterHasText(PDPage page, String expectedText) { |
| 1030 |
try { |
| 1031 |
assertThat(new PdfTextExtractorByArea().extractFooterText(page).trim(), is(expectedText)); |
| 1032 |
} catch (TaskIOException e) { |
| 1033 |
fail(e.getMessage()); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
private boolean isBlankPage(PDPage page) throws TaskIOException { |
| 1038 |
return StringUtils.isBlank(new PdfTextExtractorByArea() |
| 1039 |
.extractTextFromArea(page, |
| 1040 |
new Rectangle(0, 0, (int) page.getTrimBox().getWidth(), (int) page.getTrimBox().getHeight())) |
| 1041 |
.trim()); |
| 1042 |
} |
| 1043 |
|
| 1044 |
private List<PDField> findFieldsMatching(String partialName, PDDocument doc) { |
| 1045 |
return doc.getDocumentCatalog().getAcroForm().getFieldTree().stream() |
| 1046 |
.filter(f -> f.getFullyQualifiedName().contains(partialName)) |
| 1047 |
.collect(Collectors.toList()); |
| 1048 |
} |
| 1049 |
|
| 1050 |
private List<PDField> findFieldsNamedExact(String name, PDDocument doc) { |
| 1051 |
return doc.getDocumentCatalog().getAcroForm().getFieldTree().stream() |
| 1052 |
.filter(f -> f.getFullyQualifiedName().equals(name)) |
| 1053 |
.collect(Collectors.toList()); |
| 1054 |
} |
| 1055 |
} |