/** * 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(); }
private void fillMetrics(byte[] touni, IntHashtable widths, int dw) { try { PdfContentParser ps = new PdfContentParser(new PRTokeniser(touni)); PdfObject ob = null; boolean notFound = true; int nestLevel = 0; int maxExc = 50; while ((notFound || nestLevel > 0)) { try { ob = ps.readPRObject(); } catch (Exception ex) { if (--maxExc < 0) break; continue; } if (ob == null) break; if (ob.type() == PdfContentParser.COMMAND_TYPE) { if (ob.toString().equals("begin")) { notFound = false; nestLevel++; } else if (ob.toString().equals("end")) { nestLevel--; } else if (ob.toString().equals("beginbfchar")) { while (true) { PdfObject nx = ps.readPRObject(); if (nx.toString().equals("endbfchar")) break; String cid = decodeString((PdfString) nx); String uni = decodeString((PdfString) ps.readPRObject()); if (uni.length() == 1) { int cidc = cid.charAt(0); int unic = uni.charAt(uni.length() - 1); int w = dw; if (widths.containsKey(cidc)) w = widths.get(cidc); metrics.put(Integer.valueOf(unic), new int[] {cidc, w}); } } } else if (ob.toString().equals("beginbfrange")) { while (true) { PdfObject nx = ps.readPRObject(); if (nx.toString().equals("endbfrange")) break; String cid1 = decodeString((PdfString) nx); String cid2 = decodeString((PdfString) ps.readPRObject()); int cid1c = cid1.charAt(0); int cid2c = cid2.charAt(0); PdfObject ob2 = ps.readPRObject(); if (ob2.isString()) { String uni = decodeString((PdfString) ob2); if (uni.length() == 1) { int unic = uni.charAt(uni.length() - 1); for (; cid1c <= cid2c; cid1c++, unic++) { int w = dw; if (widths.containsKey(cid1c)) w = widths.get(cid1c); metrics.put(Integer.valueOf(unic), new int[] {cid1c, w}); } } } else { PdfArray a = (PdfArray) ob2; for (int j = 0; j < a.size(); ++j, ++cid1c) { String uni = decodeString(a.getAsString(j)); if (uni.length() == 1) { int unic = uni.charAt(uni.length() - 1); int w = dw; if (widths.containsKey(cid1c)) w = widths.get(cid1c); metrics.put(Integer.valueOf(unic), new int[] {cid1c, w}); } } } } } } } } catch (Exception e) { throw new ExceptionConverter(e); } }