Saving documents in Android

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:

1void saveDocument(PDFViewCtrl pdfViewCtrl, final String filePath, final SDFDoc.SaveMode[] saveModes) throws Exception {
2 final PDFDoc pdfDoc = pdfViewCtrl.getDoc();
3 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
4 @Override
5 public void run() throws Exception {
6 pdfDoc.save(filePath, saveModes, null);
7 }
8 });
9}

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

1void saveDocument(final PDFViewCtrl pdfViewCtrl, final Uri uri, final SDFDoc.SaveMode[] saveModes) throws Exception {
2 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
3 @Override
4 public void run() throws Exception {
5 SecondaryFileFilter filter = null;
6 try {
7 filter = new SecondaryFileFilter(pdfViewCtrl.getContext(), uri);
8 PDFDoc pdfDoc = pdfViewCtrl.getDoc();
9 pdfDoc.save(filter, saveModes);
10 } finally {
11 if (filter != null) {
12 filter.close();
13 }
14 }
15 }
16 });
17}

Save to the same PDF document

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

1void saveIncrementally(final PDFViewCtrl pdfViewCtrl) throws Exception {
2 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
3 @Override
4 public void run() throws Exception {
5 pdfViewCtrl.getDoc().save();
6 }
7 });
8}

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.

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 https://groups.google.com/forum/#!msg/pdfnet-sdk/GZwt_U16ipw/zhq43GQnNlYJ).

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales