One of the basic building blocks of a PDF document is an SDF stream object. For example, in a PDF document all page content, images, embedded fonts, and files are represented using object streams that can be compressed and encrypted using various Filter chains. See the "Stream Objects" and "Filters" chapters in the PDF Reference Manual for more details.
Apryse SDK supports an efficient and flexible architecture for processing streams using filter pipelines.
A filter is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. A filter can also perform certain transformations of input/output data (e.g. data compression/decompression, color conversion, and so on).
Input filters/streams
Apryse SDK enables generic input from external files using the MappedFile filter. Use MappedFile to open, read from, and close files on a file system. For example:
1Dim file As MappedFile = New MappedFile(filename)
2Dim doc_stream As SDFDoc = New SDFDoc(file)
Opens an external image file for reading. MappedFile buffers input and output for better performance. Although it is possible to read input data directly through the Filter interface (MappedFile is a subclass of Filter), it is more convenient to attach a FilterReader to the filter and then read data through FilterReader interface:
1long file_sz = file.FileSize();
2FilterReader file_reader = new FilterReader(file);
3byte[] mem = new byte[(int) file_sz];
4long bytes_read = file_reader.Read(mem);
5SDFDoc doc_mem = new SDFDoc(mem, file_sz);
1size_t file_sz = file.FileSize();
2FilterReader file_reader(file);
3unsigned char* mem = new unsigned char[file_sz];
4file_reader.Read((unsigned char*)mem, file_sz);
5SDFDoc doc_mem(mem, file_sz);
1fileSZ := file.FileSize()
2fileReader := NewFilterReader(file)
3mem := fileReader.Read(fileSZ)
4memBytes := make([]byte, int(mem.Size()))
5for i := 0; i < int(mem.Size()); i++{
6 memBytes[i] = mem.Get(i)
7}
8doc := NewSDFDoc(&memBytes[0], fileSZ)
1long file_sz = file.fileSize();
2FilterReader file_reader = new FilterReader(file);
2Dim dec_stm As Filter = stream.GetDecodedStream()
3Dim reader As FilterReader = New FilterReader(dec_stm)
Stream.GetRawStream() creates a Filter used to extract raw data as it appears in a serialized SDF document (or a decrypted version of the stream if the document is secured). Stream.GetDecodedStream() creates a Filter pipeline and returns the last filter in the chain. For example, a given stream may be compressed using JPEG (DCTDecode) compression and encoded using ASCII85 into an ASCII stream. When GetDecodedStream() is invoked on this SDF stream, it will return the last filter in a chain that composed of three filters (the file segment input Filter, the DCTDecode Filter, and the ASCII85Decode Filter, respectively). Data extracted from the returned Filter will be raw image data (i.e. RGB byte triples).
It's possible to iterate through the Filter chain using the Filter.GetAttachedFilter() method. It's also possible to construct new filter chains, and to edit existing ones, using the Filter.AttachFilter() method.
Output filters/streams
To write a filter to a file, simply use Filter.WriteToFile():
1Dim writer As FilterWriter = New FilterWriter(dec_stm)
2writer.writeString("Hello World")
3writer.flush()
Implement custom filters
Apryse SDK provides full support for all common Filters used in PDF. Although included Filters should cover all common use case scenarios, advanced users may want to provide custom implementations for certain filters (e.g. custom color conversion, or a new compression method). Apryse SDK provides an open and expandable architecture for creation of custom filters. To implement a custom Filter, derive a new class from Filter base class and implement the required interface. A more detailed guide for implementing custom Filters is available through Apryse Systems developer program.