Opening and saving
Open PDFs from files and bytes, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, linearize and copy documents with NexPDF for Java.
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 a file or bytes
NexPdfDocument.open(path) reads a file; NexPdfDocument.openBytes(data) takes the bytes you
already have, for example from an upload or object storage. open throws IOException when the
file cannot be read.
import com.nexdevit.nexpdf.NexPdfDocument;
import java.nio.file.Files;
import java.nio.file.Path;
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("contract.pdf"))) {
System.out.println(doc.pageCount() + " pages");
}
byte[] data = Files.readAllBytes(Path.of("contract.pdf"));
try (NexPdfDocument doc = NexPdfDocument.openBytes(data)) {
System.out.println(doc.pageCount());
}Both accept JPEG, PNG and TIFF pictures too (one page per picture). With the conversion module,
Word, Excel and PowerPoint files, EPUB and zipped FictionBook books, XPS files and CBZ comics are
detected and converted on open.
A document holds native memory. Close it with close() or try-with-resources; after close() any
call throws IllegalStateException, and isClosed() tells you whether it is closed.
Encrypted documents
Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported.
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("locked.pdf"), "s3cret")) {
System.out.println(doc.securityJson());
} catch (NexPdfException e) {
if (e.getMessage().equals("password required") || e.getMessage().equals("incorrect password")) {
askTheUserAgain();
} else {
throw e;
}
}openBytes(data, password) does the same for bytes. Office files, e-books and comics carry no PDF
password: since 1.1 they are converted as usual and the password is ignored, so you can pass the
password a user typed for any file. Files that are only restricted by permissions
(no open password) open without a password. doc.securityJson() reports the encryption and
permissions; see Security.
Create a PDF from other formats
| Source | Method | Module |
|---|---|---|
| JPEG, PNG, TIFF | NexPdfDocument.openImage(data) | none |
| HTML | NexPdfDocument.fromHtml(html) | conversion |
| Word, Excel, PowerPoint | NexPdfDocument.openOffice(data) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | NexPdfDocument.openEbook(data) | conversion |
The same methods exist as static methods of NexPdf.
try (NexPdfDocument doc = NexPdfDocument.openImage(Files.readAllBytes(Path.of("scan.tiff")))) {
doc.save(Path.of("scan.pdf")); // one page per TIFF frame
}
try (NexPdfDocument doc = NexPdfDocument.fromHtml("<h1>Invoice 42</h1><p>Total: 120 EUR</p>")) {
doc.save(Path.of("invoice.pdf"));
}To add pictures to an existing document, use insertImagePages(index, data); see
Editing.
Save
save(path) writes the document with every change as a complete new file. saveBytes() returns the
same bytes instead of writing a file.
doc.save(Path.of("out.pdf"));
byte[] pdf = doc.saveBytes(); // e.g. to return from a web handlerFast Web View
A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:
doc.save(Path.of("web.pdf"), true);
byte[] web = 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
copy() returns an independent document with the same content and changes. Use it as an undo
snapshot, or to hand work to another thread:
NexPdfDocument snapshot = doc.copy();
doc.deletePage(0);
// ... undo:
doc.close();
doc = snapshot;Related
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory