> 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/open-save-document/save.md).

# Saving documents in Android

Learn how to save PDF documents using PDFNet's saving APIs from the PDFDoc class. Save to local storage, external storage, or incrementally for faster saving. Explore different saving modes for optimi

Use PDFNet saving APIs from [`PDFDoc`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFDoc.html) class to save PDF documents on local storage. The `PDFDoc` can be obtained from [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) using [`PDFViewCtrl.getDoc()`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html#getDoc\(\)).

## Save to a new PDF document

To save the current PDF document to a new file on internal storage use \[save]\(<https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFDoc.html#save(java.lang.String>, com.pdftron.sdf.SDFDoc.SaveMode, com.pdftron.pdf.ProgressMonitor)):

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

```java
void saveDocument(PDFViewCtrl pdfViewCtrl, final String filePath, final SDFDoc.SaveMode[] saveModes) throws Exception {
    final PDFDoc pdfDoc = pdfViewCtrl.getDoc();
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            pdfDoc.save(filePath, saveModes, null);
        }
    });
}
```

{% endcode %}
{% endtab %}

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

```kotlin
fun saveDocument(pdfViewCtrl: PDFViewCtrl, filePath: String?, saveModes: Array<SaveMode?>?) {
    val pdfDoc = pdfViewCtrl.doc
    pdfViewCtrl.docLock(true) { pdfDoc.save(filePath, saveModes, null) }
}
```

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

To save the document on external storage specified by a URI, you can create a [`Filter`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/filters/Filter.html) and use \[save(Filter, SaveMode\[])]\(<https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFDoc.html#save(com.pdftron.filters.Filter>, com.pdftron.sdf.SDFDoc.SaveMode)):

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

```java
void saveDocument(final PDFViewCtrl pdfViewCtrl, final Uri uri, final SDFDoc.SaveMode[] saveModes) throws Exception {
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            SecondaryFileFilter filter = null;
            try {
                filter = new SecondaryFileFilter(pdfViewCtrl.getContext(), uri);
                PDFDoc pdfDoc = pdfViewCtrl.getDoc();
                pdfDoc.save(filter, saveModes);
            } finally {
                if (filter != null) {
                    filter.close();
                }
            }
        }
    });
}
```

{% endcode %}
{% endtab %}

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

```kotlin
fun saveDocument(pdfViewCtrl: PDFViewCtrl, uri: Uri?, saveModes: Array<SaveMode?>?) {
    pdfViewCtrl.docLock(true) {
        var filter: SecondaryFileFilter? = null
        try {
            filter = SecondaryFileFilter(pdfViewCtrl.context, uri)
            val pdfDoc = pdfViewCtrl.doc
            pdfDoc.save(filter, saveModes)
        } finally {
            filter?.close()
        }
    }
}
```

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

## Save to the same PDF document

If you want to save changes on an opened PDF document you can save incrementally which is faster:

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

```java
void saveIncrementally(final PDFViewCtrl pdfViewCtrl) throws Exception {
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            pdfViewCtrl.getDoc().save();
        }
    });
}
```

{% endcode %}
{% endtab %}

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

```kotlin
fun saveIncrementally(pdfViewCtrl: PDFViewCtrl) {
    pdfViewCtrl.docLock(true) { pdfViewCtrl.doc.save() }
}
```

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

## Saving modes

There are a few ways to save a PDF document on permanent storage such as saving with removal of unused objects, incremental saving, and saving in linearized format. See complete list in [API details for SaveMode](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/sdf/SDFDoc.SaveMode.html).

### Save with remove unused objects

The [`REMOVE_UNUSED`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/sdf/SDFDoc.SaveMode.html) mode saves disk storage space by rearranging the PDF document and removing unused objects. As a result, the output file can be significantly smaller. However, the saving process itself may take longer compared to incremental mode.

### Save incrementally

Use the [`INCREMENTAL`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/sdf/SDFDoc.SaveMode.html) mode if you are particularly concerned about the time it takes to save the file. If you use this mode, any changes to the document, even the deletion of annotations, will be appended to the end of the PDF file. Note that it is not possible to incrementally save the document if the underlying file has an XRef table that had to be repaired when the file was opened (see <https://groups.google.com/forum/#!msg/pdfnet-sdk/GZwt_U16ipw/zhq43GQnNlYJ>).

### Save in linearized format

The [`LINEARIZED`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/sdf/SDFDoc.SaveMode.html) mode is to be preferentially used in file streaming situations, such as when you want to upload a PDF document. A linearized PDF file is a PDF file that is structured in a way that allows the first page of the PDF file to be displayed in a user's device before the entire file is downloaded from the Web server. If you're developing a system that creates large PDF files for delivery to client, you should try to make sure that your system can generate linearized PDF files. Your users might become frustrated and impatient if your system creates large PDF files that are not linearized, because it could take 30 seconds or even longer for a user's device to display a large PDF file that is not linearized. PDF linearization is also know as 'fast web view'.


---

# 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/open-save-document/save.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.
