This is a discussion on Using PrintJob class of Actionscript 3.0 within the Tutorials forums, part of the Flash English category; Sometimes happen to have to print the contents of our SWF in paper or PDF .
The right Actionscript 3.0 ...
Inside it, on Stage, I have a MovieClip with instance name "content_mc" which contains an image and a dynamic text field with a description of that image.
Then, again on Stage, I have a button with instance name "print_btn" that will launch the event to print.
Now I create the Document Class, an AS file named "Main.as", implemented in this way:
Code:
package
{
import flash.display.*;
import flash.events.*;
import flash.printing.*;
public class Main extends MovieClip
{
public var print_job:PrintJob;
private var options:PrintJobOptions;
private var my_sprite:Sprite;
private var amount:int;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
initButton();
}
private function initButton():void
{
print_btn.label="PRINT";
print_btn.addEventListener(MouseEvent.MOUSE_DOWN,printImage);
}
private function printImage(evt:MouseEvent):void
{
amount=content_mc.numChildren;
my_sprite=new Sprite();
addChild(my_sprite);
print_job=new PrintJob();
options=new PrintJobOptions();
options.printAsBitmap=false;
if(print_job.start())
{
try
{
addContentToSprite();
}
catch (error:Error)
{
trace(error);
}
print_job.send();
removeContentAndSprite();
}
else
{
trace("Print deleted");
removeChild(my_sprite);
}
}
private function addContentToSprite():void
{
for (var i:int=0;i < amount;i++)
{
my_sprite.addChild(content_mc.getChildAt(0));
}
print_job.addPage(my_sprite,null,options);
}
private function removeContentAndSprite():void
{
for (var i:int=0;i < amount;i++)
{
content_mc.addChild(my_sprite.getChildAt(0));
}
removeChild(my_sprite);
}
}
}
Here is the result, try it:
Analizing the code:
Properties
a PrintJob class instance
public var print_job:PrintJob;
a PrintJobOptions class instance
private var options:PrintJobOptions;
one Sprite ( not to drink )
private var my_sprite:Sprite;
one numeric variable that will count the pages to print
private var amount:int;
Methods
printImage()
This method creates a new instance of class PrintJob, a new Sprite, printing options and finally it prints.
IMPORTANT: because the press is made correctly, you need to temporarily remove the contents of MovieClip you want to print and insert it into the new Sprite residing in Stage then restore the content in content_mc.
Check number of childrens that content_mc contains
amount=content_mc.numChildren;
Create the new Sprite and add it to stage
my_sprite=new Sprite();
addChild(my_sprite);
Create a new PrintJob instance and few options needed
print_job=new PrintJob();
options=new PrintJobOptions();
options.printAsBitmap=false;
I tell Flash to print but before that I call the method addContentToSprite, which switches the content of content_mc with new Sprite and after that I call the method removeContentAndSprite, which restore the content of content_mc and removes the sprite.
if(print_job.start())
{
try
{
addContentToSprite();
}
catch (error:Error)
{
trace(error);
}
print_job.send();
removeContentAndSprite();
}
else
{
trace("Print deleted");
removeChild(my_sprite);
}
addContentToSprite()
This method removes the content of content_mc and moves it into the new Sprite
for (var i:int=0;i < amount;i++)
{
my_sprite.addChild(content_mc.getChildAt(0));
}
print_job.addPage(my_sprite,null,options);
removeContentAndSprite()
This method restores the content of content_mc and removes the Sprite