Opening and saving
Open PDFs from files, content URIs and bytes, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, linearize and copy documents with NexPDF for Android.
License module
Opening PDFs and pictures, saving and copying are part of the viewer module and work without a
key. Creating a PDF from HTML, Office or e-book files needs the conversion module (Startup tier and
above).
Open bytes, a file or a content URI
Every open call takes the document's bytes. Read them from wherever the file is: a file in app
storage, a content: URI from the system picker or another app, the assets, or a download. Do it
off the main thread for large files.
import com.nexdevit.nexpdf.NexPdfDocument
val fromFile = File(filesDir, "contract.pdf").readBytes()
val fromUri = contentResolver.openInputStream(uri)!!.use { it.readBytes() }
NexPdfDocument.openBytes(fromUri).use { doc ->
Log.i("NexPDF", "${doc.pageCount()} pages")
}openBytes also accepts JPEG, PNG and TIFF pictures (one page per picture), and converts Word,
Excel and PowerPoint files, EPUB and FictionBook books, XPS/OpenXPS packages and CBZ comics; see
below.
A document holds native memory. Close it with close(), use { } or try-with-resources; after
close() every method throws IllegalStateException. A document that is never closed is never
freed.
Encrypted documents
NexPdfDocument.openBytes(bytes) fails with password required for an encrypted file. Pass the
user (open) password or the owner password to NexPdfDocument.openBytes(bytes, password). A
missing or wrong password throws IllegalArgumentException with password required or
incorrect password, and NexPdfDocument.isPasswordError(e) tells those apart from other
failures:
val doc = try {
NexPdfDocument.openBytes(bytes, "s3cret")
} catch (e: IllegalArgumentException) {
if (NexPdfDocument.isPasswordError(e)) askTheUserAgain() else Log.e("NexPDF", e.message ?: "")
return
}
doc.use { Log.i("NexPDF", it.pageText(0)) }Office files, e-books and comics carry no PDF password: with a password, openBytes converts them
as usual and ignores it. For a raw handle, NexPdfJni.openBytesWithPassword(bytes, password) returns
0 on failure, with the reason in NexPdfJni.lastError(); free it with NexPdfJni.free.
RC4, AES-128 and AES-256 files are supported. Files that are only restricted by permissions (no
open password) open with openBytes. NexPdfJni.securityJson(handle) reports the encryption and
permissions; see Security.
Create a PDF from other formats
| Source | Function | Module |
|---|---|---|
| JPEG, PNG, TIFF | NexPdfDocument.openBytes(bytes) or openImage(bytes) | none |
| HTML | NexPdfDocument.fromHtml(html) | conversion |
| Word, Excel, PowerPoint | NexPdfDocument.openBytes(bytes) or openOffice(bytes) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | NexPdfDocument.openBytes(bytes) or openEbook(bytes) | conversion |
NexPdfDocument.openBytes(tiffBytes).use { scan -> // one page per TIFF frame
File(filesDir, "scan.pdf").writeBytes(scan.saveBytes())
}
NexPdfDocument.fromHtml("<h1>Invoice 42</h1><p>Total: 120 EUR</p>").use { invoice ->
File(filesDir, "invoice.pdf").writeBytes(invoice.saveBytes())
}
NexPdfDocument.openBytes(epubBytes).use { book -> // EPUB, FictionBook, XPS or CBZ
File(filesDir, "book.pdf").writeBytes(book.saveBytes())
}openBytes reads the file names inside a ZIP (without decompressing anything) to pick the e-book or
the Office converter. To add pictures to an existing document, use
NexPdfJni.insertImagePages(handle, index, bytes); see Editing.
Save
saveBytes() returns the document with every change as a complete new file. Write it wherever it
goes: a file, a content: URI from ACTION_CREATE_DOCUMENT, or an upload.
val pdf = doc.saveBytes()
File(filesDir, "out.pdf").writeBytes(pdf)
contentResolver.openOutputStream(targetUri)!!.use { it.write(pdf) }For a raw handle, NexPdfJni.saveBytes(handle) does the same.
Fast Web View
A linearized file shows its first page before the download ends, which helps when PDFs are served
over HTTP. saveLinearized() returns such a file (NexPdfJni.saveLinearized(handle) for a raw
handle):
val webReady = doc.saveLinearized()Signed files
Signing returns the finished signed file. Write those bytes as they are: saving the document object again rewrites the file and breaks the signature. See Signatures.
Copy a document
There is no copy method. Open the saved bytes to get an independent document with the same content and changes, for example as an undo snapshot or to hand work to another thread:
val snapshot = NexPdfDocument.openBytes(doc.saveBytes())In the reader
The reader opens a content: or file: URI given in its intent, converts
Word, Excel and PowerPoint files and pictures, asks for the password of encrypted files, and lets
the user save with Save as.
Related
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory