@@ -225,8 +225,13 @@ public void setCloseStream(boolean closeStream) {
225
225
* If any exception generated while reading XRef section, PdfReader will try to rebuild it.
226
226
*
227
227
* @return true, if PdfReader rebuilt Cross-Reference section.
228
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
228
229
*/
229
230
public boolean hasRebuiltXref () {
231
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
232
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
233
+ }
234
+
230
235
return rebuiltXref ;
231
236
}
232
237
@@ -235,35 +240,57 @@ public boolean hasRebuiltXref() {
235
240
* That Do Not Support Compressed Reference Streams" in PDF 32000-1:2008 spec.
236
241
*
237
242
* @return true, if the document has hybrid Cross-Reference section.
243
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
238
244
*/
239
245
public boolean hasHybridXref () {
246
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
247
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
248
+ }
249
+
240
250
return hybridXref ;
241
251
}
242
252
243
253
/**
244
254
* Indicates whether the document has Cross-Reference Streams.
245
255
*
246
256
* @return true, if the document has Cross-Reference Streams.
257
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
247
258
*/
248
259
public boolean hasXrefStm () {
260
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
261
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
262
+ }
263
+
249
264
return xrefStm ;
250
265
}
251
266
252
267
/**
253
268
* If any exception generated while reading PdfObject, PdfReader will try to fix offsets of all objects.
254
- *
269
+ * <p>
270
+ * This method's returned value might change over time, because PdfObjects reading
271
+ * can be postponed even up to document closing.
255
272
* @return true, if PdfReader fixed offsets of PdfObjects.
273
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
256
274
*/
257
275
public boolean hasFixedXref () {
276
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
277
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
278
+ }
279
+
258
280
return fixedXref ;
259
281
}
260
282
261
283
/**
262
284
* Gets position of the last Cross-Reference table.
263
285
*
264
286
* @return -1 if Cross-Reference table has rebuilt, otherwise position of the last Cross-Reference table.
287
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
265
288
*/
266
289
public long getLastXref () {
290
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
291
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
292
+ }
293
+
267
294
return lastXref ;
268
295
}
269
296
@@ -478,8 +505,13 @@ public long getFileLength() throws IOException {
478
505
*
479
506
* @return {@code true} if the document was opened with the owner password or if it's not encrypted,
480
507
* {@code false} if the document was opened with the user password.
508
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
481
509
*/
482
510
public boolean isOpenedWithFullPermission () {
511
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
512
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
513
+ }
514
+
483
515
return !encrypted || decrypt .isOpenedWithFullPermission () || unethicalReading ;
484
516
}
485
517
@@ -489,8 +521,19 @@ public boolean isOpenedWithFullPermission() {
489
521
* See ISO 32000-1, Table 22 for more details.
490
522
*
491
523
* @return the encryption permissions, an unsigned 32-bit quantity.
524
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
492
525
*/
493
526
public long getPermissions () {
527
+
528
+ /* !pdfDocument.getXref().isReadingCompleted() can be used for encryption properties as well,
529
+ * because decrypt object is initialized in private readDecryptObj method which is called in our code
530
+ * in the next line after the setting isReadingCompleted line. This means that there's no way for users
531
+ * when this method would work incorrectly right now.
532
+ */
533
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
534
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
535
+ }
536
+
494
537
long perm = 0 ;
495
538
if (encrypted && decrypt .getPermissions () != null ) {
496
539
perm = (long ) decrypt .getPermissions ();
@@ -502,8 +545,13 @@ public long getPermissions() {
502
545
* Gets encryption algorithm and access permissions.
503
546
*
504
547
* @see EncryptionConstants
548
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
505
549
*/
506
550
public int getCryptoMode () {
551
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
552
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
553
+ }
554
+
507
555
if (decrypt == null )
508
556
return -1 ;
509
557
else
@@ -525,9 +573,17 @@ public PdfAConformanceLevel getPdfAConformanceLevel() {
525
573
* Computes user password if standard encryption handler is used with Standard40, Standard128 or AES128 encryption algorithm.
526
574
*
527
575
* @return user password, or null if not a standard encryption handler was used or if ownerPasswordUsed wasn't use to open the document.
576
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
528
577
*/
529
578
public byte [] computeUserPassword () {
530
- if (!encrypted || !decrypt .isOpenedWithFullPermission ()) return null ;
579
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
580
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
581
+ }
582
+
583
+ if (!encrypted || !decrypt .isOpenedWithFullPermission ()) {
584
+ return null ;
585
+ }
586
+
531
587
return decrypt .computeUserPassword (properties .password );
532
588
}
533
589
@@ -540,8 +596,13 @@ public byte[] computeUserPassword() {
540
596
*
541
597
* @return byte array represents original file ID.
542
598
* @see PdfDocument#getOriginalDocumentId()
599
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
543
600
*/
544
601
public byte [] getOriginalFileId () {
602
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
603
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
604
+ }
605
+
545
606
PdfArray id = trailer .getAsArray (PdfName .ID );
546
607
if (id != null && id .size () == 2 ) {
547
608
return ByteUtils .getIsoBytes (id .getAsString (0 ).getValue ());
@@ -559,8 +620,13 @@ public byte[] getOriginalFileId() {
559
620
*
560
621
* @return byte array represents modified file ID.
561
622
* @see PdfDocument#getModifiedDocumentId()
623
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
562
624
*/
563
625
public byte [] getModifiedFileId () {
626
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
627
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
628
+ }
629
+
564
630
PdfArray id = trailer .getAsArray (PdfName .ID );
565
631
if (id != null && id .size () == 2 ) {
566
632
return ByteUtils .getIsoBytes (id .getAsString (1 ).getValue ());
@@ -569,7 +635,14 @@ public byte[] getModifiedFileId() {
569
635
}
570
636
}
571
637
638
+ /**
639
+ * @throws PdfException, if the method has been invoked before the PDF document was read.
640
+ */
572
641
public boolean isEncrypted () {
642
+ if (pdfDocument == null || !pdfDocument .getXref ().isReadingCompleted ()) {
643
+ throw new PdfException (PdfException .DocumentHasNotBeenReadYet );
644
+ }
645
+
573
646
return encrypted ;
574
647
}
575
648
@@ -737,7 +810,7 @@ protected PdfObject readObject(boolean readAsDirect, boolean objStm) throws IOEx
737
810
return new PdfNumber (tokens .getByteContent ());
738
811
case String : {
739
812
PdfString pdfString = new PdfString (tokens .getByteContent (), tokens .isHexString ());
740
- if (isEncrypted () && !decrypt .isEmbeddedFilesOnly () && !objStm ) {
813
+ if (encrypted && !decrypt .isEmbeddedFilesOnly () && !objStm ) {
741
814
pdfString .setDecryption (currentIndirectReference .getObjNumber (), currentIndirectReference .getGenNumber (), decrypt );
742
815
}
743
816
return pdfString ;
0 commit comments