How to Add Rich Media Files to PDF Using C# .NET
Tags: webdev, devops, csharp, tutorial
Published: 2024-05-31 17:20:55

---
canonical_url: https://developer.mescius.com/blogs/how-to-add-rich-media-files-to-pdf-using-c-sharp-net
description: Learn how to add Rich Media files to PDF Using C# .NET. See more from Document Solutions today.
---
**What You Will Need**
- Document Solutions for PDF
- Visual Studio
- NuGet

**Controls Referenced**
- [DsPDF](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/Document.html)

**Tutorial Concept**

Learn how to add rich media files to PDF documents using DsPdf in C# .NET

---
In today's digital age, static documents often fall short of effectively engaging audiences. Incorporating rich media files, such as videos and audio clips, into PDF documents can significantly enhance user experience and comprehension. In this blog post, we'll explore how to leverage [Document Solutions for PDF](https://developer.mescius.com/document-solutions/dot-net-pdf-api "https://developer.mescius.com/document-solutions/dot-net-pdf-api") (DsPdf), a .NET PDF API, to seamlessly integrate rich media files into PDF documents, thereby creating dynamic and captivating content. We'll delve into:

*   [The Benefits of Media Files in PDF Documents](#Benefits)
*   [Leveraging C# and PDF API](#Leveraging)
*   [Step-by-Step Implementation](#Step)

## <a id="Benefits"></a>Benefits **of Media Files in PDF Documents**

Adding media files to PDF documents enhances the interactivity and richness of the content, making it more engaging for the reader. Below, we provide a detailed overview of this process and its key features:

**1. Interactive Multimedia Content**  
Embedding videos and audio clips in PDFs creates a dynamic reading experience, allowing users to interact with the content directly within the document.  
**2. Support for Various Media Formats**  
PDFs can support a range of media formats, including MP3, OGG, WAV, MP4, SWF, WebM, and more, ensuring compatibility with different types of multimedia content.  
**3. Enhanced Presentations**  
Integrate multimedia elements in business presentations, educational materials, and marketing brochures to convey information more effectively.  
**4. Interactive Tutorials and E-Learning**  
Use media files in educational PDFs to include tutorials, demonstrations, and interactive learning modules.

## <a id="Leveraging"></a>Leveraging C# and PDF API

DsPdf offers the [**RichMediaAnnotation**](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/annotationtypes.html#i-heading-richmedia-annotation "https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/annotationtypes.html#i-heading-richmedia-annotation") class to incorporate multimedia support programmatically into your PDF documents.

The key capabilities of this class are as follows:

1.  **Embed Multimedia Content:** RichMedia annotations enable incorporating multimedia assets, including audio, video, and animations, into PDF files. This can enhance presentations, educational materials, or interactive forms.
2.  **Annotation Properties:** RichMedia annotations have properties that define how the multimedia content should be presented. These properties may include the activation conditions, visibility settings, and the appearance of the annotation.
3.  **Activation and Deactivation:** Activation conditions determine when the multimedia content should start or stop playing. For example, you can set the content to play when the user clicks the annotation or when the page containing the clip becomes visible.
4.  **Presentation Style:** RichMedia annotations support two presentation styles - **Embedded** and **Windowed**.
5.  **Control Navigation:** RichMedia annotations allow you to control the settings of navigation options to True or False.

## <a id="Step"></a>Step-by-Step Implementation

Let's outline the process of adding rich media files to an existing PDF document using C#. In this blog, we will replace an image with a video file.

1.  **Install PDF Library:** Begin by [installing DsPdf from the NuGet Package Manager](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/GettingStarted.html "https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/GettingStarted.html") in your C# project.
2.  **Load PDF Document:** Utilize DsPdf’s functionality to embed rich media files into the PDF document. For video or audio clips, specify the file path and stream. Then, load the file stream in DsPdf.  

```
        var pdfPath = Path.Combine("Resources", "PDFs", "Wetlands.pdf");
        var videoPath = Path.Combine("Resources", "Video", "waterfall.mp4");

        using var fs = File.OpenRead(pdfPath);
        var doc = new GcPdfDocument();
        doc.Load(fs);
```

3.**Remove the Image:** Find the largest image on the first page and remove it using [**RedactAnnotation**](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/annotationtypes.html#i-heading-redact-annotation "https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/annotationtypes.html#i-heading-redact-annotation").  

```
        var page = doc.Pages.First();
        RectangleF imageRect = RectangleF.Empty;
        foreach (var img in page.GetImages())
        {
              foreach (var l in img.Locations.Where(l_ => l_.Page.Index == 0))
              {
                            var r = l.PageBounds.ToRect();
                            if (r.Height > imageRect.Height)
                                imageRect = r;
               }
        }
        if (imageRect == RectangleF.Empty)
        throw new Exception("Could not find an image on the first page.");
        doc.Redact(new RedactAnnotation() { Page = page, Rect = imageRect });

```
4.**Add Rich Media:** Add a video clip in the rectangle previously occupied by the image. You can set various properties (like embedding a media file such as a video), the video's activation and deactivation conditions, and whether or not to show the navigation tools on the video.  

```
        var rma = new RichMediaAnnotation();
        var videoEfs = EmbeddedFileStream.FromFile(doc, videoPath);
        var videoFileSpec = FileSpecification.FromEmbeddedStream(Path.GetFileName(videoPath), videoEfs);
        rma.SetVideo(videoFileSpec);
        rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
        rma.ActivationCondition = RichMediaAnnotationActivation.PageBecomesVisible;
        rma.DeactivationCondition = RichMediaAnnotationDeactivation.PageBecomesInvisible;
        rma.ShowNavigationPane = true;
        rma.Page = page;
        rma.Rect = imageRect;
```

5.**Save PDF Document:** Once all properties have been added, save the PDF document.

```
        doc.Save(stream);
```

You have just added a media file to a PDF document! This is what it looks like in our JavaScript-based [Document Solutions PDF Viewer](https://developer.mescius.com/document-solutions/javascript-pdf-viewer "https://developer.mescius.com/document-solutions/javascript-pdf-viewer") :

![Rich Media File](//cdn.mescius.io/umb/media/0gsntxt0/richmediafile.gif?rmode=max&width=748&height=488)

### Learn More About DsPdf’s Support of Rich Media Files

*   .NET [Documentation](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/annotationtypes.html#i-heading-richmedia-annotation "https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/annotationtypes.html#i-heading-richmedia-annotation")
*   .NET [Demo](https://developer.mescius.com/document-solutions/dot-net-pdf-api/demos/features/annotations/rich-media/pdf-cs "https://developer.mescius.com/document-solutions/dot-net-pdf-api/demos/features/annotations/rich-media/pdf-cs")

### More Topics to Read

*   [Adding media files to PDF documents in client-side JavaScript PDF Viewer](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/RichMediaAnnotation.html "https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/RichMediaAnnotation.html")
*   [Adding rich media files programmatically to PDF Documents using JavaScript](https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/RichMediaAnnotation.html#i-heading-add-rich-media-programmatically "https://developer.mescius.com/document-solutions/dot-net-pdf-api/docs/online/RichMediaAnnotation.html#i-heading-add-rich-media-programmatically")
*   [JavaScript Demo](https://developer.mescius.com/document-solutions/javascript-pdf-viewer/demos/viewer-features/rich-media/purejs "https://developer.mescius.com/document-solutions/javascript-pdf-viewer/demos/viewer-features/rich-media/purejs")

What do you think about this feature? Please feel free to share any comments below.