10 Replies Latest reply on Jun 20, 2009 5:14 AM by abhayshow

    Printing in AS3 in CS4

    cterry06

      I am trying to have the user be able to click a button, which I called "printMe", so the instructor can see their results.

       

      Right now my code is:

      printMe.addEventListener(MouseEvent.CLICK, printHandler);
      function printHandler(event:MouseEvent):void
      {
         
      };

       

      I have googled and searched the forums here but every code I try doesn't seem to work.

       

      Does anyone know code that works for them to print with a button?

       

      Thanks!

        • 1. Re: Printing in AS3 in CS4
          kglad Adobe Community Professional & MVP

          at a minimum, you have to create a printjob instance, apply the start() method, apply the addPage() method and finally apply the send() method.

           

          check the help files for details.

          • 2. Re: Printing in AS3 in CS4
            cterry06 Level 1

            Does that mean I need to create a movie clip?

             

            I am confused as all the code I've seen seems to imply that. I just want to be able to have one frame print when the user clicks on a button.

             

            I attached my .swf file, maybe that can help you out.

            • 3. Re: Printing in AS3 in CS4
              kglad Adobe Community Professional & MVP

              you can print using a sprite or movieclip.  because you have a frame you want to print, you must already have a movieclip.

               

              if your code is attached to the main timeline and you want to print a frame on the main timeline, you can use "this" as the first parameter in the addPage() method.

              1 person found this helpful
              • 4. Re: Printing in AS3 in CS4
                cterry06 Level 1

                This is my code right now for my Button "printMe":

                 

                printMe.addEventListener(MouseEvent.CLICK, printHandler);
                function printHandler(event:MouseEvent):void
                {
                    var myPrintJob:PrintJob = new PrintJob();
                    var result:Boolean = myPrintJob.start();
                    if (result){
                        // successfully initalized print action
                        myPrintJob.addPage("results_mc", null, {printAsBitmap:true}, 18);
                        myPrintJob.send();
                        delete myPrintJob;
                    } else {
                        // user doesn't have pritner or use canceled print action
                    }
                };

                 

                when I hit control + enter I get these messages:

                1067Implicit coercion of a value of type %s to an unrelated type %s.

                1118Implicit coercion of a value with static type %s to a possibly unrelated type %s.
                1189Attempt to delete the fixed property %s.  Only dynamically defined properties can be deleted.

                 

                I also get:

                Warning: 3600.  The declared property myPrintJob cannot be deleted.  To free associated memory, set its value to null.

                 

                What's going on with my project??

                • 5. Re: Printing in AS3 in CS4
                  kglad Adobe Community Professional & MVP

                  the first parameter should be an instance reference, not a string.  so, if you have a movieclip with instance name results_mc on the same timeline as the timeline that contains that code, remove the quotes surrounding results_mc.

                  • 6. Re: Printing in AS3 in CS4
                    cterry06 Level 1

                    Okay I fixed it so now my code is this for the printMe button:

                     

                    printMe.addEventListener(MouseEvent.CLICK, printHandler);
                    function printHandler(event:MouseEvent):void
                    {
                        var myPrintJob:PrintJob = new PrintJob();
                        var result:Boolean = myPrintJob.start();
                        if (result){
                            // successfully initalized print action
                            myPrintJob.addPage(results_mc, null, {printAsBitmap:true}, 18);
                            myPrintJob.send();
                            delete myPrintJob;
                        } else {
                            // user doesn't have printer or use canceled print action
                        }
                    };

                     

                    However I'm still getting these errors:

                    1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.printing:PrintJobOptions.

                            myPrintJob.addPage(results_mc, null, {printAsBitmap:true}, 18);

                    1189: Attempt to delete the fixed property myPrintJob.  Only dynamically defined properties can be deleted.

                              delete myPrintJob;

                    Warning: 3600: The declared property myPrintJob cannot be deleted. To free associated memory, set its value to null.

                             delete myPrintJob;

                     

                    All of the code is in the same timeline.  I have a scripts layer, buttons layer, a layer just for the results_mc movie clip, text layer, images layer, and my background layer.  I have 42 frames but I only want to print frame 18 because it has the results page.

                     

                    Thanks again for helping me out!

                    • 7. Re: Printing in AS3 in CS4
                      RossRitchey Level 4

                      1. The {} around printAsBitmap:true state that printAsBitmap is a parameter on an Object.  That step in the function is requiring a PrintJobOptions, not an Object.

                       

                      To fix this part add the following lines of code before the addPage call:

                       

                      var printOption:PrintJobOptions = new PrintJobOptions();

                      printOption.printAsBitmap = true;

                       

                      And then

                      Replace: {printAsBitmap:true}

                      With: printOption

                       

                      2. You cannot delete the PrintJob object.  Instead set it to null:

                       

                      Replace: delete myPrintJob;

                      With: myPrintJob = null;

                       

                      So, the final code would look like:

                       

                      printMe.addEventListener(MouseEvent.CLICK, printHandler);
                      function printHandler(event:MouseEvent):void
                      {
                          var myPrintJob:PrintJob = new PrintJob();
                          var result:Boolean = myPrintJob.start();

                          var printOption:PrintJobOptions = new PrintJobOptions();

                          printOption.printAsBitmap = true;
                          if (result){
                              // successfully initalized print action

                              myPrintJob.addPage(results_mc, null, printOption, 18);
                              myPrintJob.send();
                             myPrintJob = null;
                          } else {
                              // user doesn't have printer or use canceled print action
                          }
                      };

                       

                       

                      Also, here is a great tutorial/article with a step-by-step on using the PrintJob class:

                      http://blog.tikikitchen.com/2008/04/18/printing-in-flash-in-1000000000-simple-steps

                      • 8. Re: Printing in AS3 in CS4
                        cterry06 Level 1

                        Thanks so much the code worked, but it just printed my blank movie clip, not what I wanted to print which is the other content.  Unless I need to put my content inside that movie clip?

                        • 9. Re: Printing in AS3 in CS4
                          RossRitchey Level 4

                          The PrintJob class will only print what is inside of the MovieClip you pass it, so, yes you need to put it inside that MovieClip.

                           

                          If you want to print the entire stage, I believe kglad said you can pass "this" as the clip:

                           

                          IE - addPage(this,......

                          • 10. Re: Printing in AS3 in CS4
                            abhayshow

                            Thanx bro. it helped me a lot...

                            many-many thanx to u....