| 1 |
/* |
| 2 |
* Created on 18 nov 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.assertTrue; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.util.Optional; |
| 26 |
import java.util.Spliterator; |
| 27 |
import java.util.Spliterators; |
| 28 |
import java.util.stream.StreamSupport; |
| 29 |
|
| 30 |
import org.junit.Test; |
| 31 |
import org.sejda.io.SeekableSources; |
| 32 |
import org.sejda.model.exception.TaskIOException; |
| 33 |
import org.sejda.model.scale.ScaleType; |
| 34 |
import org.sejda.sambox.input.PDFParser; |
| 35 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 36 |
import org.sejda.sambox.pdmodel.PDPage; |
| 37 |
import org.sejda.sambox.pdmodel.common.PDRectangle; |
| 38 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; |
| 39 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination; |
| 40 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; |
| 41 |
import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDOutlineTreeIterator; |
| 42 |
|
| 43 |
/** |
| 44 |
* @author Andrea Vacondio |
| 45 |
* |
| 46 |
*/ |
| 47 |
public class PdfScalerTest { |
| 48 |
|
| 49 |
@Test |
| 50 |
public void shrinkContentNoBoxes() throws IOException, TaskIOException { |
| 51 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 52 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 53 |
PdfScaler scaler = new PdfScaler(ScaleType.CONTENT); |
| 54 |
scaler.scale(document, 0.5); |
| 55 |
PDPage page = document.getPage(0); |
| 56 |
assertEquals(new PDRectangle(0, 0, 595, 842), page.getMediaBox()); |
| 57 |
assertEquals(new PDRectangle(0, 0, 595, 842), page.getCropBox()); |
| 58 |
assertEquals(new PDRectangle(148.75f, 210.5f, 297.5f, 421f), page.getArtBox()); |
| 59 |
assertEquals(new PDRectangle(148.75f, 210.5f, 297.5f, 421f), page.getBleedBox()); |
| 60 |
assertEquals(new PDRectangle(148.75f, 210.5f, 297.5f, 421f), page.getTrimBox()); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void shrinkContentYesBoxes() throws IOException, TaskIOException { |
| 66 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 67 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 68 |
PDPage page = document.getPage(0); |
| 69 |
PDRectangle yesBox = new PDRectangle(0, 0, 200, 400); |
| 70 |
PDRectangle cropBox = new PDRectangle(0, 0, 400, 800); |
| 71 |
page.setCropBox(cropBox); |
| 72 |
page.setArtBox(yesBox); |
| 73 |
page.setTrimBox(yesBox); |
| 74 |
page.setBleedBox(yesBox); |
| 75 |
PdfScaler scaler = new PdfScaler(ScaleType.CONTENT); |
| 76 |
scaler.scale(document, 0.5); |
| 77 |
|
| 78 |
assertEquals(new PDRectangle(0, 0, 595, 842), page.getMediaBox()); |
| 79 |
assertEquals(cropBox, page.getCropBox()); |
| 80 |
assertEquals(new PDRectangle(150, 300, 100, 200), page.getArtBox()); |
| 81 |
assertEquals(new PDRectangle(150, 300, 100, 200), page.getBleedBox()); |
| 82 |
assertEquals(new PDRectangle(150, 300, 100, 200), page.getTrimBox()); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void expandContentNoBoxes() throws IOException, TaskIOException { |
| 88 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 89 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 90 |
PdfScaler scaler = new PdfScaler(ScaleType.CONTENT); |
| 91 |
scaler.scale(document, 1.5); |
| 92 |
PDPage page = document.getPage(0); |
| 93 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getMediaBox()); |
| 94 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getCropBox()); |
| 95 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getArtBox()); |
| 96 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getBleedBox()); |
| 97 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getTrimBox()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void expandContentYesBoxesOverflow() throws IOException, TaskIOException { |
| 103 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 104 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 105 |
PDPage page = document.getPage(0); |
| 106 |
PDRectangle yesBox = new PDRectangle(0, 0, 200, 400); |
| 107 |
PDRectangle cropBox = new PDRectangle(0, 0, 400, 800); |
| 108 |
page.setCropBox(cropBox); |
| 109 |
page.setArtBox(yesBox); |
| 110 |
page.setTrimBox(yesBox); |
| 111 |
page.setBleedBox(yesBox); |
| 112 |
PdfScaler scaler = new PdfScaler(ScaleType.CONTENT); |
| 113 |
scaler.scale(document, 2.5); |
| 114 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getMediaBox()); |
| 115 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getCropBox()); |
| 116 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getArtBox()); |
| 117 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getBleedBox()); |
| 118 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getTrimBox()); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void expandContentYesBoxesNoOverflow() throws IOException, TaskIOException { |
| 125 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 126 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 127 |
PDPage page = document.getPage(0); |
| 128 |
PDRectangle yesBox = new PDRectangle(0, 0, 200, 400); |
| 129 |
PDRectangle cropBox = new PDRectangle(0, 0, 400, 800); |
| 130 |
page.setCropBox(cropBox); |
| 131 |
page.setArtBox(yesBox); |
| 132 |
page.setTrimBox(yesBox); |
| 133 |
page.setBleedBox(yesBox); |
| 134 |
PdfScaler scaler = new PdfScaler(ScaleType.CONTENT); |
| 135 |
scaler.scale(document, 1.5); |
| 136 |
assertEquals(new PDRectangle(0, 0, 595f, 842), page.getMediaBox()); |
| 137 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getCropBox()); |
| 138 |
assertEquals(new PDRectangle(50, 100, 300, 600), page.getArtBox()); |
| 139 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getBleedBox()); |
| 140 |
assertEquals(new PDRectangle(0, 0, 400, 800), page.getTrimBox()); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void shrinkPageNoBoxes() throws IOException, TaskIOException { |
| 146 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 147 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 148 |
PdfScaler scaler = new PdfScaler(ScaleType.PAGE); |
| 149 |
scaler.scale(document, 0.5); |
| 150 |
PDPage page = document.getPage(0); |
| 151 |
PDRectangle expected = new PDRectangle(0f, 0f, 297.5f, 421f); |
| 152 |
assertEquals(expected, page.getMediaBox()); |
| 153 |
assertEquals(expected, page.getCropBox()); |
| 154 |
assertEquals(expected, page.getArtBox()); |
| 155 |
assertEquals(expected, page.getBleedBox()); |
| 156 |
assertEquals(expected, page.getTrimBox()); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void expandPageNoBoxes() throws IOException, TaskIOException { |
| 162 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 163 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 164 |
PdfScaler scaler = new PdfScaler(ScaleType.PAGE); |
| 165 |
scaler.scale(document, 1.5); |
| 166 |
PDPage page = document.getPage(0); |
| 167 |
PDRectangle expected = new PDRectangle(0, 0, 892.5f, 1263f); |
| 168 |
assertEquals(expected, page.getMediaBox()); |
| 169 |
assertEquals(expected, page.getCropBox()); |
| 170 |
assertEquals(expected, page.getTrimBox()); |
| 171 |
assertEquals(expected, page.getArtBox()); |
| 172 |
assertEquals(expected, page.getBleedBox()); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void expandPagesYesBoxes() throws IOException, TaskIOException { |
| 178 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 179 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/one_page.pdf")))) { |
| 180 |
PDPage page = document.getPage(0); |
| 181 |
PDRectangle yesBox = new PDRectangle(0, 0, 200, 400); |
| 182 |
PDRectangle cropBox = new PDRectangle(0, 0, 400, 800); |
| 183 |
page.setCropBox(cropBox); |
| 184 |
page.setArtBox(yesBox); |
| 185 |
page.setTrimBox(yesBox); |
| 186 |
page.setBleedBox(yesBox); |
| 187 |
PdfScaler scaler = new PdfScaler(ScaleType.PAGE); |
| 188 |
scaler.scale(document, 1.5); |
| 189 |
|
| 190 |
assertEquals(new PDRectangle(0, 0, 892.5f, 1263f), page.getMediaBox()); |
| 191 |
assertEquals(new PDRectangle(0, 0, 600, 1200), page.getCropBox()); |
| 192 |
assertEquals(new PDRectangle(0, 0, 300, 600), page.getArtBox()); |
| 193 |
assertEquals(new PDRectangle(0, 0, 300, 600), page.getBleedBox()); |
| 194 |
assertEquals(new PDRectangle(0, 0, 300, 600), page.getTrimBox()); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
@Test |
| 199 |
public void expandPageAdjustOutlineCoordinates() throws IOException, TaskIOException { |
| 200 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 201 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/test_outline.pdf")))) { |
| 202 |
PdfScaler scaler = new PdfScaler(ScaleType.PAGE); |
| 203 |
scaler.scale(document, PDRectangle.A0.getHeight() / PDRectangle.A4.getHeight()); |
| 204 |
PDOutlineItem outlineItem = StreamSupport |
| 205 |
.stream(Spliterators.spliteratorUnknownSize( |
| 206 |
new PDOutlineTreeIterator(document.getDocumentCatalog().getDocumentOutline()), |
| 207 |
Spliterator.ORDERED | Spliterator.NONNULL), false) |
| 208 |
.filter(item -> item.getTitle().contains("third level")).findFirst().orElse(null); |
| 209 |
Optional<PDPageDestination> destination = outlineItem |
| 210 |
.resolveToPageDestination(document.getDocumentCatalog()); |
| 211 |
assertTrue(destination.isPresent()); |
| 212 |
PDPageXYZDestination pageDest = ((PDPageXYZDestination) destination.get()); |
| 213 |
assertEquals(2754, pageDest.getTop()); |
| 214 |
assertEquals(224, pageDest.getLeft()); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
@Test |
| 219 |
public void expandContentAdjustOutlineCoordinates() throws IOException, TaskIOException { |
| 220 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 221 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/test_outline.pdf")))) { |
| 222 |
PdfScaler scaler = new PdfScaler(ScaleType.CONTENT); |
| 223 |
scaler.scale(document, PDRectangle.A5.getHeight() / PDRectangle.A4.getHeight()); |
| 224 |
PDOutlineItem outlineItem = StreamSupport |
| 225 |
.stream(Spliterators.spliteratorUnknownSize( |
| 226 |
new PDOutlineTreeIterator(document.getDocumentCatalog().getDocumentOutline()), |
| 227 |
Spliterator.ORDERED | Spliterator.NONNULL), false) |
| 228 |
.filter(item -> item.getTitle().contains("third level")).findFirst().orElse(null); |
| 229 |
Optional<PDPageDestination> destination = outlineItem |
| 230 |
.resolveToPageDestination(document.getDocumentCatalog()); |
| 231 |
assertTrue(destination.isPresent()); |
| 232 |
PDPageXYZDestination pageDest = ((PDPageXYZDestination) destination.get()); |
| 233 |
assertEquals(609, pageDest.getTop()); |
| 234 |
assertEquals(126, pageDest.getLeft()); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
} |