-
Notifications
You must be signed in to change notification settings - Fork 823
/
PDFParser.java
234 lines (218 loc) · 8.42 KB
/
PDFParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pdfbox.pdfparser;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.io.ScratchFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
public class PDFParser extends COSParser
{
private static final Log LOG = LogFactory.getLog(PDFParser.class);
/**
* Constructor.
* Unrestricted main memory will be used for buffering PDF streams.
*
* @param source source representing the pdf.
* @throws IOException If something went wrong.
*/
public PDFParser(RandomAccessRead source) throws IOException
{
this(source, "", ScratchFile.getMainMemoryOnlyInstance());
}
/**
* Constructor.
*
* @param source input representing the pdf.
* @param scratchFile use a {@link ScratchFile} for temporary storage.
*
* @throws IOException If something went wrong.
*/
public PDFParser(RandomAccessRead source, ScratchFile scratchFile) throws IOException
{
this(source, "", scratchFile);
}
/**
* Constructor.
* Unrestricted main memory will be used for buffering PDF streams.
*
* @param source input representing the pdf.
* @param decryptionPassword password to be used for decryption.
* @throws IOException If something went wrong.
*/
public PDFParser(RandomAccessRead source, String decryptionPassword) throws IOException
{
this(source, decryptionPassword, ScratchFile.getMainMemoryOnlyInstance());
}
/**
* Constructor.
*
* @param source input representing the pdf.
* @param decryptionPassword password to be used for decryption.
* @param scratchFile use a {@link ScratchFile} for temporary storage.
*
* @throws IOException If something went wrong.
*/
public PDFParser(RandomAccessRead source, String decryptionPassword, ScratchFile scratchFile)
throws IOException
{
this(source, decryptionPassword, null, null, scratchFile);
}
/**
* Constructor.
* Unrestricted main memory will be used for buffering PDF streams.
*
* @param source input representing the pdf.
* @param decryptionPassword password to be used for decryption.
* @param keyStore key store to be used for decryption when using public key security
* @param alias alias to be used for decryption when using public key security
*
* @throws IOException If something went wrong.
*/
public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
String alias) throws IOException
{
this(source, decryptionPassword, keyStore, alias, ScratchFile.getMainMemoryOnlyInstance());
}
/**
* Constructor.
*
* @param source input representing the pdf.
* @param decryptionPassword password to be used for decryption.
* @param keyStore key store to be used for decryption when using public key security
* @param alias alias to be used for decryption when using public key security
* @param scratchFile buffer handler for temporary storage; it will be closed on
* {@link COSDocument#close()}
*
* @throws IOException If something went wrong.
*/
public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
String alias, ScratchFile scratchFile) throws IOException
{
super(source, decryptionPassword, keyStore, alias);
fileLen = source.length();
init(scratchFile);
}
private void init(ScratchFile scratchFile) throws IOException
{
String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE);
if (eofLookupRangeStr != null)
{
try
{
setEOFLookupRange(Integer.parseInt(eofLookupRangeStr));
}
catch (NumberFormatException nfe)
{
LOG.warn("System property " + SYSPROP_EOFLOOKUPRANGE
+ " does not contain an integer value, but: '" + eofLookupRangeStr + "'");
}
}
document = new COSDocument(scratchFile);
}
/**
* This will get the PD document that was parsed. When you are done with
* this document you must call close() on it to release resources.
*
* @return The document at the PD layer.
*
* @throws IOException If there is an error getting the document.
*/
public PDDocument getPDDocument() throws IOException
{
PDDocument doc = new PDDocument(getDocument(), source, getAccessPermission());
doc.setEncryptionDictionary(getEncryption());
return doc;
}
/**
* The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset)
* to all the pdf's objects. It can handle linearized pdfs, which will have an xref at the end pointing to an xref
* at the beginning of the file. Last the root object is parsed.
*
* @throws InvalidPasswordException If the password is incorrect.
* @throws IOException If something went wrong.
*/
protected void initialParse() throws InvalidPasswordException, IOException
{
COSDictionary trailer = retrieveTrailer();
COSBase base = parseTrailerValuesDynamically(trailer);
if (!(base instanceof COSDictionary))
{
throw new IOException("Expected root dictionary, but got this: " + base);
}
COSDictionary root = (COSDictionary) base;
// in some pdfs the type value "Catalog" is missing in the root object
if (isLenient() && !root.containsKey(COSName.TYPE))
{
root.setItem(COSName.TYPE, COSName.CATALOG);
}
// parse all objects, starting at the root dictionary
parseDictObjects(root, (COSName[]) null);
// parse all objects of the info dictionary
COSBase infoBase = trailer.getDictionaryObject(COSName.INFO);
if (infoBase instanceof COSDictionary)
{
parseDictObjects((COSDictionary) infoBase, (COSName[]) null);
}
// check pages dictionaries
checkPages(root);
document.setDecrypted();
initialParseDone = true;
}
/**
* This will parse the stream and populate the COSDocument object. This will close
* the keystore stream when it is done parsing.
*
* @throws InvalidPasswordException If the password is incorrect.
* @throws IOException If there is an error reading from the stream or corrupt data
* is found.
*/
public void parse() throws InvalidPasswordException, IOException
{
// set to false if all is processed
boolean exceptionOccurred = true;
try
{
// PDFBOX-1922 read the version header and rewind
if (!parsePDFHeader() && !parseFDFHeader())
{
throw new IOException( "Error: Header doesn't contain versioninfo" );
}
if (!initialParseDone)
{
initialParse();
}
exceptionOccurred = false;
}
finally
{
if (exceptionOccurred && document != null)
{
IOUtils.closeQuietly(document);
document = null;
}
}
}
}