> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/low-level-pdf-api/filters.md).

# Filters and streams in Android

Learn about SDF stream objects in PDF documents, filter pipelines, and more with Apryse SDK. Explore efficient ways to process streams and access data using various filters. Dive into the details now!

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:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
MappedFile file = new MappedFile(filename);
SDFDoc doc_stream = new SDFDoc(file);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val file = MappedFile(filename)
val doc_stream = SDFDoc(file)
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
long file_sz = file.fileSize();
FilterReader file_reader = new FilterReader(file);
byte[] mem = new byte[(int) file_sz];
long bytes_read = file_reader.read(mem);
SDFDoc doc_mem = new SDFDoc(mem, file_sz);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val file_sz = file.fileSize()
val file_reader = FilterReader(file)
val mem = ByteArray(file_sz.toInt())
val bytes_read = file_reader.read(mem)
val doc_mem = SDFDoc(mem)
```

{% endcode %}
{% endtab %}
{% endtabs %}

Data associated with SDF stream objects can be accessed using `Stream.GetRawStream()` or `Stream.GetDecodedStream()` methods.

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Obj stream = doc.getTrailer();
Filter dec_stm = stream.getDecodedStream();
FilterReader reader = new FilterReader(dec_stm);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val stream = doc.trailer()
val dec_stm = stream.getDecodedStream();
val reader = FilterReader(dec_stm)
```

{% endcode %}
{% endtab %}
{% endtabs %}

`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():`

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
dec_stm.writeToFile(output_filename, false);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
dec_stm.writeToFile(output_filename, false);
```

{% endcode %}
{% endtab %}
{% endtabs %}

To modify or add to an output file filter/stream, simply use the FilterWriter class:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
FilterWriter writer = new FilterWriter(dec_stm);
writer.writeString("Hello World");
writer.flush();
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val writer = new FilterWriter(dec_stm);
writer.writeString("Hello World");
writer.flush();
```

{% endcode %}
{% endtab %}
{% endtabs %}

## 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.

Please contact [support@apryse.com](https://docs.apryse.com) for more details.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/low-level-pdf-api/filters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
