Skip to main content

Attachment Class

A file attachment stored in a PDF document.

Declaration

public class Attachment extends JavaObject

Remarks

The following code snippet attaches a text file to a PDF document:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.printing.DXPaperKind;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument()) {

            // Add an A4-size page to the document.
            pdfDocument.getPages().add(DXPaperKind.A4);

            // Create an attachment.
            Attachment attachment = new Attachment();
            attachment.setFileName("sample.txt");
            attachment.setData(Files.readAllBytes(Path.of("sample.txt")));
            attachment.setMimeType("text/plain");
            attachment.setDescription("Sample text file");
            attachment.setRelationship(AssociatedFileRelationship.SOURCE);

            // Add attachment to the document.
            pdfDocument.getAttachments().add(attachment);

            // Save the document.
            try (WritableByteChannel channel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(channel);
            }
        }
    }
}

Refer to the following help topic for additional information: Attach Files to PDF Documents.

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
Attachment

Constructors

Attachment() Constructor

Initializes a new instance of the Attachment class.

Declaration

public Attachment()

Attachment(String fileName, byte[] data) Constructor

Initializes a new instance of the Attachment class with specified settings.

Declaration

public Attachment(String fileName, byte[] data)

Parameters

Name Type Description
fileName String

The attachment file name.

data byte[]

The attachment data.

Methods

getCreationDate() Method

Returns the date when the attachment was created.

Declaration

public OffsetDateTime getCreationDate()

Returns

Type Description
OffsetDateTime

The date when the attachment was created.

getData() Method

Returns the binary content of the attached file.

Declaration

public byte[] getData()

Returns

Type Description
byte[]

The binary content of the attached file.

getDescription() Method

Returns the text that describes the attachment.

Declaration

public String getDescription()

Returns

Type Description
String

The attachment description.

getFileName() Method

Returns the attached file name.

Declaration

public String getFileName()

Returns

Type Description
String

The name of the attached file.

getMimeType() Method

Returns the MIME type of the attached file.

Declaration

public String getMimeType()

Returns

Type Description
String

MIME type of the attached file (for example, text/plain, application/pdf).

getModificationDate() Method

Returns the date when the attachment was last modified.

Declaration

public OffsetDateTime getModificationDate()

Returns

Type Description
OffsetDateTime

The date when the attachment was last modified.

getRelationship() Method

Returns the role of the file in the document.

Declaration

public AssociatedFileRelationship getRelationship()

Returns

Type Description
AssociatedFileRelationship

The role of the file in the document (for example, SOURCE).

setCreationDate(OffsetDateTime value) Method

Sets the date when the attachment was created.

Declaration

public void setCreationDate(OffsetDateTime value)

Parameters

Name Type Description
value OffsetDateTime

The date when the attachment was created.

setData(byte[] value) Method

Sets the binary content of the attached file.

Declaration

public void setData(byte[] value)

Parameters

Name Type Description
value byte[]

The binary content of the attached file.

setDescription(String value) Method

Sets the optional text that describes the attachment.

Declaration

public void setDescription(String value)

Parameters

Name Type Description
value String

The attachment description.

setFileName(String value) Method

Sets the attached file name.

Declaration

public void setFileName(String value)

Parameters

Name Type Description
value String

The name of the attached file.

setMimeType(String value) Method

Sets the MIME type of the attached file.

Declaration

public void setMimeType(String value)

Parameters

Name Type Description
value String

MIME type of the attached file (for example, text/plain, application/pdf).

setModificationDate(OffsetDateTime value) Method

Sets the date when the attachment was last modified.

Declaration

public void setModificationDate(OffsetDateTime value)

Parameters

Name Type Description
value OffsetDateTime

The date when the attachment was last modified.

setRelationship(AssociatedFileRelationship value) Method

Sets the role of the file in the document.

Declaration

public void setRelationship(AssociatedFileRelationship value)

Parameters

Name Type Description
value AssociatedFileRelationship

The role of the file in the document (for example, SOURCE).