Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Using PrintJob class of Actionscript 3.0

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


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 19-11-08, 06:12
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Using PrintJob class of Actionscript 3.0

Sometimes happen to have to print the contents of our SWF in paper or PDF.


The right Actionscript 3.0 class to do that is is the PrintJob!


It happened just yesterday to me with a client who I made a small application.

This application displays images with descriptions and the client has asked me to add an option, which allowed the user to print on paper or PDF ( if

he has Adobe Acrobat Pro ) image and description.


Because I felt it could serve others, I public a simple tutorial on how to use PrintJob class of Actionscript 3.0.


Unfortunately, this class has a bug ( https: / / bugs.adobe.com/jira/browse/FP-307 ) if used on MAC.

It works only locally, on the Web does not work.

Do your tests and keep track of your results!


Create a FLA and save it as name "main.fla".


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

for (var i:int=0;i < amount;i++)

{

content_mc.addChild(my_sprite.getChildAt(0));

}



removeChild(my_sprite);


Source files:
Attached Files
File Type: zip PrintJob.zip (838.9 KB, 54 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Tags
print, printjob

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Timer class of Actionscript 3.0 Flep Tutorials 5 20-12-08 17:44
Slicing an image with the Matrix Class of Actionscript 3.0 Flep Tutorials 7 17-11-08 01:48
SWF to PNG with Actionscript 3.0 - ByteArray class Flep Tutorials 74 31-10-08 17:26
Actionscript calls Javascript - ExternalInterface class of Flash CS3 Flep Tutorials 1 21-10-08 16:30
CheckEmail Class - Actionscript 3.0 Flep Tutorials 9 19-12-07 08:03


All times are GMT. The time now is 12:37.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap