com.lowagie.text.exceptions.BadPasswordException Java Examples

The following examples show how to use com.lowagie.text.exceptions.BadPasswordException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfCopyFieldsImp.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
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);
}
 
Example #2
Source File: PdfCopyFieldsImp.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #3
Source File: EncriptionTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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();
	
}
 
Example #4
Source File: EncriptionTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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();
	
}
 
Example #5
Source File: ITextDocumentReader.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * The OpenPDF implementation of the Reader
 * 
 * @param dssDocument {@link DSSDocument} to read
 * @param passwordProtection binaries of a password to open a protected document
 * @throws IOException if an exception occurs
 * @throws InvalidPasswordException if the password is not provided or invalid for a protected document
 */
public ITextDocumentReader(DSSDocument dssDocument, byte[] passwordProtection) throws IOException, InvalidPasswordException {
	Objects.requireNonNull(dssDocument, "The document must be defined!");
	try (InputStream is = dssDocument.openStream()) {
		this.pdfReader = new PdfReader(is, passwordProtection);
	} catch (BadPasswordException e) {
           throw new InvalidPasswordException(e.getMessage());
	}
}
 
Example #6
Source File: ITextDocumentReader.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * The OpenPDF implementation of the Reader
 * 
 * @param binaries a byte array of a PDF to read
 * @param passwordProtection binaries of a password to open a protected document
 * @throws IOException if an exception occurs
 * @throws eu.europa.esig.dss.pades.exception.InvalidPasswordException if the password is not provided or invalid for a protected document
 */
public ITextDocumentReader(byte[] binaries, byte[] passwordProtection) throws IOException, InvalidPasswordException {
	Objects.requireNonNull(binaries, "The document binaries must be defined!");
	try {
		this.pdfReader = new PdfReader(binaries, passwordProtection);
	} catch (BadPasswordException e) {
           throw new InvalidPasswordException(e.getMessage());
	}
}
 
Example #7
Source File: PdfStamperImp.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * 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.");
		}
		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();
}
 
Example #8
Source File: PdfStamperImp.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** 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();
}