Skip to main content Skip to footer

How to Add Rich Media Files to PDF Using C# .NET

Quick Start Guide
What You Will Need

Document Solutions for PDF

Visual Studio

NuGet

Controls Referenced DsPDF
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 (DsPdf), a .NET PDF API, to seamlessly integrate rich media files into PDF documents, thereby creating dynamic and captivating content. We'll delve into:

Ready to Get Started? Download Document Solutions for PDF Today!

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.

Leveraging C# and PDF API

DsPdf offers the RichMediaAnnotation 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.

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 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.
    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 :

Rich Media File

Ready to Try It Out? Download Document Solutions for PDF Today!

Learn More About DsPdf’s Support of Rich Media Files

More Topics to Read

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

 

comments powered by Disqus