Pdf - PdfAttachmentCollection

18 Nov 20263 minutes to read

Represents the collection of attachments embedded in a PDF document.

// Load an existing PDF document.
let document: PdfDocument = new PdfDocument(data, password);
// Get the attachments embedded in the document.
let attachments: PdfAttachmentCollection = document.attachments;
// Get the attachment count.
let count: number = attachments.count;
// Get attachment data from the document attachment collection.
let attachmentData: Uint8Array = attachments.getItem(0).data;
// Add a new attachment to the PDF document and get its index.
let index: number = attachments.add(new PdfAttachment(attachmentData, 'input.txt'));
// Add an attachment at a specified index.
attachments.add(new PdfAttachment(attachmentData, 'inserted.txt'), 0);
// Save and destroy the document.
document.save('output.pdf');
document.destroy();

Properties

Get count number

Gets the number of document-level file attachments.

Methods

add

Adds an attachment to the PDF document.

Parameter Type Description
attachment PdfAttachment The attachment to add.

Returns number

add

Adds an attachment to the PDF document at the specified index.

Parameter Type Description
attachment PdfAttachment The attachment to add.
index number The zero-based insertion index.

Returns void

clear

Removes all document-level file attachments and clears the embedded-files name tree.

Returns void

getItem

Returns the attachment at the specified zero-based index.

Parameter Type Description
index number The zero-based index of the attachment to retrieve.

Returns PdfAttachment

has

Returns whether the specified attachment belongs to this document.

Parameter Type Description
attachment PdfAttachment The attachment instance to check.

Returns boolean

remove

Removes the specified attachment instance from the document.

Parameter Type Description
attachment PdfAttachment The attachment instance to remove.

Returns void

removeAt

Removes the attachment at the specified zero-based index.

Parameter Type Description
index number The zero-based index of the attachment to remove.

Returns void