PDF for WPF and Silverlight Overview > Features: PDF for WPF and Silverlight > Attaching Files to PDF Document |
Adding file attachments to PDF files is often a useful feature. Attachments can contain any kind of file, including spreadsheets with detailed information that would clutter the main document, multimedia files with movies and sound, sample code, and so on.
Adding file attachments to your PDF for WPF documents is easy. All you have to do is call the C1PdfDocument.AddAttachment method and specify which file you want to attach, what area of the page should contain the attachment, and optionally, the appearance of the attachment.
For example, the following code attaches all files in the application directory to the PDF document:
To write the code in Visual Basic:
Dim rect As New Rect(100, 100, 60, 10)
Dim font As New Font("Arial", 9)
' Attach some files.
Dim path As String = "c:\temp\files"
Dim file As String
For Each file In Directory.GetFiles(path)
Dim width As Single = rect.Width
rect.Width = rect.Height
_c1pdf.FillRectangle(Colors.Gray, rect)
_c1pdf.AddAttachment(file, rect)
rect.Width = width
rect.X += rect.Height
_c1pdf.DrawString(Path.GetFileName(file), font, Colors.Black, rect)
rect.X -= rect.Height
rect.Y += 2 * rect.Height
Next file
To write the code in C#:
Rect rect = new Rect(100, 100, 60, 10);
Font font = new Font("Arial", 9);
// Attach some files.
string path = @"c:\temp\files";
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
float width = rect.Width;
rect.Width = rect.Height;
_c1pdf.FillRectangle(Colors.Gray, rect);
_c1pdf.AddAttachment(file, rect);
rect.Width = width;
rect.X += rect.Height;
_c1pdf.DrawString(Path.GetFileName(file), font, Colors.Black, rect);
rect.X -= rect.Height;
rect.Y += 2 * rect.Height;
}