int efficientPDFPageCount(String path) { RandomAccessFileOrArray file = new RandomAccessFileOrArray(path, false, true ); PdfReader reader = new PdfReader(file); int ret = reader.getNumberOfPages(); reader.close(); return ret; }
public void close() throws IOException { file.close(); }
private void readAnchorTable(int anchorTableLocation) throws IOException { rf.seek(anchorTableLocation); int anchorFormat = rf.readShort(); if (anchorFormat != 1) { System.err.println("The extra features of the AnchorFormat " + anchorFormat + " will not be used"); } int x = rf.readShort(); int y = rf.readShort(); }
private int readUnsignedShort(RandomAccessFileOrArray stream) throws IOException { if (isBigEndian) { return stream.readUnsignedShort(); } else { return stream.readUnsignedShortLE(); } }
private short readShort(RandomAccessFileOrArray stream) throws IOException { if (isBigEndian) { return stream.readShort(); } else { return stream.readShortLE(); } }
long pointer = stream.getFilePointer(); // Save stream pointer stream.seek(0L); int endian = stream.readUnsignedShort(); if (!isValidEndianTag(endian)) { throw new IllegalArgumentException(MessageLocalization.getComposedMessage("bad.endianness.tag.not.0x4949.or.0x4d4d")); stream.seek(4L); long offset = readUnsignedInt(stream, isBigEndian); stream.seek(offset); int entries = readUnsignedShort(stream, isBigEndian); stream.skip(12*entries); offset = readUnsignedInt(stream, isBigEndian); } catch(EOFException eof) { stream.seek(pointer); // Reset stream pointer return numDirectories;
protected byte[] getFullFont() throws IOException { RandomAccessFileOrArray rf2 = null; try { rf2 = new RandomAccessFileOrArray(rf); rf2.reOpen(); byte b[] = new byte[(int) rf2.length()]; rf2.readFully(b); return b; } finally { try { if (rf2 != null) { rf2.close(); } } catch (Exception e) { } } }
rf.seek(location[0]); int want = 4; long got = rf.skip(want); if (got < want) { throw new DocumentException("Skip TT font weight, expect read " + want + " bytes, but only got " + got); descr.setWeight(rf.readUnsignedShort()); want = 20; got = rf.skip(want); if (got < want) { throw new DocumentException("Skip TT font strikeout, expect read " + want + " bytes, but only got " + got); descr.setYStrikeoutSize(rf.readShort()); descr.setYStrikeoutPosition(rf.readShort()); rf.seek(location[0]); want = 8; got = rf.skip(want); if (got < want) { throw new DocumentException("Skip TT font underline, expect read " + want + " bytes, but only got " + got); descr.setUnderlinePosition(rf.readShort()); descr.setUnderlineThickness(rf.readShort()); rf.close(); rf = null; return rf;
/** Gets the contents of the page. * @param pageNum the page number. 1 is the first * @throws IOException on error * @return the content */ public byte[] getPageContent(final int pageNum) throws IOException{ RandomAccessFileOrArray rf = getSafeFile(); try { rf.reOpen(); return getPageContent(pageNum, rf); } finally { try{rf.close();}catch(Exception e){} } }
private TableHeader readHeader() throws IOException { rf.seek(tableLocation); // 32 bit signed int version = rf.readInt(); // 16 bit unsigned int scriptListOffset = rf.readUnsignedShort(); int featureListOffset = rf.readUnsignedShort(); int lookupListOffset = rf.readUnsignedShort(); // LOG.debug("version=" + version); // LOG.debug("scriptListOffset=" + scriptListOffset); // LOG.debug("featureListOffset=" + featureListOffset); // LOG.debug("lookupListOffset=" + lookupListOffset); TableHeader header = new TableHeader(version, scriptListOffset, featureListOffset, lookupListOffset); return header; }
protected void createTableDirectory() throws IOException, DocumentException { tableDirectory = new HashMap<String, int[]>(); rf.seek(directoryOffset); int id = rf.readInt(); if (id != 0x00010000) throw new DocumentException(MessageLocalization.getComposedMessage("1.is.not.a.true.type.file", fileName)); int num_tables = rf.readUnsignedShort(); rf.skipBytes(6); for (int k = 0; k < num_tables; ++k) { String tag = readStandardString(4); int tableLocation[] = new int[3]; tableLocation[TABLE_CHECKSUM] = rf.readInt(); tableLocation[TABLE_OFFSET] = rf.readInt(); tableLocation[TABLE_LENGTH] = rf.readInt(); tableDirectory.put(tag, tableLocation); } }
/** * Reads the glyphs widths. The widths are extracted from the table 'hmtx'. * The glyphs are normalized to 1000 units. * * @throws DocumentException the font is invalid * @throws IOException the font file could not be read */ protected void readGlyphWidths() throws DocumentException, IOException { int table_location[]; table_location = tables.get("hmtx"); if (table_location == null) throw new DocumentException(MessageLocalization.getComposedMessage("table.1.does.not.exist.in.2", "hmtx", fileName + style)); rf.seek(table_location[0]); glyphWidthsByIndex = new int[hhea.numberOfHMetrics]; for (int k = 0; k < hhea.numberOfHMetrics; ++k) { glyphWidthsByIndex[k] = rf.readUnsignedShort() * 1000 / head.unitsPerEm; @SuppressWarnings("unused") int leftSideBearing = rf.readShort() * 1000 / head.unitsPerEm; } }
void readSegment(JBIG2Segment s) throws IOException { int ptr = (int)ra.getFilePointer(); if ( s.dataLength == 0xffffffffl ) { // TODO figure this bit out, 7.2.7 return; } byte[] data = new byte[(int)s.dataLength]; ra.read(data); s.data = data; if ( s.type == PAGE_INFORMATION ) { int last = (int)ra.getFilePointer(); ra.seek(ptr); int page_bitmap_width = ra.readInt(); int page_bitmap_height = ra.readInt(); ra.seek(last); JBIG2Page p = pages.get(Integer.valueOf(s.page)); if ( p == null ) { throw new IllegalStateException(MessageLocalization.getComposedMessage("referring.to.widht.height.of.page.we.havent.seen.yet.1", s.page)); } p.pageBitmapWidth = page_bitmap_width; p.pageBitmapHeight = page_bitmap_height; } }
public static void populateDescription(String path, BaseFont font, FontDescription descr) throws IOException, NoSuchFieldException, IllegalAccessException, DocumentException { RandomAccessFileOrArray rf = null; try { rf = new RandomAccessFileOrArray(getTTCName(path)); rf = populateDescription0(path, font, descr, rf); } finally { if (rf != null) { try { rf.close(); } catch (IOException e) { // ignore } } } }
public String readLine() throws IOException { StringBuilder input = new StringBuilder(); int c = -1; boolean eol = false; while (!eol) { switch (c = read()) { case -1: case '\n': eol = true; break; case '\r': eol = true; long cur = getFilePointer(); if ((read()) != '\n') { seek(cur); } break; default: input.append((char)c); break; } } if ((c == -1) && (input.length() == 0)) { return null; } return input.toString(); }
long newpos; pos = getFilePointer(); len = length(); newpos = pos + n; if (newpos > len) { newpos = len; seek(newpos);
private void readScriptRecord(final int scriptListTableLocationOffset, Map<String, Integer> scriptRecords) throws IOException { String scriptTag = rf.readString(4, "utf-8"); int scriptOffset = rf.readShort(); scriptRecords.put(scriptTag, scriptListTableLocationOffset + scriptOffset); }
@Deprecated //TODO: remove all references to this call, then remove this method public void reOpen() throws IOException { seek(0); }
public long getStartxref() throws IOException { int arrLength = 1024; long fileLength = file.length(); long pos = fileLength - arrLength; if (pos < 1) pos = 1; while (pos > 0){ file.seek(pos); String str = readString(arrLength); int idx = str.lastIndexOf("startxref"); if (idx >= 0) return pos + idx; pos = pos - arrLength + 9; // 9 = "startxref".length() } throw new InvalidPdfException(MessageLocalization.getComposedMessage("pdf.startxref.not.found")); }
/** * The information in the maps of the table 'cmap' is coded in several formats. * Format 6 is a trimmed table mapping. It is similar to format 0 but can have * less than 256 entries. * * @return a <CODE>HashMap</CODE> representing this map * @throws IOException the font file could not be read */ HashMap<Integer, int[]> readFormat6() throws IOException { HashMap<Integer, int[]> h = new HashMap<Integer, int[]>(); rf.skipBytes(4); int start_code = rf.readUnsignedShort(); int code_count = rf.readUnsignedShort(); for (int k = 0; k < code_count; ++k) { int r[] = new int[2]; r[0] = rf.readUnsignedShort(); r[1] = getGlyphWidth(r[0]); h.put(Integer.valueOf(k + start_code), r); } return h; }