/**
  * Extracts document level attachments
  *
  * @param filename a file from which document level attachments will be extracted
  * @throws IOException
  */
 public void extractDocLevelAttachments(String filename) throws IOException {
   PdfReader reader = new PdfReader(filename);
   PdfDictionary root = reader.getCatalog();
   PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
   PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
   PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
   PdfDictionary filespec;
   PdfDictionary refs;
   FileOutputStream fos;
   PRStream stream;
   for (int i = 0; i < filespecs.size(); ) {
     filespecs.getAsString(i++);
     filespec = filespecs.getAsDict(i++);
     refs = filespec.getAsDict(PdfName.EF);
     for (PdfName key : refs.getKeys()) {
       fos = new FileOutputStream(String.format(PATH, filespec.getAsString(key).toString()));
       stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
       fos.write(PdfReader.getStreamBytes(stream));
       fos.flush();
       fos.close();
     }
   }
   reader.close();
 }
 /**
  * Switches to the previous revision.
  *
  * @throws IOException
  * @throws GeneralSecurityException
  */
 public void switchToPreviousRevision() throws IOException, GeneralSecurityException {
   LOGGER.info("Switching to previous revision.");
   latestRevision = false;
   dss = reader.getCatalog().getAsDict(PdfName.DSS);
   Calendar cal = pkcs7.getTimeStampDate();
   if (cal == null) cal = pkcs7.getSignDate();
   // TODO: get date from signature
   signDate = cal.getTime();
   List<String> names = fields.getSignatureNames();
   if (names.size() > 1) {
     signatureName = names.get(names.size() - 2);
     reader = new PdfReader(fields.extractRevision(signatureName));
     this.fields = reader.getAcroFields();
     names = fields.getSignatureNames();
     signatureName = names.get(names.size() - 1);
     pkcs7 = coversWholeDocument();
     LOGGER.info(
         String.format(
             "Checking %ssignature %s",
             pkcs7.isTsp() ? "document-level timestamp " : "", signatureName));
   } else {
     LOGGER.info("No signatures in revision");
     pkcs7 = null;
   }
 }
 /**
  * Parses a string with structured content.
  *
  * @param reader the PdfReader that has access to the PDF file
  * @param os the OutputStream to which the resulting xml will be written
  * @param charset the charset to encode the data
  * @since 5.0.5
  */
 public void convertToXml(PdfReader reader, OutputStream os, String charset) throws IOException {
   this.reader = reader;
   OutputStreamWriter outs = new OutputStreamWriter(os, charset);
   out = new PrintWriter(outs);
   // get the StructTreeRoot from the root object
   PdfDictionary catalog = reader.getCatalog();
   PdfDictionary struct = catalog.getAsDict(PdfName.STRUCTTREEROOT);
   // Inspect the child or children of the StructTreeRoot
   inspectChild(struct.getDirectObject(PdfName.K));
   out.flush();
   out.close();
 }