SwfImportOperation Import Function. Imports selected frames of a Flash movie. ABCpdf .NET PDF Library.

ABCpdf .net

 
   

Imports selected frames of a Flash movie.

 

   
Syntax  

[C#]
void Import(string path)
void Import(Stream stream, string url)

[Visual Basic]
Sub Import(path As String)
Sub Import(stream As Stream, url As String)

Throws Exceptions may throw Exception()

 

   

Params
 
Name Description
path The file path to the Flash movie.
stream The stream containing the Flash movie.
url The value of the MovieClip._url ActionScript property.

 

   

Notes
 

Imports selected frames of a Flash movie. By default, only one frame is imported. ActionScript 2 or below is supported. ActionScript 3 is not supported.

Alpha Blending. PDF supports using different color spaces for alpha blending. When there is a transparent object on a page, the blending color space will be used. By default, it uses DeviceCMYK. You will likely see some color shifts even for opaque objects because colors are converted to the alpha blending color space and then to DeviceRGB for display. The effect is more noticeable for objects using component-wise alpha blending.

To use RGB as it is used in SWF for alpha blending, set the default blending color space for the page to DeviceRGB:

doc.SetInfo(doc.Page, "/Group*/Type:Name", "Group");
doc.SetInfo(doc.Page, "/Group*/S:Name", "Transparency");
doc.SetInfo(doc.Page, "/Group*/CS:Name", "DeviceRGB");

You must specify a Doc before the ProcessingObject event of ProcessingSourceType.ImageFrame returns/is handled.

An exception will be thrown if the operation is not possible. This may happen if the Flash movie is corrupt.

    This method generates events as follows:
  1. Before the Flash engine advances, a ProcessingObject event of ProcessingSourceType.MultiFrameImage is generated. If ResetRegions is true, BackgroundRegion and ClipRegion are reset to the frame bounds/stage size of the Flash movie when the event is generated. The regions are set as if XRect.SetRect is called with the X, Y, Width, and Height properties of the ProcessingInfo. You can provide parameters in Parameters for initializing the SWF machine before the first time a proper FrameNumber is returned in this event.
    Name Description
    ProcessingObjectEventArgs.Cancel See ProcessingObjectEventArgs.Info.FrameNumber below.
    ProcessingObjectEventArgs.Info.Handled Ignored.
    ProcessingObjectEventArgs.Info.SourceType ProcessingSourceType.MultiFrameImage
    ProcessingObjectEventArgs.Info.FrameCount The number of frames in the Flash movie.
    ProcessingObjectEventArgs.Info.FrameNumber The positive number of frames from the start of the movie to elapse before rendering. This value is not bounded by FrameCount. The value of this property for the first of such events is one. Subsequent value is null. The event handler may set this value to a number equal to or greater than the value set in a previous such event. If the value is set smaller, the corresponding ProcessedObject event indicating failure is generated. Set it to null to end the processing of the entire movie. If it is null, the corresponding ProcessedObject event is not generated. Otherwise, if ProcessingObjectEventArgs.Cancel is set to true, the corresponding ProcessedObject event is not generated, and the same event is immediately generated again with this value set to null.
    ProcessingObjectEventArgs.Info.FrameRate The number of frames per second of the Flash movie.
    ProcessingObjectEventArgs.Info.X The x-coordinate (in twips) of the frame bounds of the Flash movie.
    ProcessingObjectEventArgs.Info.Y The y-coordinate (in twips) of the frame bounds of the Flash movie.
    ProcessingObjectEventArgs.Info.Width The width (in twips) of the stage of the Flash movie.
    ProcessingObjectEventArgs.Info.Height The height (in twips) of the stage of the Flash movie.
  2. After a proper FrameNumber is returned in a previous ProcessingObject event of ProcessingSourceType.MultiFrameImage and the Flash engine advances to the frame, a ProcessingObject event of ProcessingSourceType.ImageFrame is generated. If ResetRegions is true, BackgroundRegion and ClipRegion are reset to the frame bounds/stage size of the Flash movie when the event is generated because the frame actions may have changed the stage scale mode. You must specify a Doc before this event returns/is handled. The frame is rendered on to Doc.Rect of the current page using Doc.Transform.
    Name Description
    ProcessingObjectEventArgs.Cancel If this property is set to true, the frame is not rendered and the corresponding ProcessedObject event is not generated.
    ProcessingObjectEventArgs.Info.Handled Ignored.
    ProcessingObjectEventArgs.Info.SourceType ProcessingSourceType.ImageFrame
    ProcessingObjectEventArgs.Info.FrameCount The number of frames in the Flash movie.
    ProcessingObjectEventArgs.Info.FrameNumber The number of frames elapsed from the start of the movie.
    ProcessingObjectEventArgs.Info.FrameRate The number of frames per second of the Flash movie.
    ProcessingObjectEventArgs.Info.X The x-coordinate (in twips) of the frame bounds of the Flash movie.
    ProcessingObjectEventArgs.Info.Y The y-coordinate (in twips) of the frame bounds of the Flash movie.
    ProcessingObjectEventArgs.Info.Width The width (in twips) of the stage of the Flash movie.
    ProcessingObjectEventArgs.Info.Height The height (in twips) of the stage of the Flash movie.
    ProcessingObjectEventArgs.Info.BackgroundColor The background color of the frame.
  3. While rendering a frame, a ProcessingObject event of ProcessingSourceType.Image is generated when an image is imported.
    Name Description
    ProcessingObjectEventArgs.Cancel Ignored.
    ProcessingObjectEventArgs.Info.Handled Ignored.
    ProcessingObjectEventArgs.Info.SourceType ProcessingSourceType.Image
    ProcessingObjectEventArgs.Info.FrameCount The number of frames in the Flash movie.
    ProcessingObjectEventArgs.Info.FrameNumber The number of frames elapsed from the start of the movie.
    ProcessingObjectEventArgs.Info.FrameRate The number of frames per second of the Flash movie.
    ProcessingObjectEventArgs.Info.Width The pixel width of the image.
    ProcessingObjectEventArgs.Info.Height The pixel height of the image.
  4. While rendering a frame, a ProcessingObject event of ProcessingSourceType.ImageMask is generated when an image used as a mask is imported.
    Name Description
    ProcessingObjectEventArgs.Cancel Ignored.
    ProcessingObjectEventArgs.Info.Handled Ignored.
    ProcessingObjectEventArgs.Info.SourceType ProcessingSourceType.ImageMask
    ProcessingObjectEventArgs.Info.FrameCount The number of frames in the Flash movie.
    ProcessingObjectEventArgs.Info.FrameNumber The number of frames elapsed from the start of the movie.
    ProcessingObjectEventArgs.Info.FrameRate The number of frames per second of the Flash movie.
    ProcessingObjectEventArgs.Info.Width The pixel width of the image.
    ProcessingObjectEventArgs.Info.Height The pixel height of the image.

Unless stated otherwise, a ProcessedObject event corresponding to each ProcessingObject event is generated.

ProcessingObjectEventArgs.Info.SourceType ProcessedObjectEventArgs.Object when successful
ProcessingSourceType.MultiFrameImage GraphicLayer
ProcessingSourceType.ImageFrame null
ProcessingSourceType.Image PixMap
ProcessingSourceType.ImageMask PixMap

Imported images are not compressed. You may wish to analyse and compress these PixMap objects by pre and post processing them during the ProcessingObject and ProcessedObject events.

 

   

Example
 

See the SwfParameters.FlashVars property or the ProcessingInfo.FrameNumber property for simple examples.

 

Here we import 24 frames from a Flash movie alternately into two PDF documents. The frames are 0.2 seconds apart. Doc.Rect is set so that the outputs are 6-up and preserve the aspect ratio. For the fifth to the eighth imported frames in each document, the background is suppressed and a transparent oval in the same color is drawn instead.

[C#]
Doc doc1 = new Doc();
Doc doc2 = new Doc();
using(SwfImportOperation operation = new SwfImportOperation()) {
  const int fontSize = 20;
  int k = 0;
  bool failed = false;
  operation.ProcessingObject += delegate(object sender, ProcessingObjectEventArgs e) {
    switch(e.Info.SourceType) {
    case ProcessingSourceType.MultiFrameImage:
      if(failed || k>=24)
        e.Info.FrameNumber = null;
      else
        e.Info.FrameNumber = 1+Convert.ToInt64(0.2*k*e.Info.FrameRate.Value);
      e.Tag = ProcessingSourceType.MultiFrameImage;
      break;
    case ProcessingSourceType.ImageFrame: {
      SwfImportOperation op = (SwfImportOperation)sender;
      op.Doc = op.Doc==doc1? doc2: doc1;
      const int distance = 20;
      const int margin = 30;
      double width = op.Doc.MediaBox.Width-2*margin;
      double height = op.Doc.MediaBox.Height-2*margin;
      double scale = Math.Min((width-distance)/(2*e.Info.Width.Value),
        (height-2*distance-3*fontSize)/(3*e.Info.Height.Value));

      int p = k/2;
      double rectWidth = scale*e.Info.Width.Value;
      double rectHeight = scale*e.Info.Height.Value;
      op.Doc.Rect.SetRect(margin+(width+distance)*(p%2)/2,
        margin+height-rectHeight-(height+2*distance)*(p/2%3)/3,
        rectWidth, rectHeight);
      if(p%6==0)
        op.Doc.Page = op.Doc.AddPage();
      if(p>=4 && p<8 && e.Info.BackgroundColor!=null) {
        op.BackgroundRegion = null;
        op.Doc.Color.String = e.Info.BackgroundColor.String;
        op.Doc.Color.Alpha = 127;
        op.Doc.AddOval(true);
      }
    } break;
    }
  };
  operation.ProcessedObject += delegate(object sender, ProcessedObjectEventArgs e) {
    if(!e.Successful) {
      failed = true;
      return;
    }
    if(e.Tag is ProcessingSourceType
      && (ProcessingSourceType)e.Tag==ProcessingSourceType.MultiFrameImage)
    {
      SwfImportOperation op = (SwfImportOperation)sender;
      op.Doc.Color.Gray = 0;
      op.Doc.Color.Alpha = 255;
      op.Doc.FontSize = fontSize;
      op.Doc.TextStyle.HPos = 0.5;
      op.Doc.Rect.Top = op.Doc.Rect.Bottom;
      op.Doc.Rect.Bottom = op.Doc.MediaBox.Bottom;
      op.Doc.AddText(string.Format("{0} secs", 0.2*k));
      ++k;
    }
    PixMap pixmap = e.Object as PixMap;
    if(pixmap!=null)
      pixmap.Compress();
  };
  operation.Import(Server.MapPath("ABCpdf.swf"));
}
doc1.Save(Server.MapPath("swf1.pdf"));
doc1.Clear();
doc2.Save(Server.MapPath("swf2.pdf"));
doc2.Clear();

[Visual Basic]
Dim theMyOp As MyOp
Using theOperation As New SwfImportOperation
  theMyOp = New MyOp(theOperation)
  theOperation.Import(Server.MapPath("ABCpdf.swf"))
End Using
theMyOp.Save(Server.MapPath("swf1.pdf"), Server.MapPath("swf2.pdf"))
theMyOp.Clear()

Private NotInheritable Class MyOp
  Private Const theFontSize As Integer = 20
  Private theDoc1 As Doc
  Private theDoc2 As Doc
  Private k As Integer
  Private theFailed As Boolean

  Public Sub New(theOperation As SwfImportOperation)
    theDoc1 = New Doc()
    theDoc2 = New Doc()
    k = 0
    theFailed = False
    AddHandler theOperation.ProcessingObject, AddressOf Processing
    AddHandler theOperation.ProcessedObject, AddressOf Processed
  End Sub

  Public Sub Save(path1 As String, path2 As String)
    theDoc1.Save(Server.MapPath(path1))
    theDoc2.Save(Server.MapPath(path2))
  End Sub

  Public Sub Clear()
    theDoc1.Clear()
    theDoc2.Clear()
  End Sub

  Private Sub Processing(sender As Object, e As ProcessingObjectEventArgs)
    Select Case e.Info.SourceType
    Case ProcessingSourceType.MultiFrameImage
      If theFailed Or k >= 24 Then
        e.Info.FrameNumber = Nothing
      Else
        e.Info.FrameNumber = 1 + Convert.ToInt64(0.2 * k * e.Info.FrameRate.Value)
      End If
      e.Tag = ProcessingSourceType.MultiFrameImage
    Case ProcessingSourceType.ImageFrame
      Dim op As SwfImportOperation = CType(sender, SwfImportOperation)
      If op.Doc Is theDoc1 Then
        op.Doc = theDoc2
      Else
        op.Doc = theDoc1
      End If
      Const distance As Integer = 20
      Const margin As Integer = 30
      Dim width As Double = op.Doc.MediaBox.Width - 2 * margin
      Dim height As Double = op.Doc.MediaBox.Height - 2 * margin
      Dim scale As Double = Math.Min((width - distance) / (2 * e.Info.Width.Value), _
        (height - 2 * distance - 3 * theFontSize) / (3 * e.Info.Height.Value))

      Dim p As Integer = k \ 2
      Dim rectWidth As Double = scale * e.Info.Width.Value
      Dim rectHeight As Double = scale * e.Info.Height.Value
      op.Doc.Rect.SetRect(margin + (width + distance) * (p Mod 2) / 2, _
        margin + height - rectHeight - (height + 2 * distance) * (p / 2 Mod 3) / 3, _
        rectWidth, rectHeight)
      If p Mod 6 = 0 Then
        op.Doc.Page = op.Doc.AddPage()
      End If
      If p >= 4 And p < 8 And e.Info.BackgroundColor IsNot Nothing Then
        op.BackgroundRegion = Nothing
        op.Doc.Color.String = e.Info.BackgroundColor.String
        op.Doc.Color.Alpha = 127
        op.Doc.AddOval(True)
      End If
    End Select
  End Sub

  Private Sub Processed(sender As Object, e As ProcessedObjectEventArgs)
    If Not e.Successful Then
      theFailed = True
      Return
    End If
    If TypeOf e.Tag Is ProcessingSourceType Then
       If CType(e.Tag, ProcessingSourceType) = ProcessingSourceType.MultiFrameImage Then
         Dim op As SwfImportOperation = CType(sender, SwfImportOperation)
         op.Doc.Color.Gray = 0
         op.Doc.Color.Alpha = 255
         op.Doc.FontSize = theFontSize
         op.Doc.TextStyle.HPos = 0.5
         op.Doc.Rect.Top = op.Doc.Rect.Bottom
         op.Doc.Rect.Bottom = op.Doc.MediaBox.Bottom
         op.Doc.AddText(String.Format("{0} secs", 0.2 * k))
         k += 1
       End If
    End If
    Dim thePixMap As PixMap = TryCast(e.Object, PixMap)
    If thePixMap IsNot Nothing Then
       thePixMap.Compress()
    End If
  End Sub
End Class

swf1.pdf
 

swf2.pdf