Skip to main content

Embedding Files

Version: 12.0

Embedding Files

The DynamicPDF Core Suite allows embedding any file type in a PDF document as an attachment.

Use the EmbeddedFile class to attach a file to a PDF document. To embed a file you use a Document class instance's EmbeddedFiles property to Add a file.

The Document class has an EmbeddedFiles property that consists of an b. You add an EmbeddedFile instance to this list using its Add method.

Example​

The following example illustrates adding two PDF documents, an image, and a word document as attachments to a PDF document.

info

The document.InitialPageMode is optional and used here to ensure the PDF shows attachments when opened.

Document document = new Document();
document.Pages.Add(new Page(PageSize.Letter));

Label label = new Label("Embedded Files Example", 50, 20, 215, 0);
document.Pages[0].Elements.Add(label);

string fileOne = "DocumentA.pdf";
string fileTwo = "DocumentB.pdf";
string fileThree = "DPDFLogo.png";
string fileFour = "Doc1.docx";

EmbeddedFile embeddedFile1 = new EmbeddedFile(fileOne);
EmbeddedFile embeddedFile2 = new EmbeddedFile(fileTwo);
EmbeddedFile embeddedFile3 = new EmbeddedFile(fileThree);
EmbeddedFile embeddedFile4 = new EmbeddedFile(fileFour);

document.EmbeddedFiles.Add(embeddedFile1);
document.EmbeddedFiles.Add(embeddedFile2);
document.EmbeddedFiles.Add(embeddedFile3);
document.EmbeddedFiles.Add(embeddedFile4);

document.InitialPageMode = PageMode.ShowAttachments;

document.Draw(outputPath);
Dim document As New Document()
document.Pages.Add(New Page(PageSize.Letter))
Dim label As New Label("Embedded Files Example", 50, 20, 215, 0)
document.Pages(0).Elements.Add(label)

Dim fileOne As String = "DocumentA.pdf"
Dim fileTwo As String = "DocumentB.pdf"
Dim fileThree As String = "DPDFLogo.png"
Dim fileFour As String = "Doc1.docx"

Dim embeddedFile1 As New EmbeddedFile(fileOne)
Dim embeddedFile2 As New EmbeddedFile(fileTwo)
Dim embeddedFile3 As New EmbeddedFile(fileThree)
Dim embeddedFile4 As New EmbeddedFile(fileFour)

document.EmbeddedFiles.Add(embeddedFile1)
document.EmbeddedFiles.Add(embeddedFile2)
document.EmbeddedFiles.Add(embeddedFile3)
document.EmbeddedFiles.Add(embeddedFile4)

document.Draw(outputPath)

[EmbeddingFilesExample.cs](https://github.com/DynamicPDF/examples-dynamicpdf-core-suite-dotnet-core/blob/master/dynamicpdf-csharp-examples/Examples/EmbeddingFilesExample.cs){#gitlink-id target=_blank}


[EmbeddingFilesExample.vb](https://github.com/DynamicPDF/examples-dynamicpdf-core-suite-dotnet-core/blob/master/dynamicpdf-vbnet-examples/Examples/EmbeddingFilesExample.vb){#gitlink-id target=_blank}

^^^ Figure 1. A PDF document with four embedded files as attachments. ^^^