InsertVideo.vb
C# VB
'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.IOImports System.DrawingImports System.LinqImports GrapeCity.Documents.PdfImports GrapeCity.Documents.Pdf.Annotations
'' This demo shows how to replace an image in a PDF with a video clip.Public Class InsertVideo Public Function CreatePDF(ByVal stream As Stream) As Integer Dim pdfPath = Path.Combine("Resources", "PDFs", "Wetlands.pdf") Dim videoPath = Path.Combine("Resources", "Video", "waterfall.mp4")
Using fs = File.OpenRead(pdfPath) Dim doc = New GcPdfDocument() doc.Load(fs) Dim page = doc.Pages.First()
' Find the largest image on the first page: Dim imageRect As RectangleF = RectangleF.Empty For Each img In page.GetImages() For Each l In img.Locations.Where(Function(l_) l_.Page.Index = 0) Dim r = l.PageBounds.ToRect() If r.Height > imageRect.Height Then imageRect = r End If Next Next If imageRect = RectangleF.Empty Then Throw New Exception("Could not find an image on the first page.") End If
' Remove it: doc.Redact(New RedactAnnotation() With {.Page = page, .Rect = imageRect})
' Add a video clip in the rectangle previously occupied by the image: Dim rma = New RichMediaAnnotation() Dim videoEfs = EmbeddedFileStream.FromFile(doc, videoPath) Dim 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
'' Done: doc.Save(stream) Return doc.Pages.Count End Using End FunctionEnd Class