I/O Operations

I/O Patterns

Aspose.PDF FOSS for Java consistently accepts both file paths and Java streams as input and output in all major APIs, including Document, PdfFileEditor, and all facade classes.

Document I/O

The Document class accepts a file path string or an InputStream in its constructor. The save() method accepts a file path string or an OutputStream.

// File path input
try (Document doc = new Document("input.pdf")) {
    doc.save("output.pdf");
}

// Stream input/output
try (Document doc = new Document(inputStream)) {
    doc.save(outputStream);
}

Both constructors load the entire document into memory. For large documents, ensure sufficient heap space or consider using PdfFileEditor.setUseDiskBuffer(true).

PdfFileEditor Stream Support

PdfFileEditor operations accept both file path strings and Java InputStream/OutputStream objects:

PdfFileEditor editor = new PdfFileEditor();
// All operations support both file paths and streams
editor.concatenate(new InputStream[]{s1, s2}, outputStream);
editor.extract(inputStream, 1, 3, outputStream);

In-Memory Pipelines

For in-memory pipelines that avoid temporary files, use ByteArrayOutputStream to capture document output, then wrap with ByteArrayInputStream for downstream processing:

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (Document doc = new Document()) {
    doc.getPages().add();
    doc.save(buffer);
}
byte[] pdfBytes = buffer.toByteArray();
// Pass pdfBytes downstream — no temp file needed
ByteArrayInputStream next = new ByteArrayInputStream(pdfBytes);

Facade I/O

All facade classes (e.g. PdfAnnotationEditor, PdfContentEditor, PdfFileSecurity) follow the same bind-operate-save pattern and accept file paths, streams, or a Document object via their bindPdf() method.

See Also