|
1 | 1 | package com.mindee.pdf; |
2 | 2 |
|
| 3 | +import com.mindee.MindeeException; |
3 | 4 | import com.mindee.input.LocalInputSource; |
4 | 5 | import java.io.IOException; |
5 | 6 | import java.nio.file.Files; |
|
12 | 13 | */ |
13 | 14 | @Getter |
14 | 15 | public class ExtractedPDF { |
| 16 | + /** |
| 17 | + * PDF content as bytes. |
| 18 | + */ |
15 | 19 | private final byte[] fileBytes; |
| 20 | + /** |
| 21 | + * Name of the file when writing to disk. |
| 22 | + */ |
16 | 23 | private final String filename; |
| 24 | + /** |
| 25 | + * 0-based indexes of all pages taken from the original PDF. |
| 26 | + */ |
| 27 | + private final int[] pageIndexes; |
| 28 | + /** |
| 29 | + * The number of pages in this PDF file. |
| 30 | + */ |
| 31 | + private final int pageCount; |
17 | 32 |
|
18 | 33 | /** |
19 | 34 | * Default constructor. |
20 | 35 | * |
21 | 36 | * @param fileBytes PDF file as bytes. |
22 | 37 | * @param filename Name of the extracted file. |
| 38 | + * @param pageIndexes Two-element array: index of the first and last extracted page. |
23 | 39 | */ |
24 | | - public ExtractedPDF(byte[] fileBytes, String filename) { |
| 40 | + public ExtractedPDF(byte[] fileBytes, String filename, int[] pageIndexes) { |
25 | 41 | this.fileBytes = fileBytes; |
26 | 42 | this.filename = filename; |
| 43 | + this.pageIndexes = pageIndexes; |
| 44 | + this.pageCount = pageIndexes.length; |
27 | 45 | } |
28 | 46 |
|
29 | 47 | /** |
30 | 48 | * Write the extracted PDF to a file. |
31 | 49 | * |
32 | 50 | * @param outputPath the output path, it may be a file or a directory. |
33 | | - * @throws IOException Throws if the file can't be accessed. |
34 | 51 | */ |
35 | | - public void writeToFile(Path outputPath) throws IOException { |
| 52 | + public void writeToFile(Path outputPath) throws MindeeException { |
36 | 53 | if (!Files.isDirectory(outputPath)) { |
37 | | - throw new IllegalArgumentException("Provided path is not a directory."); |
| 54 | + throw new MindeeException("Provided path is not a directory."); |
| 55 | + } |
| 56 | + try { |
| 57 | + Files.write(outputPath.resolve(this.filename), this.fileBytes); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new MindeeException("Could not save file " + this.filename + ".", e); |
38 | 60 | } |
39 | | - |
40 | | - Files.write(outputPath.resolve(this.filename), this.fileBytes); |
41 | 61 | } |
42 | 62 |
|
43 | 63 | /** |
44 | 64 | * Write the extracted PDF to a file. |
45 | 65 | * |
46 | 66 | * @param outputPath the output path, it may be a file or a directory. |
47 | | - * @throws IOException Throws if the file can't be accessed. |
48 | 67 | */ |
49 | | - public void writeToFile(String outputPath) throws IOException { |
| 68 | + public void writeToFile(String outputPath) throws MindeeException { |
50 | 69 | writeToFile(Paths.get(outputPath)); |
51 | 70 | } |
52 | 71 |
|
53 | 72 | /** |
54 | 73 | * Return the file in a format suitable for sending to MindeeClient for parsing. |
55 | 74 | * |
56 | 75 | * @return an instance of {@link LocalInputSource} |
57 | | - * @throws IOException Throws if the file can't be accessed. |
58 | 76 | */ |
59 | | - public LocalInputSource asInputSource() throws IOException { |
| 77 | + public LocalInputSource asInputSource() { |
60 | 78 | return new LocalInputSource(this.fileBytes, this.filename); |
61 | 79 | } |
62 | 80 | } |
0 commit comments