2009-09-14 12 views
5

Ich versuche derzeit, eine Funktion zu erstellen, mit der ich in MovieClip übergeben und drucken kann.AS3 mit PrintJob zum Drucken eines MovieClip

Hier ist die vereinfachte Version der Funktion:

function printMovieClip(clip:MovieClip) { 

var printJob:PrintJob = new PrintJob(); 
var numPages:int = 0; 
var printY:int = 0; 
var printHeight:Number; 

if (printJob.start()) { 

/* Resize movie clip to fit within page width */ 
if (clip.width > printJob.pageWidth) { 
    clip.width = printJob.pageWidth; 
    clip.scaleY = clip.scaleX; 
} 

numPages = Math.ceil(clip.height/printJob.pageHeight); 

/* Add pages to print job */ 
for (var i:int = 0; i < numPages; i++) { 
printJob.addPage(clip, new Rectangle(0, printY, printJob.pageWidth, printJob.pageHeight)); 
printY += printJob.pageHeight; 
} 

/* Send print job to printer */ 
printJob.send(); 

/* Delete job from memory */ 
printJob = null; 

} 

} 

printMovieClip(testMC); 

Leider ist dies nicht wie erwartet das heißt Arbeitsdruck über die gesamte Breite der MovieClip und auf der Länge von Seitenumbrüchen zu tun.

Antwort

5

Ich habe vergessen, den Druckbereich zu skalieren, um die Größe des Movieclips anzupassen. Siehe unten für Arbeitslösung:

function printMovieClip(clip:MovieClip) { 

    var printJob:PrintJob = new PrintJob(); 
    var numPages:int = 0; 
    var printArea:Rectangle; 
    var printHeight:Number; 
    var printY:int = 0; 

    if (printJob.start()) { 

     /* Resize movie clip to fit within page width */ 
     if (clip.width > printJob.pageWidth) { 
      clip.width = printJob.pageWidth; 
      clip.scaleY = clip.scaleX; 
     } 

     /* Store reference to print area in a new variable! Will save on scaling calculations later... */ 
     printArea = new Rectangle(0, 0, printJob.pageWidth/clip.scaleX, printJob.pageHeight/clip.scaleY); 

     numPages = Math.ceil(clip.height/printJob.pageHeight); 

     /* Add pages to print job */ 
     for (var i:int = 0; i < numPages; i++) { 
      printJob.addPage(clip, printArea); 
      printArea.y += printArea.height; 
     } 

     /* Send print job to printer */ 
     printJob.send(); 

     /* Delete job from memory */ 
     printJob = null; 

    } 

} 

printMovieClip(testMC); 
+0

nützlich sein, wenn das funktioniert (I habe es noch nicht versucht ...), dann danke, denn es ist ein sehr allgemeines und nützliches Beispiel, wie man einen großen MovieClip druckt, um mehrere Seiten in Flash AS3 zu überspannen. – Triynko

2

Vielen Dank für Ihren Open-Source-Geist! Aufgrund Ihrer großartigen Arbeit implementiere ich es und mache ein paar Verbesserungen, um mein praktisches MovieClip-Druckproblem zu lösen. Der Hauptfortschritt, den ich gemacht habe, besteht darin, einen Weg zu finden, einen MovieClip mit mehreren Frames nur durch einen einzigen Druckauftrag zu drucken. Natürlich habe ich die Frage "Drucken der vollen Breite des Movieclip" gelöst. Da SWF-Inhalte in Form von Vektorgrafiken gespeichert werden, müssen Sie sicherstellen, dass clip.height = printArea.height; clip.width = printArea.width;. Es ist eine einfache Möglichkeit:

1//MC printing Function 
2private function printMovieClip(clip:MovieClip):void 
3{ 
4 var printJob:PrintJob=new PrintJob(); 
5 var printArea:Rectangle; 
6 if (!printJob.start()) 
7  return; 
8 //The page you choose to print ,"selectPages" is a mx:combox object i used to support printing one frame of MC 
9 var printPage:int=selectPages.selectedItem.data; 
10 if (printPage == 0) //print all frames of the MovieClip 
11  { 
12  for (var i:int=1; i <= clip.totalFrames; i++) 
13  { 
14   clip.gotoAndStop(i); 
15   /* Resize movie clip to fit within page width */ 
16   clip.width=printJob.pageWidth; 
17   clip.scaleY=clip.scaleX; 
18   /* Store reference to print area in a new variable! Will save on scaling */ 
19   printArea=new Rectangle(0, 0, printJob.pageWidth, printJob.pageHeight); 
20   //numPages=Math.ceil(clip.height/printJob.pageHeight); 
21     /* Add pages to print job */ 
22   printJob.addPage(clip, printArea); 
23  } 
24  } 
25 else //print the selected frame 
26 { 
     //goto the selected frame firstly 
27  clip.gotoAndStop(printPage); 
28  /* Resize movie clip to fit within page width */ 
29  clip.width=printJob.pageWidth; 
30  clip.scaleY=clip.scaleX; 
31  printArea=new Rectangle(0, 0, printJob.pageWidth, printJob.pageHeight); 
32   /* Add pages to print job */ 
33  printJob.addPage(clip, printArea); 
34  } 
35 
36  /* Send print job to printer */ 
37  printJob.send(); 
38   /* Delete job from memory */ 
39  printJob=null; 
40 
41 } 

Wenn Sie weitere Informationen wünschen, können Sie einen Blick auf meinen Clip Bild nehmen kann (und da Sie ein wenig Chinesisch verstehen): alles in my blog ist. Es gibt auch MovieClip thumbnails (noch chinesisch).

+0

Ich habe nicht einmal mehrere Frames betrachtet, gute Arbeit :) obwohl als Vorschlag, was ist mit dem Entfernen der Verweis auf die mx: Combobox von der Funktion und die "printPage" Variable ein optionales Argument machen? Würde es etwas flexibler machen. –

0

Ich habe eine kleine Korrektur hinzugefügt, die die MovieClip-Abmessungen nach dem Druckauftrag zurücksetzt. Das Problem war, dass der Code den Filmclip auf der Bühne skalieren würde, wenn Sie etwas drucken, das größer ist als Ihre Seite. So wird behoben i, dass ... nichts Besonderes, aber vielleicht für andere Leute :)

dieser Code behebt auch die Tatsache, dass Ihre transparente PNG wird auch transparent sein, auf dem Druck

protected function printMovieClip(clip:MovieClip):void { 

      var printJob:PrintJob = new PrintJob(); 
      var printJobOptions:PrintJobOptions = new PrintJobOptions(); 
      var numPages:int = 0; 
      var printArea:Rectangle; 
      var printHeight:Number; 
      var printY:int = 0; 
      var originalWidth:Number; 
      var originalHeight:Number; 

      if (printJob.start()) { 

       originalWidth = clip.width; 
       originalHeight = clip.height; 

       if (clip.width > printJob.pageWidth) { 
        clip.width = printJob.pageWidth; 
        clip.scaleY = clip.scaleX; 
       } 

       printArea = new Rectangle(0, 0, printJob.pageWidth/clip.scaleX, printJob.pageHeight/clip.scaleY); 

       numPages = Math.ceil(clip.height/printJob.pageHeight); 

       for (var i:int = 0; i < numPages; i++) 
       { 
        printJobOptions.printAsBitmap = true; 
        printJob.addPage(clip, printArea, printJobOptions); 
        printArea.y += printArea.height; 
       } 

       /* Send print job to printer */ 
       printJob.send(); 

       /* Delete job from memory */ 
       printJob = null; 

       /* reset the clips width and height on stage so it is back at its original size*/ 
       clip.width = originalWidth; 
       clip.height = originalHeight; 
      } 

     } 
+0

Ich muss das versuchen, klingt ace. Vielen Dank! –

Verwandte Themen