pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation
PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance

PDF Content Extraction
PDF Navigation

PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2015-05-25

How to add or open pdf attachments - managing embedded files

Introduction


Embedded files, so-called pdf attachments, are used to guarantee the validity of the document in case there are external references in main content referring to them. For example, you can include supplementary documentation or appendix as an embedded file and link to it from some page of the main document. They can also be used in the same way as email attachments, and pdf document will act as “envelope” containing additional data. Using embedded files, PDF documents can be turned to self-contained structured units.

Detailed description of this PDF feature is contained in section 7.11.4 Embedded File Streams of the PDF specification.  

Adding PDF attachments


Code sample below shows how to add attachments to PDF document:

public void AddPDFAttachments()
{
    // create document
    FixedDocument doc = new FixedDocument();

    // add attachments
    doc.Attachments.Add("Appendix-A",new EmbeddedFile("appendix_a.txt"));
    doc.Attachments.Add("Appendix-B",new EmbeddedFile("appendix_b.jpg"));

    // create text object
    Page page = new Page();
    TextObject textObject = new TextObject(StandardFonts.Helvetica,14);
    textObject.AppendText("Document with attachments, created using Apitron PDF Kit
    for .NET");
    page.Content.Translate(10,820);
    page.Content.AppendText(textObject);

    // add first page
    doc.Pages.Add(page);

    // save document
    using (Stream stream = File.Create("pdf_attachments.pdf"))
    {
        doc.Save(stream);
    }
}

Resulting image:

Pic. 1 Add pdf attachments

Pic. 1 Add pdf attachments

Note the panel on the left, with a paperclip, it allows you to view all attachments and open the desired file for viewing. You can also enumerate and read attachments programmatically, see the next sample.

Reading pdf attachments


See the code sample below that enumerates all attachments and prints their names and sizes:

public void ReadPDFAttachments()
{
    // open document
    using (Stream stream = File.Open("pdf_attachments.pdf",FileMode.Open))
    {
        FixedDocument doc = new FixedDocument(stream);

        // enumerate attachments, print their names and sizes
        foreach (var fileEntry in doc.Attachments)
        {
            using (Stream attachmentStream = fileEntry.Value.GetStream())
            {
                Console.WriteLine("Found attachment: {0}, Size: {1} bytes",
                    fileEntry.Key,attachmentStream.Length);  
            }           
        }       
    }

    Console.ReadLine();
}

This code produces the following results:

Pic. 2 Enumerating and reading pdf attachments

Pic. 2 Enumerating and reading pdf attachments

You could also remove any attachment using the single call:

doc.Attachments.RemoveAttachment([attachment key]);

Conclusion


Embedded files feature is very useful for including additional data into the main PDF document without changing its content. And as you could see from this article, managing attachments is quite easy with fixed layout API offered by Apitron PDF Kit for .NET component. This pdf manipulation library can be used to add, remove, enumerate, or read any files attached to pdf documents. Contact us if you have any questions and we’ll be happy to answer.

No comments:

Post a Comment