输入/输出操作

输入/输出模式

Aspose.PDF FOSS for Java consistently accepts both file paths and Java streams as 在所有主要API中输入和输出,包括: Document, PdfFileEditor,其他一切. 面向课程.

文件输入/输出

关于 Document 类接受一个文件路径字符串或一个 InputStream 在它的构造者. 关于 save() 方法接受一个文件路径字符串或一个 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);
}

对于大型文档,确保文件的存储量是相同. 足够的堆空间或考虑使用 PdfFileEditor.setUseDiskBuffer(true).

支持PdfFileEditor流程

PdfFileEditor 操作既接受文件路径字符串,也接受Java InputStream/OutputStream 目标:

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);

存储管道

对于避免临时文件的内存管道,使用: ByteArrayOutputStream 于 年 月 日 捕获文件输出,然后包装与 ByteArrayInputStream 后续加工:

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);

面部输入/输出

所有面积类 (例如:. PdfAnnotationEditor, PdfContentEditor, PdfFileSecurity) 遵循相同的绑定操作保存模式,并接受文件路径,流或一个 Document 通过他们的 bindPdf() 方法.

See Also

 中文