PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Use CPDFDocument to add, list, and remove embedded files, and CPDFAttachment to read or export them. The document in each example is already open.
sourcePath is a readable local file path, and name is the attachment name:
import com.compdfkit.core.attachment.CPDFAttachment;
CPDFAttachment attachment = document.addAttachment(sourcePath, "specification.txt");
if (attachment != null) {
try {
boolean updated = attachment.setDescription("Project specification");
// Check updated to confirm the description was written.
} finally {
attachment.close();
}
} else {
// Addition failed. Check the document and source file.
}After adding, removing, changing descriptions, or writing data, save the document to persist the changes. Releasing an attachment handle does not remove the embedded file.
import com.compdfkit.core.attachment.CPDFAttachmentInfo;
int count = document.getAttachmentCount();
for (CPDFAttachmentInfo info : document.getAttachmentList()) {
int attachmentIndex = info.getIndex();
String name = info.getName();
String description = info.getDescription();
String mimeType = info.getMimeType();
long sizeBytes = info.getSizeBytes();
// Display the metadata and keep attachmentIndex for subsequent operations.
}Attachments are stored in a PDF name tree. Enumeration order may differ from insertion order. Use info.getIndex() rather than the list position to access an attachment. Refresh the list after adding or removing attachments, and discard indexes from the old list.
Obtain attachmentIndex from CPDFAttachmentInfo.getIndex() in the current list. outputPath must be writable; create its parent directory first:
CPDFAttachment attachment = document.getAttachment(attachmentIndex);
if (attachment != null) {
try {
boolean exported = attachment.saveTo(outputPath);
// Check exported to confirm export succeeded.
} finally {
attachment.close();
}
}| Method | Purpose |
|---|---|
read() | Reads the attachment into a byte[]; consider memory usage for large files |
saveTo(String), saveTo(File) | Exports to a local file |
writeTo(OutputStream) | Writes to an application-provided stream; the caller closes the stream and handles IOException |
write(byte[]) | Replaces attachment data; check the result and save the document |
setDescription(String) | Changes the description; check the result and save the document |
Run attachment operations on a background thread. Check return values and do not treat a failed read as valid attachment content.
Remove an attachment using its current valid index:
boolean removed = document.removeAttachment(attachmentIndex);
if (removed) {
// Refresh the attachment list, then save the document.
}You can also call remove() on a valid attachment object. Do not reuse a handle after removal or document closure. Call close() on every acquired CPDFAttachment and finish attachment operations before closing the document.