Java Code Examples for com.lowagie.text.pdf.RandomAccessFileOrArray

The following examples show how to use com.lowagie.text.pdf.RandomAccessFileOrArray. These examples are extracted from open source projects. 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
@Test
public void transparentTiffTest() throws IOException {
    InputStream inputStream = TiffReadingTest.class.getClassLoader().getResourceAsStream("gradient.tiff");
    byte[] data;

    try(ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) {
        int bytesRead;
        byte[] buffer = new byte[8192];

        while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
            byteOutputStream.write(buffer, 0, bytesRead);
        }

        data = byteOutputStream.toByteArray();
    }

    RandomAccessFileOrArray ra = new RandomAccessFileOrArray(data);
    int pages = TiffImage.getNumberOfPages(ra);

    for (int i = 1; i <= pages; i++) {
        Assert.assertNotNull(TiffImage.getTiffImage(ra, i));
    }
}
 
Example 2
Source Project: gcs   Source File: PdfTextExtractor.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Gets the content stream of a page.
 * @param pageNum	the page number of page you want get the content stream from
 * @return	a byte array with the content stream of a page
 * @throws IOException
 */
private byte[] getContentBytesForPage(int pageNum) throws IOException {
    RandomAccessFileOrArray f = reader.getSafeFile();
    byte[] contentBytes = reader.getPageContent(pageNum, f);
    f.close();
    return contentBytes;
}
 
Example 3
Source Project: gcs   Source File: PdfContentReaderTool.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Writes information about a specific page from PdfReader to the specified output stream.
 * 
 * @since 2.1.5
 * @param reader the PdfReader to read the page content from
 * @param pageNum the page number to read
 * @param out the output stream to send the content to
 * @throws IOException
 */
static public void listContentStreamForPage(PdfReader reader, int pageNum, PrintWriter out) throws IOException {
	out.println("==============Page " + pageNum + "====================");
	out.println("- - - - - Dictionary - - - - - -");
	PdfDictionary pageDictionary = reader.getPageN(pageNum);
	out.println(getDictionaryDetail(pageDictionary));
	out.println("- - - - - Content Stream - - - - - -");
	RandomAccessFileOrArray f = reader.getSafeFile();

	byte[] contentBytes = reader.getPageContent(pageNum, f);
	f.close();

	InputStream is = new ByteArrayInputStream(contentBytes);
	int ch;
	while ((ch = is.read()) != -1) {
		out.print((char) ch);
	}

	out.println("- - - - - Text Extraction - - - - - -");
	PdfTextExtractor extractor = new PdfTextExtractor(reader);
	String extractedText = extractor.getTextFromPage(pageNum);
	if (extractedText.length() != 0) {
		out.println(extractedText);
	} else {
		out.println("No text found on page " + pageNum);
	}

	out.println();

}
 
Example 4
Source Project: gcs   Source File: TiffImage.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/** Gets the number of pages the TIFF document has.
 * @param s the file source
 * @return the number of pages
 */    
public static int getNumberOfPages(RandomAccessFileOrArray s) {
    try {
        return TIFFDirectory.getNumDirectories(s);
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example 5
Source Project: gcs   Source File: JBIG2Image.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Gets a byte array that can be used as a /JBIG2Globals,
 * or null if not applicable to the given jbig2.
 * @param	ra	an random access file or array
 * @return	a byte array
 */
public static byte[] getGlobalSegment(RandomAccessFileOrArray ra ) {
	try {
		JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
		sr.read();
		return sr.getGlobal(true);
	} catch (Exception e) {
        return null;
    }
}
 
Example 6
Source Project: gcs   Source File: JBIG2Image.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * returns an Image representing the given page.
 * @param ra	the file or array containing the image
 * @param page	the page number of the image
 * @return	an Image object
 */
public static Image getJbig2Image(RandomAccessFileOrArray ra, int page) {
	if (page < 1)
           throw new IllegalArgumentException("The page number must be >= 1.");
	
	try {
		JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
		sr.read();
		JBIG2SegmentReader.JBIG2Page p = sr.getPage(page);
		Image img = new ImgJBIG2(p.pageBitmapWidth, p.pageBitmapHeight, p.getData(true), sr.getGlobal(true));
		return img;
	} catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example 7
Source Project: gcs   Source File: JBIG2Image.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/***
 * Gets the number of pages in a JBIG2 image.
 * @param ra	a random acces file array containing a JBIG2 image
 * @return	the number of pages
 */
public static int getNumberOfPages(RandomAccessFileOrArray ra) {
	try {
		JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
		sr.read();
		return sr.numberOfPages();
	} catch (Exception e) {
        throw new ExceptionConverter(e);
    }
   }
 
Example 8
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory from a SeekableStream. The directory parameter specifies which
 * directory to read from the linked list present in the stream; directory 0 is normally read
 * but it is possible to store multiple images in a single TIFF file by maintaining multiple
 * directories.
 *
 * @param stream a SeekableStream to read from.
 * @param directory the index of the directory to read.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, int directory) throws IOException {

	long global_save_offset = stream.getFilePointer();
	long ifd_offset;

	// Read the TIFF header
	stream.seek(0L);
	int endian = stream.readUnsignedShort();
	if (!isValidEndianTag(endian)) {
		throw new IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
	}
	isBigEndian = endian == 0x4d4d;

	int magic = readUnsignedShort(stream);
	if (magic != 42) {
		throw new IllegalArgumentException("Bad magic number, should be 42.");
	}

	// Get the initial ifd offset as an unsigned int (using a long)
	ifd_offset = readUnsignedInt(stream);

	for (int i = 0; i < directory; i++) {
		if (ifd_offset == 0L) {
			throw new IllegalArgumentException("Directory number too large.");
		}

		stream.seek(ifd_offset);
		int entries = readUnsignedShort(stream);
		stream.skip(12 * entries);

		ifd_offset = readUnsignedInt(stream);
	}

	stream.seek(ifd_offset);
	initialize(stream);
	stream.seek(global_save_offset);
}
 
Example 9
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory by reading a SeekableStream. The ifd_offset parameter specifies
 * the stream offset from which to begin reading; this mechanism is sometimes used to store
 * private IFDs within a TIFF file that are not part of the normal sequence of IFDs.
 *
 * @param stream a SeekableStream to read from.
 * @param ifd_offset the long byte offset of the directory.
 * @param directory the index of the directory to read beyond the one at the current stream
 *            offset; zero indicates the IFD at the current offset.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory) throws IOException {

	long global_save_offset = stream.getFilePointer();
	stream.seek(0L);
	int endian = stream.readUnsignedShort();
	if (!isValidEndianTag(endian)) {
		throw new IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
	}
	isBigEndian = endian == 0x4d4d;

	// Seek to the first IFD.
	stream.seek(ifd_offset);

	// Seek to desired IFD if necessary.
	int dirNum = 0;
	while (dirNum < directory) {
		// Get the number of fields in the current IFD.
		int numEntries = readUnsignedShort(stream);

		// Skip to the next IFD offset value field.
		stream.seek(ifd_offset + 12 * numEntries);

		// Read the offset to the next IFD beyond this one.
		ifd_offset = readUnsignedInt(stream);

		// Seek to the next IFD.
		stream.seek(ifd_offset);

		// Increment the directory.
		dirNum++;
	}

	initialize(stream);
	stream.seek(global_save_offset);
}
 
Example 10
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private short readShort(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readShort();
	} else {
		return stream.readShortLE();
	}
}
 
Example 11
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private int readUnsignedShort(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readUnsignedShort();
	} else {
		return stream.readUnsignedShortLE();
	}
}
 
Example 12
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private int readInt(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readInt();
	} else {
		return stream.readIntLE();
	}
}
 
Example 13
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private long readUnsignedInt(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readUnsignedInt();
	} else {
		return stream.readUnsignedIntLE();
	}
}
 
Example 14
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private long readLong(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readLong();
	} else {
		return stream.readLongLE();
	}
}
 
Example 15
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private float readFloat(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readFloat();
	} else {
		return stream.readFloatLE();
	}
}
 
Example 16
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private double readDouble(RandomAccessFileOrArray stream) throws IOException {
	if (isBigEndian) {
		return stream.readDouble();
	} else {
		return stream.readDoubleLE();
	}
}
 
Example 17
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private static int readUnsignedShort(RandomAccessFileOrArray stream, boolean isBigEndian) throws IOException {
	if (isBigEndian) {
		return stream.readUnsignedShort();
	} else {
		return stream.readUnsignedShortLE();
	}
}
 
Example 18
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
private static long readUnsignedInt(RandomAccessFileOrArray stream, boolean isBigEndian) throws IOException {
	if (isBigEndian) {
		return stream.readUnsignedInt();
	} else {
		return stream.readUnsignedIntLE();
	}
}
 
Example 19
Source Project: gcs   Source File: TIFFDirectory.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the number of image directories (subimages) stored in a given TIFF file, represented
 * by a <code>SeekableStream</code>.
 */
public static int getNumDirectories(RandomAccessFileOrArray stream) throws IOException {
	long pointer = stream.getFilePointer(); // Save stream pointer

	stream.seek(0L);
	int endian = stream.readUnsignedShort();
	if (!isValidEndianTag(endian)) {
		throw new IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
	}
	boolean isBigEndian = endian == 0x4d4d;
	int magic = readUnsignedShort(stream, isBigEndian);
	if (magic != 42) {
		throw new IllegalArgumentException("Bad magic number, should be 42.");
	}

	stream.seek(4L);
	long offset = readUnsignedInt(stream, isBigEndian);

	int numDirectories = 0;
	while (offset != 0L) {
		++numDirectories;

		// EOFException means IFD was probably not properly terminated.
		try {
			stream.seek(offset);
			int entries = readUnsignedShort(stream, isBigEndian);
			stream.skip(12 * entries);
			offset = readUnsignedInt(stream, isBigEndian);
		} catch (EOFException eof) {
			// numDirectories--;
			break;
		}
	}

	stream.seek(pointer); // Reset stream pointer
	return numDirectories;
}
 
Example 20
/**
 * Gets the content stream of a page.
 * @param pageNum	the page number of page you want get the content stream from
 * @return	a byte array with the content stream of a page
 * @throws IOException
 */
private byte[] getContentBytesForPage(int pageNum) throws IOException {
    RandomAccessFileOrArray f = reader.getSafeFile();
    byte[] contentBytes = reader.getPageContent(pageNum, f);
    f.close();
    return contentBytes;
}
 
Example 21
/**
 * Writes information about a specific page from PdfReader to the specified output stream.
 * @since 2.1.5
 * @param reader    the PdfReader to read the page content from
 * @param pageNum   the page number to read
 * @param out       the output stream to send the content to
 * @throws IOException
 */
static public void listContentStreamForPage(PdfReader reader, int pageNum, PrintWriter out) throws IOException {
    out.println("==============Page " + pageNum + "====================");
    out.println("- - - - - Dictionary - - - - - -");
    PdfDictionary pageDictionary = reader.getPageN(pageNum);
    out.println(getDictionaryDetail(pageDictionary));
    out.println("- - - - - Content Stream - - - - - -");
    RandomAccessFileOrArray f = reader.getSafeFile();
    
    byte[] contentBytes = reader.getPageContent(pageNum, f);
    f.close();

    
    InputStream is = new ByteArrayInputStream(contentBytes);
    int ch;
    while ((ch = is.read()) != -1){
        out.print((char)ch);
    }

    out.println("- - - - - Text Extraction - - - - - -");
    PdfTextExtractor extractor = new PdfTextExtractor(reader);
    String extractedText = extractor.getTextFromPage(pageNum);
    if (extractedText.length() != 0)
        out.println(extractedText);
    else
        out.println("No text found on page " + pageNum);
    
    out.println();
    
}
 
Example 22
Source Project: itext2   Source File: TiffImage.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Gets the number of pages the TIFF document has.
 * @param s the file source
 * @return the number of pages
 */    
public static int getNumberOfPages(RandomAccessFileOrArray s) {
    try {
        return TIFFDirectory.getNumDirectories(s);
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example 23
Source Project: itext2   Source File: JBIG2Image.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets a byte array that can be used as a /JBIG2Globals,
 * or null if not applicable to the given jbig2.
 * @param	ra	an random access file or array
 * @return	a byte array
 */
public static byte[] getGlobalSegment(RandomAccessFileOrArray ra ) {
	try {
		JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
		sr.read();
		return sr.getGlobal(true);
	} catch (Exception e) {
        return null;
    }
}
 
Example 24
Source Project: itext2   Source File: JBIG2Image.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * returns an Image representing the given page.
 * @param ra	the file or array containing the image
 * @param page	the page number of the image
 * @return	an Image object
 */
public static Image getJbig2Image(RandomAccessFileOrArray ra, int page) {
	if (page < 1)
           throw new IllegalArgumentException("The page number must be >= 1.");
	
	try {
		JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
		sr.read();
		JBIG2SegmentReader.JBIG2Page p = sr.getPage(page);
		Image img = new ImgJBIG2(p.pageBitmapWidth, p.pageBitmapHeight, p.getData(true), sr.getGlobal(true));
		return img;
	} catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example 25
Source Project: itext2   Source File: JBIG2Image.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/***
 * Gets the number of pages in a JBIG2 image.
 * @param ra	a random acces file array containing a JBIG2 image
 * @return	the number of pages
 */
public static int getNumberOfPages(RandomAccessFileOrArray ra) {
	try {
		JBIG2SegmentReader sr = new JBIG2SegmentReader(ra);
		sr.read();
		return sr.numberOfPages();
	} catch (Exception e) {
        throw new ExceptionConverter(e);
    }
   }
 
Example 26
Source Project: itext2   Source File: TIFFDirectory.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a TIFFDirectory by reading a SeekableStream.
 * The ifd_offset parameter specifies the stream offset from which
 * to begin reading; this mechanism is sometimes used to store
 * private IFDs within a TIFF file that are not part of the normal
 * sequence of IFDs.
 *
 * @param stream a SeekableStream to read from.
 * @param ifd_offset the long byte offset of the directory.
 * @param directory the index of the directory to read beyond the
 *        one at the current stream offset; zero indicates the IFD
 *        at the current offset.
 */
public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory)
throws IOException {
    
    long global_save_offset = stream.getFilePointer();
    stream.seek(0L);
    int endian = stream.readUnsignedShort();
    if (!isValidEndianTag(endian)) {
        throw new
        IllegalArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
    }
    isBigEndian = (endian == 0x4d4d);
    
    // Seek to the first IFD.
    stream.seek(ifd_offset);
    
    // Seek to desired IFD if necessary.
    int dirNum = 0;
    while(dirNum < directory) {
        // Get the number of fields in the current IFD.
        int numEntries = readUnsignedShort(stream);
        
        // Skip to the next IFD offset value field.
        stream.seek(ifd_offset + 12*numEntries);
        
        // Read the offset to the next IFD beyond this one.
        ifd_offset = readUnsignedInt(stream);
        
        // Seek to the next IFD.
        stream.seek(ifd_offset);
        
        // Increment the directory.
        dirNum++;
    }
    
    initialize(stream);
    stream.seek(global_save_offset);
}
 
Example 27
Source Project: itext2   Source File: TIFFDirectory.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private short readShort(RandomAccessFileOrArray stream)
throws IOException {
    if (isBigEndian) {
        return stream.readShort();
    } else {
        return stream.readShortLE();
    }
}
 
Example 28
Source Project: itext2   Source File: TIFFDirectory.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private int readUnsignedShort(RandomAccessFileOrArray stream)
throws IOException {
    if (isBigEndian) {
        return stream.readUnsignedShort();
    } else {
        return stream.readUnsignedShortLE();
    }
}
 
Example 29
Source Project: itext2   Source File: TIFFDirectory.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private int readInt(RandomAccessFileOrArray stream)
throws IOException {
    if (isBigEndian) {
        return stream.readInt();
    } else {
        return stream.readIntLE();
    }
}
 
Example 30
Source Project: itext2   Source File: TIFFDirectory.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private long readUnsignedInt(RandomAccessFileOrArray stream)
throws IOException {
    if (isBigEndian) {
        return stream.readUnsignedInt();
    } else {
        return stream.readUnsignedIntLE();
    }
}