PdfReader implements Closeable, Serializable { public void close() throws IOException { tokens.close(); } PdfReader(IRandomAccessSource byteSource, ReaderProperties properties);  PdfReader(InputStream is, ReaderProperties properties);  PdfReader(java.io.File file);  PdfReader(InputStream is);  PdfReader(String filename, ReaderProperties properties);  PdfReader(String filename); void close(); PdfReader setUnethicalReading(boolean unethicalReading); PdfReader setMemorySavingMode(boolean memorySavingMode); boolean isCloseStream(); void setCloseStream(boolean closeStream); boolean hasRebuiltXref(); boolean hasHybridXref(); boolean hasXrefStm(); boolean hasFixedXref(); long getLastXref(); byte[] readStreamBytes(PdfStream stream, boolean decode); byte[] readStreamBytesRaw(PdfStream stream); InputStream readStream(PdfStream stream, boolean decode); static byte[] decodeBytes(byte[] b, PdfDictionary streamDictionary); static byte[] decodeBytes(byte[] b, PdfDictionary streamDictionary, Map<PdfName, IFilterHandler> filterHandlers); RandomAccessFileOrArray getSafeFile(); long getFileLength(); boolean isOpenedWithFullPermission(); long getPermissions(); int getCryptoMode(); PdfAConformanceLevel getPdfAConformanceLevel(); byte[] computeUserPassword(); byte[] getOriginalFileId(); byte[] getModifiedFileId(); boolean isEncrypted();  }
@Test public void pagesTest11() throws IOException { String filename = sourceFolder + "hello.pdf"; PdfReader reader = new PdfReader(filename); PdfDocument document = new PdfDocument(reader); try { document.getPage(-30); } catch (IndexOutOfBoundsException e) { Assert.assertEquals(MessageFormatUtil.format(PdfException.RequestedPageNumberIsOutOfBounds, -30), e.getMessage()); } try { document.getPage(0); } catch (IndexOutOfBoundsException e) { Assert.assertEquals(MessageFormatUtil.format(PdfException.RequestedPageNumberIsOutOfBounds, 0), e.getMessage()); } document.getPage(1); try { document.getPage(25); } catch (IndexOutOfBoundsException e) { Assert.assertEquals(MessageFormatUtil.format(PdfException.RequestedPageNumberIsOutOfBounds, 25), e.getMessage()); } document.close(); }
@Test @LogMessages(messages = { @LogMessage(messageTemplate = LogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT), @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE), }) public void correctSimpleDoc4() throws IOException { String filename = sourceFolder + "correctSimpleDoc4.pdf"; PdfReader reader = new PdfReader(filename); try { PdfDocument document = new PdfDocument(reader); Assert.fail("Expect exception"); } catch (PdfException e) { Assert.assertEquals( PdfException.InvalidPageStructurePagesPagesMustBePdfDictionary, e.getMessage()); } finally { reader.close(); } }
@Test public void fixPdfTest04() throws IOException { String filename = sourceFolder + "CompressionWrongObjStm.pdf"; PdfReader reader = new PdfReader(filename); boolean exception = false; try { new PdfDocument(reader); } catch (PdfException ex) { exception = true; } Assert.assertTrue(exception); reader.close(); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT)) public void fixPdfTest05() throws IOException { String filename = sourceFolder + "CompressionWrongShift.pdf"; PdfReader reader = new PdfReader(filename); boolean exception = false; try { new PdfDocument(reader); } catch (PdfException ex) { exception = true; } Assert.assertTrue(exception); reader.close(); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE, count = 2)) public void fixPdfTest07() throws IOException { String filename = sourceFolder + "XRefSectionWithFreeReferences1.pdf"; PdfReader reader = new PdfReader(filename); boolean exception = false; try { new PdfDocument(reader); } catch (ClassCastException ex) { exception = true; } Assert.assertTrue(exception); reader.close(); }
@Test @LogMessages(messages = { @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE) }) public void fixPdfTest14() throws IOException { String filename = sourceFolder + "XRefWithInvalidGenerations2.pdf"; PdfReader reader = new PdfReader(filename); boolean exception = false; try { new PdfDocument(reader); } catch (PdfException ex) { exception = true; } Assert.assertTrue(exception); reader.close(); }
@Test(timeout = 1000) public void streamLengthCorrection1() throws IOException { synchronized (this) { String filename = sourceFolder + "10PagesDocumentWithInvalidStreamLength.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); int pageCount = pdfDoc.getNumberOfPages(); for (int k = 1; k < pageCount + 1; k++) { PdfPage page = pdfDoc.getPage(k); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(57, content.length); } pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection2() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingLength1.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(696, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection3() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingLength2.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(697, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection4() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingLength3.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(696, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection5() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingLength4.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(696, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection6() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingWithInvalidStreamLength1.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(696, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection7() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingWithInvalidStreamLength2.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(696, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection8() throws IOException { synchronized (this) { String filename = sourceFolder + "simpleCanvasWithDrawingWithInvalidStreamLength3.pdf"; PdfReader.correctStreamLength = true; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfPage page = pdfDoc.getPage(1); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(697, content.length); pdfDoc.close(); } }
@Test(timeout = 1000) public void streamLengthCorrection9() throws IOException { synchronized (this) { String filename = sourceFolder + "10PagesDocumentWithInvalidStreamLength2.pdf"; PdfReader.correctStreamLength = false; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); int pageCount = pdfDoc.getNumberOfPages(); for (int k = 1; k < pageCount + 1; k++) { PdfPage page = pdfDoc.getPage(k); page.getPdfObject().get(PdfName.MediaBox); byte[] content = page.getFirstContentStream().getBytes(); Assert.assertEquals(20, content.length); } pdfDoc.close(); PdfReader.correctStreamLength = true; } }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE)) public void freeReferencesTest() throws IOException { String filename = sourceFolder + "freeReferences.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); Assert.assertNull(pdfDoc.getPdfObject(8)); Assert.assertFalse(pdfDoc.getReader().rebuiltXref); pdfDoc.close(); }
@Test public void freeReferencesTest02() throws IOException, InterruptedException { String cmpFile = sourceFolder + "cmp_freeReferences02.pdf"; String outputFile = destinationFolder + "freeReferences02.pdf"; String inputFile = sourceFolder + "freeReferences02.pdf"; PdfWriter writer = new PdfWriter(outputFile); PdfReader reader = new PdfReader(inputFile); PdfDocument inputPdfDocument = new PdfDocument(reader); PdfDocument outputPdfDocument = new PdfDocument(writer); int lastPage = inputPdfDocument.getNumberOfPages(); inputPdfDocument.copyPagesTo(lastPage, lastPage, outputPdfDocument); inputPdfDocument.close(); outputPdfDocument.close(); Assert.assertNull(new CompareTool().compareByContent(outputFile, cmpFile, destinationFolder, "diff_")); }
@Test public void pdfVersionTest() throws IOException { String filename = sourceFolder + "hello.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); Assert.assertEquals(PdfVersion.PDF_1_4, pdfDoc.getPdfVersion()); pdfDoc.close(); }
@Test @LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE, count =1), @LogMessage(messageTemplate = LogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT), @LogMessage(messageTemplate = LogMessageConstant.ENCOUNTERED_INVALID_MCR)}) public void wrongTagStructureFlushingTest() throws IOException { String source = sourceFolder + "wrongTagStructureFlushingTest.pdf"; String dest = destinationFolder + "wrongTagStructureFlushingTest.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(dest)); pdfDoc.setTagged(); Assert.assertEquals(PdfNull.PDF_NULL, ((PdfDictionary)pdfDoc.getPdfObject(12)).get(PdfName.Pg)); pdfDoc.close(); }
@Test @Ignore("DEVSIX-2649") @LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE, count =1), @LogMessage(messageTemplate = LogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT)}) public void wrongStructureFlushingTest() throws IOException { String source = sourceFolder + "wrongStructureFlushingTest.pdf"; String dest = destinationFolder + "wrongStructureFlushingTest.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(source), new PdfWriter(dest)); pdfDoc.close(); }
@Test @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.INVALID_INDIRECT_REFERENCE)) public void hugeInvalidIndRefObjNumberTest() throws IOException { String filename = sourceFolder + "hugeIndRefObjNum.pdf"; PdfReader reader = new PdfReader(filename); PdfDocument pdfDoc = new PdfDocument(reader); PdfObject pdfObject = pdfDoc.getPdfObject(4); Assert.assertTrue(pdfObject.isDictionary()); Assert.assertEquals(PdfNull.PDF_NULL, ((PdfDictionary)pdfObject).get(PdfName.Pg)); pdfDoc.close(); }
@Test public void testManyAppendModeUpdates() throws Exception { String file = sourceFolder + "manyAppendModeUpdates.pdf"; PdfReader reader = new PdfReader(file); PdfDocument document = new PdfDocument(reader); document.close(); }