org.apache.pdfbox.io.RandomAccessRead Java Examples

The following examples show how to use org.apache.pdfbox.io.RandomAccessRead. 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: PdfBleachSession.java    From DocBleach with MIT License 6 votes vote down vote up
private PDDocument getDocument(RandomAccessRead source) throws IOException, BleachException {
  PDDocument doc;
  for (String pwd : COMMON_PASSWORDS) {
    ScratchFile scratchFile = new ScratchFile(MEMORY_USAGE_SETTING);
    doc = testPassword(scratchFile, source, pwd);
    if (doc != null) {
      LOGGER.debug("Password was guessed: '{}'", pwd);
      doc.protect(new StandardProtectionPolicy(pwd, pwd, doc.getCurrentAccessPermission()));
      return doc;
    }
    scratchFile.close();
  }

  // @TODO: fetch password from config?

  throw new BleachException("PDF is protected with an unknown password");
}
 
Example #2
Source File: PdfBleachSession.java    From DocBleach with MIT License 6 votes vote down vote up
@SuppressFBWarnings(
    value = "EXS_EXCEPTION_SOFTENING_RETURN_FALSE",
    justification = "This method is an helper to check the password")
private PDDocument testPassword(ScratchFile inFile, RandomAccessRead source, String password)
    throws IOException {
  PDFParser parser = new PDFParser(source, password, inFile);
  try {
    parser.parse();
    return parser.getPDDocument();
  } catch (InvalidPasswordException e) {
    LOGGER.error("The tested password is invalid");
    return null;
  } finally {
    source.rewind((int) source.getPosition());
  }
}
 
Example #3
Source File: PdfBleachSession.java    From DocBleach with MIT License 6 votes vote down vote up
void sanitize(RandomAccessRead source, OutputStream outputStream)
    throws IOException, BleachException {
  final PDDocument doc = getDocument(source);

  final PDDocumentCatalog docCatalog = doc.getDocumentCatalog();

  sanitizeNamed(doc, docCatalog.getNames());
  PDDocumentCatalogBleach catalogBleach = new PDDocumentCatalogBleach(this);
  catalogBleach.sanitize(docCatalog);
  sanitizeDocumentOutline(doc.getDocumentCatalog().getDocumentOutline());

  cosObjectBleach.sanitizeObjects(doc.getDocument().getObjects());

  doc.save(outputStream);
  doc.close();
}
 
Example #4
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Parses a PDF. Depending on the memory settings parameter the given input stream is either
 * copied to memory or to a temporary file to enable random access to the pdf.
 *
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param password 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 memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password, InputStream keyStore, 
                              String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    try
    {
        RandomAccessRead source = scratchFile.createBuffer(input);
        PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
        parser.parse();
        return parser.getPDDocument();
    }
    catch (IOException ioe)
    {
        IOUtils.closeQuietly(scratchFile);
        throw ioe;
    }
}
 
Example #5
Source File: PdfBleach.java    From DocBleach with MIT License 5 votes vote down vote up
@Override
public void sanitize(InputStream inputStream, OutputStream outputStream, BleachSession session)
    throws BleachException {
  try (RandomAccessRead source = new RandomAccessBufferedFileInputStream(inputStream)) {
    new PdfBleachSession(session).sanitize(source, outputStream);
  } catch (IOException e) {
    throw new BleachException(e);
  }
}
 
Example #6
Source File: SignatureOptions.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void initFromRandomAccessRead(RandomAccessRead rar) throws IOException
{
    pdfSource = rar;
    PDFParser parser = new PDFParser(pdfSource);
    parser.parse();
    visualSignature = parser.getDocument();
}
 
Example #7
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor for encrypted pdfs.
 * 
 * @param source input representing the pdf.
 * @param password password to be used for decryption.
 * @param keyStore key store to be used for decryption when using public key security
 * @param keyAlias alias to be used for decryption when using public key security
 * 
 */
public COSParser(RandomAccessRead source, String password, InputStream keyStore,
        String keyAlias)
{
    super(new RandomAccessSource(source));
    this.source = source;
    this.password = password;
    this.keyAlias = keyAlias;
    keyStoreInputStream = keyStore;
}
 
Example #8
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 *
 * @param source input representing the pdf.
 */
public COSParser(RandomAccessRead source)
{
    super(new RandomAccessSource(source));
    this.source = source;
}
 
Example #9
Source File: COSWriter.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * COSWriter constructor for incremental updates. There must be a path of objects that have
 * {@link COSUpdateInfo#isNeedToBeUpdated()} set, starting from the document catalog. For
 * signatures this is taken care by PDFBox itself.
 *
 * @param outputStream output stream where the new PDF data will be written. It will be closed
 * when this object is closed.
 * @param inputData random access read containing source PDF data
 *
 * @throws IOException if something went wrong
 */
public COSWriter(OutputStream outputStream, RandomAccessRead inputData) throws IOException
{
    // write to buffer instead of output
    setOutput(new ByteArrayOutputStream());
    setStandardOutput(new COSStandardOutputStream(output, inputData.length()));

    incrementalInput = inputData;
    incrementalOutput = outputStream;
    incrementalUpdate = true;
}
 
Example #10
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Parses a PDF.
 * 
 * @param input byte array that contains the document.
 * @param password 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 memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(byte[] input, String password, InputStream keyStore, 
        String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    RandomAccessRead source = new RandomAccessBuffer(input);
    PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
    parser.parse();
    return parser.getPDDocument();
}
 
Example #11
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Constructor that uses an existing document. The COSDocument that is passed in must be valid.
 * 
 * @param doc The COSDocument that this document wraps.
 * @param source the parser which is used to read the pdf
 * @param permission he access permissions of the pdf
 * 
 */
public PDDocument(COSDocument doc, RandomAccessRead source, AccessPermission permission)
{
    document = doc;
    pdfSource = source;
    accessPermission = permission;
}
 
Example #12
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #13
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * 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());
}
 
Example #14
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor that uses an existing document. The COSDocument that is passed in must be valid.
 * 
 * @param doc The COSDocument that this document wraps.
 * @param source the parser which is used to read the pdf
 */
public PDDocument(COSDocument doc, RandomAccessRead source)
{
    this(doc, source, null);
}
 
Example #15
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #16
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * 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());
}
 
Example #17
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * 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());
}
 
Example #19
Source File: RandomAccessSource.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * 
 * @param reader The random access reader to wrap.
 */
RandomAccessSource(RandomAccessRead reader)
{
    this.reader = reader;
}