| 1 |
/* |
| 2 |
* Created on 06 set 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; |
| 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 |
import static org.sejda.TestUtils.getEncryptionAtRestPolicy; |
| 27 |
|
| 28 |
import java.io.File; |
| 29 |
import java.io.IOException; |
| 30 |
import java.io.InputStream; |
| 31 |
import java.util.Arrays; |
| 32 |
|
| 33 |
import org.junit.Test; |
| 34 |
import org.sejda.core.support.io.IOUtils; |
| 35 |
import org.sejda.io.SeekableSources; |
| 36 |
import org.sejda.model.exception.TaskException; |
| 37 |
import org.sejda.model.input.PdfFileSource; |
| 38 |
import org.sejda.sambox.cos.COSName; |
| 39 |
import org.sejda.sambox.input.PDFParser; |
| 40 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 41 |
import org.sejda.sambox.pdmodel.PDPage; |
| 42 |
import org.sejda.sambox.pdmodel.PageLayout; |
| 43 |
import org.sejda.sambox.pdmodel.PageMode; |
| 44 |
import org.sejda.sambox.pdmodel.common.PDRectangle; |
| 45 |
import org.sejda.sambox.pdmodel.interactive.pagenavigation.PDThreadBead; |
| 46 |
|
| 47 |
/** |
| 48 |
* @author Andrea Vacondio |
| 49 |
* |
| 50 |
*/ |
| 51 |
public class PDDocumentHandlerTest { |
| 52 |
|
| 53 |
@Test |
| 54 |
public void discardBeads() throws IOException { |
| 55 |
try (PDDocument document = testDoc("pdf/one_page.pdf")) { |
| 56 |
PDPage page = document.getPage(0); |
| 57 |
page.setThreadBeads(Arrays.asList(new PDThreadBead())); |
| 58 |
assertFalse(page.getThreadBeads().isEmpty()); |
| 59 |
PDPage copy = new PDDocumentHandler().importPage(page); |
| 60 |
assertEquals(page.getMediaBox(), copy.getMediaBox()); |
| 61 |
assertEquals(page.getCropBox(), copy.getCropBox()); |
| 62 |
assertEquals(page.getRotation(), copy.getRotation()); |
| 63 |
assertEquals(page.getResources(), copy.getResources()); |
| 64 |
assertTrue(copy.getThreadBeads().isEmpty()); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void testAddBlankPageIfOdd() throws IOException { |
| 70 |
try (PDDocumentHandler handler = new PDDocumentHandler()) { |
| 71 |
assertNull(handler.addBlankPageIfOdd(new PDRectangle(10, 10))); |
| 72 |
handler.addBlankPage(new PDRectangle(10, 10)); |
| 73 |
assertNotNull(handler.addBlankPageIfOdd(new PDRectangle(10, 10))); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void layoutAndMode() { |
| 79 |
PDDocumentHandler victim = new PDDocumentHandler(); |
| 80 |
victim.initialiseBasedOn(new PDDocument()); |
| 81 |
assertFalse( |
| 82 |
victim.getUnderlyingPDDocument().getDocumentCatalog().getCOSObject().containsKey(COSName.PAGE_LAYOUT)); |
| 83 |
assertFalse( |
| 84 |
victim.getUnderlyingPDDocument().getDocumentCatalog().getCOSObject().containsKey(COSName.PAGE_MODE)); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void layoutAndModeSet() { |
| 89 |
PDDocument doc = new PDDocument(); |
| 90 |
doc.getDocumentCatalog().setPageLayout(PageLayout.TWO_COLUMN_RIGHT); |
| 91 |
doc.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES); |
| 92 |
PDDocumentHandler victim = new PDDocumentHandler(); |
| 93 |
victim.initialiseBasedOn(doc); |
| 94 |
assertEquals(PageLayout.TWO_COLUMN_RIGHT, |
| 95 |
victim.getUnderlyingPDDocument().getDocumentCatalog().getPageLayout()); |
| 96 |
assertEquals(PageMode.USE_OUTLINES, victim.getUnderlyingPDDocument().getDocumentCatalog().getPageMode()); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void inheritedMediaBox() throws IOException { |
| 101 |
try (PDDocumentHandler handler1 = new PDDocumentHandler(testDoc("pdf/media_box_inherited.pdf"))) { |
| 102 |
PDPage page = handler1.getPage(1); |
| 103 |
assertEquals(PDRectangle.A4, page.getMediaBox()); |
| 104 |
|
| 105 |
try (PDDocumentHandler handler2 = new PDDocumentHandler()) { |
| 106 |
PDPage imported = handler2.importPage(page); |
| 107 |
|
| 108 |
assertEquals("Imported page has different media box than source", imported.getMediaBox(), |
| 109 |
page.getMediaBox()); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void encryptionAtRestOutputRoundtrip() throws IOException, TaskException { |
| 116 |
File tmpFile = IOUtils.createTemporaryBuffer(); |
| 117 |
int pageNum = 0; |
| 118 |
try (PDDocumentHandler handler = new PDDocumentHandler(testDoc("pdf/test-pdf.pdf"))) { |
| 119 |
pageNum = handler.getNumberOfPages(); |
| 120 |
handler.savePDDocument(tmpFile, getEncryptionAtRestPolicy()); |
| 121 |
} |
| 122 |
|
| 123 |
// read it back |
| 124 |
PdfFileSource encrypted = PdfFileSource.newInstanceNoPassword(tmpFile); |
| 125 |
encrypted.setEncryptionAtRestPolicy(getEncryptionAtRestPolicy()); |
| 126 |
|
| 127 |
try (PDDocumentHandler handler = new DefaultPdfSourceOpener().open(encrypted)) { |
| 128 |
assertEquals(pageNum, handler.getNumberOfPages()); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
private InputStream resourceAsStream(String name) { |
| 133 |
return getClass().getClassLoader().getResourceAsStream(name); |
| 134 |
} |
| 135 |
|
| 136 |
private PDDocument testDoc(String resourceName) throws IOException { |
| 137 |
return PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(resourceAsStream(resourceName))); |
| 138 |
} |
| 139 |
} |