void addDocument(PdfReader reader) throws DocumentException, IOException { if (!reader.isOpenedWithFullPermissions()) throw new BadPasswordException("PdfReader not opened with owner password"); openDoc(); if (readers2intrefs.containsKey(reader)) { reader = new PdfReader(reader); } else { if (reader.isTampered()) throw new DocumentException("The document was reused."); reader.consolidateNamedDestinations(); reader.setTampered(true); } reader.shuffleSubsetNames(); readers2intrefs.put(reader, new IntHashtable()); readers.add(reader); int len = reader.getNumberOfPages(); IntHashtable refs = new IntHashtable(); for (int p = 1; p <= len; ++p) { refs.put(reader.getPageOrigRef(p).getNumber(), 1); reader.releasePage(p); } pages2intrefs.put(reader, refs); visited.put(reader, new IntHashtable()); fields.add(reader.getAcroFields()); updateCalculationOrder(reader); }
@Test(expected=BadPasswordException.class) public void testDecyptNoPassword() throws DocumentException, IOException { byte[] crypted = createEncryptedPDF(); // NO password supplied, will throw BaddPasswordException PdfReader reader = new PdfReader(crypted); Assert.assertEquals(1, reader.getNumberOfPages()); reader.close(); }
@Test(expected=BadPasswordException.class) public void testDecyptBadPassword() throws DocumentException, IOException { byte[] crypted = createEncryptedPDF(); // NO password supplied, will throw BaddPasswordException PdfReader reader = new PdfReader(crypted, "badpassword".getBytes()); Assert.assertEquals(1, reader.getNumberOfPages()); reader.close(); }
/** Creates new PdfStamperImp. * @param reader the read PDF * @param os the output destination * @param pdfVersion the new pdf version or '\0' to keep the same version as the original * document * @param append * @throws DocumentException on error * @throws IOException */ PdfStamperImp(PdfReader reader, OutputStream os, char pdfVersion, boolean append) throws DocumentException, IOException { super(new PdfDocument(), os); if (!reader.isOpenedWithFullPermissions()) { throw new BadPasswordException("PdfReader not opened with owner password"); } if (reader.isTampered()) { throw new DocumentException("The original document was reused. Read it again from file."); } reader.setTampered(true); this.reader = reader; file = reader.getSafeFile(); this.append = append; if (append) { if (reader.isRebuilt()) { throw new DocumentException("Append mode requires a document without errors even if recovery was possible."); } if (reader.isEncrypted()) { crypto = new PdfEncryption(reader.getDecrypt()); } pdf_version.setAppendmode(true); file.reOpen(); byte buf[] = new byte[8192]; int n; while ((n = file.read(buf)) > 0) this.os.write(buf, 0, n); file.close(); prevxref = reader.getLastXref(); reader.setAppendable(true); } else { if (pdfVersion == 0) { super.setPdfVersion(reader.getPdfVersion()); } else { super.setPdfVersion(pdfVersion); } } super.open(); pdf.addWriter(this); if (append) { body.setRefnum(reader.getXrefSize()); marked = new IntHashtable(); if (reader.isNewXrefType()) { fullCompression = true; } if (reader.isHybridXref()) { fullCompression = false; } } initialXrefSize = reader.getXrefSize(); }
/** Creates new PdfStamperImp. * @param reader the read PDF * @param os the output destination * @param pdfVersion the new pdf version or '\0' to keep the same version as the original * document * @param append * @throws DocumentException on error * @throws IOException */ PdfStamperImp(PdfReader reader, OutputStream os, char pdfVersion, boolean append) throws DocumentException, IOException { super(new PdfDocument(), os); if (!reader.isOpenedWithFullPermissions()) throw new BadPasswordException("PdfReader not opened with owner password"); if (reader.isTampered()) throw new DocumentException("The original document was reused. Read it again from file."); reader.setTampered(true); this.reader = reader; file = reader.getSafeFile(); this.append = append; if (append) { if (reader.isRebuilt()) throw new DocumentException("Append mode requires a document without errors even if recovery was possible."); if (reader.isEncrypted()) crypto = new PdfEncryption(reader.getDecrypt()); pdf_version.setAppendmode(true); file.reOpen(); byte buf[] = new byte[8192]; int n; while ((n = file.read(buf)) > 0) this.os.write(buf, 0, n); file.close(); prevxref = reader.getLastXref(); reader.setAppendable(true); } else { if (pdfVersion == 0) super.setPdfVersion(reader.getPdfVersion()); else super.setPdfVersion(pdfVersion); } super.open(); pdf.addWriter(this); if (append) { body.setRefnum(reader.getXrefSize()); marked = new IntHashtable(); if (reader.isNewXrefType()) fullCompression = true; if (reader.isHybridXref()) fullCompression = false; } initialXrefSize = reader.getXrefSize(); }