Saving PDF documents

In this document
chevron_rightSaving modes
chevron_rightSave incrementally

Use PDFNet saving APIs from PDFDoc class to save PDF documents on local storage. The PDFDoc can be obtained from PDFViewCtrl using PDFViewCtrl.getDoc().

Save to a new PDF document

To save the current PDF document to a new file on internal storage use save(String, SDFDoc.SaveMode[], ProgressMonitor):

String filepath = ...
PDFDoc pdfDoc = mPdfViewCtrl.getDoc();
SDFDoc.SaveMode[] saveModes = new SDFDoc.SaveMode[]{SDFDoc.SaveMode.REMOVE_UNUSED};
boolean shouldUnlock = false;
try {
    mPdfViewCtrl.docLock(true);
    shouldUnlock = true;
    pdfDoc.save(filepath, saveModes, null);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (shouldUnlock) {
        mPdfViewCtrl.docUnlock();
    }
}

To save the document on external storage specified by a URI, you can create a Filter and use save(Filter, SaveMode[]):

Uri uri = ...
PDFDoc pdfDoc = mPdfViewCtrl.getDoc();
SDFDoc.SaveMode[] saveModes = new SDFDoc.SaveMode[]{SDFDoc.SaveMode.REMOVE_UNUSED};
SecondaryFileFilter filter = null;
boolean shouldUnlock = false;
try {
    mPdfViewCtrl.docLock(true);
    shouldUnlock = true;
    filter = new SecondaryFileFilter(getContext(), uri);
    pdfDoc.save(filter, saveModes);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (shouldUnlock) {
        mPdfViewCtrl.docUnlock();
    }
    if (filter != null) {
        filter.close();
    }
}

Save to the same PDF document

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

boolean shouldUnlock = false;
try {
    mPdfViewCtrl.docLock(true);
    shouldUnlock = true;
    if (shouldUnlock) {
        mPdfDoc.save(); // save incrementally
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (shouldUnlock) {
        docUnlock();
    }
}

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. A complete list of saving modes is available here.

Save with remove unused objects

The REMOVE_UNUSED 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 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 http://www.pdftron.com/kbcorruptxref).

Save in linearized format

The LINEARIZED mode is to be preferentially used in file streaming situations, such as when you want to upload a PDF document. It will rearrange the PDF document and remove unused objects. However, the saving process itself may take longer compared to other modes.