Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
||||||||||||||||||||||||||







Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
In this article, we will see how to embed a file in a PDF document. For the user, an embedded file is an attachment. In PDF terms, it is a file attachment annotation.
To embed a file as an attachment, we need to first create a file attachment annotation. Then, the PDF document needs to be loaded and the annotation has to be added to the document. The output document then needs to be saved to a file.
In this article, we will create an application that will allow a user to select a PDF document and embed a file in it.
using statement for Gnostice.PDFOne.
private void button1_Click_1(object sender, EventArgs e)
{
openFileDialog1.Filter = "PDF documents (*.pdf)|*.pdf";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Any file (*.*)|*.*";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
if ((textBox1.Text.Length > 0) &&
(textBox2.Text.Length > 0)) {
PDFDocument doc = new PDFDocument();
try
{
// Create a file annotation at position (1,1)
// with the file specified in the second text box
PDFFileAttachmentAnnot fa =
new PDFFileAttachmentAnnot(1f, 1f, textBox2.Text);
fa.Color = Color.YellowGreen;
try
{
// Load the PDF document specified in the
// first text box
doc.Load(textBox1.Text);
// Add the annotation
doc.AddAnnot(fa);
// Set the output file to be opened
// after it is saved
doc.OpenAfterCreate = true;
// Save the output document
doc.Save("output.pdf");
// Close the input document
doc.Close();
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show(
"Sorry, I could not find the PDF document.",
"Error");
}
catch (PDFException pdfe)
{
MessageBox.Show(pdfe.Message, "Error");
}
finally
{
doc.Dispose();
}
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show(
"Sorry, I could not find the attachment file.",
"Error");
}
catch (PDFException pdfe)
{
MessageBox.Show(pdfe.Message, "Error");
}
}
}


---o0O0o---
| Our .NET Developer Tools | |
|---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
| Our Delphi/C++Builder developer tools | |
|---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
|
| Our Java developer tools | |
|---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
| Our Platform-Agnostic Cloud and On-Premises APIs | |
|---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
| Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2026 Gnostice Information Technologies Private Limited. All rights reserved. |