Posted
Filed under Action Script

[작성자] http://visualp.com 고병만
actionscript 3.0에서 프린트를 하기 위해서는

flash.printing.PrintJob class를 import 해야 한다.
프린트 버튼을 누루는 시점이 하나의 job 즉 작업 단위로 봐야 한다.

PrintJob 클레스는 addPage 메소드를 통해서 Sprite 단위의 인쇄 페이지를 추가 할 수 있다.
참고로 A4 Size를 픽셀 단위로 환산 하며, 595 * 842 가 된다.

Sprite객체를직접 생성 해서 프린트를 할 수도 있고, 직접 무비클립을 만들어서 인쇄 작업을 진행 할 수 있다. 무비클립에 인쇄할 페이지를 디자인 한 후 출력 할 수도 있다는 말이다.

한번에 여러장을 인쇄 하기 위해서는 addPage메소를 이용하여 Sprite 객체를 추가 해주면 된다.
다음 예제는 SharedObject에 저장된 text를 불러와서 배경이 디자인된 skin1이라는 무비클립을
인쇄하는 간단한 예제이다.

반드스 printJob.start()메소드를 호출 한후 try catch 문으로 묶어 줘야 한다.
그렇지 않으면 오류가 발생 한다.


stop();
import flash.external.ExternalInterface;
import flash.net.SharedObject;
import flash.printing.PrintJob;

saveBtn.buttonMode=true;
printBtn.buttonMode=true;

//print object 변수 선언

//shared obj 구현
var so:SharedObject = SharedObject.getLocal("sample_lesson01_9001","/");

if(so.data.text01 != null){
 text0.text=String(so.data.text01);
}

text0.addEventListener(MouseEvent.CLICK,text_clear);
saveBtn.addEventListener(MouseEvent.CLICK,save_text0);
printBtn.addEventListener(MouseEvent.CLICK,print_text0);

function print_text0(event:MouseEvent):void{
   //[프린터 작업 생성]
  
   var skin1:print_skin1 = new print_skin1();
   var printJob:PrintJob = new PrintJob();
   skin1.content_text.text = text0.text; 
   printJob.start();
  try{
    printJob.addPage(skin1);
    printJob.send();
   }catch(error:Error){
   
   }
}

function save_text0(event:MouseEvent):void{
 if(text0.text==""){
  ExternalInterface.call("내용이 존재 하지 않습니다.");
  this.stage.focus=text0;
 }else{
  so.data.text01 = text0.text;
  so.flush();
  ExternalInterface.call("저장 완료!");
 }
}

function text_clear(evetn:MouseEvent):void{
 trace("click");
 if(so.data.text01==null){
  text0.text="";
 }
}

/*
so.flush();
so.close();
*/

2010/04/28 20:53 2010/04/28 20:53