PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

How to Embed a File in a PDF Document as an Attachment

Learn to do it in .NET using PDFOne .NET.
By V. Subhash

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.

  1. Create a new C# project.
  2. Add a reference to the PDFOne .NET component DLL file and add the using statement for Gnostice.PDFOne.
  3. On the default form, add a OpenFileDialog component.
  4. Add a label, a text box, and a button to the form.
  5. Change the label text to "Enter the pathname of a PDF document".
  6. Change the button text to "Browse".
  7. Copy and paste the label, the text box, and the button on the form
  8. Change the text of the second label to "Enter the pathname of an attachment".
  9. Add two more buttons for "Attach" and "Close", as shown below.
  10. Specify the following code for button-click event of the first button.
    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;
     }
    }
    
  11. Specify the following code for button-click event of the second "Browse" button.
    private void button2_Click(object sender, EventArgs e)
    {
     openFileDialog1.Filter = "Any file (*.*)|*.*";
     openFileDialog1.Multiselect = false;
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
      textBox2.Text = openFileDialog1.FileName;
     }
    }
    
  12. Specify the following code for button-click event of the "Embed" button.
    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");
      } 
     }  
    }
    
  13. Add code to close the form to the button-click event of the "Close" button.
  14. Compile and run the application.
  15. Select the input files and click the "Attach" button."

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .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 Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A 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 Java

Multi-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
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.