So you want to make sure that you always print in landscape mode even if the user doesn't choose landscape, but the PrintJob property "landscape" is read only, and there's no way to force the user to choose landscape. In addition, PrintJob has some calculation inconsistencies with rotated clips. There is a solution, and here it is.

Pass this function a movieclip and it will make sure it prints in landscape mode. It rotates the clip, handles all the measurements, and rotates it back when it's done printing. This all happens invisibly to the user.

function printImage(mc:MovieClip):Void
{
        var realW:Number = mc._width;
        var realH:Number = mc._height;
        var orgX:Number = mc._x;
        var orgY:Number = mc._y;
        var pj:PrintJob = new PrintJob();
        var pageCount:Number = 0;
        if (pj.start())
        {
                mc._x = 0;
                mc._y = 0;
                var cXscale:Number, cYscale:Number;
                if (pj.orientation.toLowerCase() != "landscape")
                {
                        mc._rotation = 90;
                        mc._x = mc._width;
                        cXscale = (pj.pageWidth / realH) * 100;
                        cYscale = (pj.pageHeight / realW) * 100;
                }
                else
                {
                        cXscale = (pj.pageWidth / realW) * 100;
                        cYscale = (pj.pageHeight / realH) * 100;
                }
                mc._xscale = mc._yscale = Math.min(cXscale, cYscale);
                if (pj.addPage(mc, {xMin:0, xMax:realW, yMin:0, yMax:realH})) pageCount++;
        }
        if (pageCount > 0) pj.send();
        mc._xscale = mc._yscale = 100;
        mc._rotation = 0;
        mc._x = orgX;
        mc._y = orgY;
        delete pj;
}
 

16 Responses to Force landscape printing with PrintJob

  1. Adam Smith says:

    Thank you for figuring this out, I found it very useful!

  2. Pete says:

    This solution you posted on flashcoders many months ago really helped last summer, so just wanted to say thanks

  3. Oskars says:

    Hi, tnx for this example, it really helped me too.

  4. [...] Printing in landscape from Flash Flash developer Steven Sacks has code available on his site that allows you to print a .swf file in landscape format.  If the printer is set to portrait, Sasks’ code changes the orientation of the movieclip and adjusts the width to take up the space available on the page.  http://www.stevensacks.net/2007/03/16/force-landscape-printing-with-printjob [...]

  5. This help me a lot, please keep up the good work.

  6. Hello, thank you very much!
    Very good code, you are a blessed person!

  7. Fenn says:

    Been looking for something like this for ages! Any chance of this in AS3?

  8. [...] print out well.  Relaying on the user to choose landscape won't work.  Steven Sacks had the original solution in AS2/AS1.  I needed something for AS3 and in Flex.  Darron Schall had some additional advice [...]

  9. shep says:

    i need to do this for an online form however i don't know where to put this in my flash file? sorry for being a dummy but can someone guide me? I have a print button that also emails the forms data when pressed, I would love to force this form to print landscape!
    thanks

  10. craftycs says:

    I have tried using this code but I think I am not passing this to a movie clip correctly. I am trying to print the base level rather than an mc.

    this.print_btn.onRelease = function() {
    _root.printImage(0);
    };

    It prints the level however it doesn't orient the movie?

  11. Sabarinathan says:

    Hi ! What about FlexPrintJob class ?
    I am printing the DataGrid values with the help of PrintDataGrid class.. I have number columns which doesnt print with Potrait mode.. Can u please give some idea for printing the DataGrid in Landscape?

    Please.. Thanks in Advance..

  12. Hans Fouche says:

    Hi,
    This code looks like the code that could work for me. However I need it in Action Script 1 for Flash MX 6.
    Please could you assist

    Really Appreciate it.

    Hans

  13. jayuu says:

    Desperate to get this to work – can someone PLEASE post a URL of an example of this working?
    Just a simple click this button to print this dynamic text field so it prints landscape? PLEASE!!!!
    Many thanks.

  14. Sunny says:

    actionscript 3 version:

    package com.flashblocks.utils {

    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.printing.PrintJob;

    public class Printing {

    public function Printing() {}

    public static function printLandscape(clip:Sprite):void
    {
    var realW:Number = clip.width;
    var realH:Number = clip.height;
    var orgX:Number = clip.x;
    var orgY:Number = clip.y;
    var pj:PrintJob = new PrintJob();
    var pageCount:Number = 0;

    if (!pj.start()) return

    clip.x = 0;
    clip.y = 0;
    var cscaleX:Number, cscaleY:Number;
    if (pj.orientation.toLowerCase() != "landscape")
    {
    clip.rotation = 90;
    clip.x = clip.width;
    cscaleX = (pj.pageWidth / realH);
    cscaleY = (pj.pageHeight / realW);
    }
    else
    {
    cscaleX = (pj.pageWidth / realW);
    cscaleY = (pj.pageHeight / realH);
    }
    clip.scaleX = clip.scaleY = Math.min(cscaleX, cscaleY);
    if (pj.addPage(clip, new Rectangle(0, 0, realW, realH))) pageCount++;

    if (pageCount > 0) pj.send();
    clip.scaleX = clip.scaleY = 1;
    clip.rotation = 0;
    clip.x = orgX;
    clip.y = orgY;
    pj=null;
    }
    }
    }
    package com.flashblocks.utils {

    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.printing.PrintJob;

    public class Printing {

    public function Printing() {}

    public static function printLandscape(clip:Sprite):void
    {
    var realW:Number = clip.width;
    var realH:Number = clip.height;
    var orgX:Number = clip.x;
    var orgY:Number = clip.y;
    var pj:PrintJob = new PrintJob();
    var pageCount:Number = 0;

    if (!pj.start()) return

    clip.x = 0;
    clip.y = 0;
    var cscaleX:Number, cscaleY:Number;
    if (pj.orientation.toLowerCase() != "landscape")
    {
    clip.rotation = 90;
    clip.x = clip.width;
    cscaleX = (pj.pageWidth / realH);
    cscaleY = (pj.pageHeight / realW);
    }
    else
    {
    cscaleX = (pj.pageWidth / realW);
    cscaleY = (pj.pageHeight / realH);
    }
    clip.scaleX = clip.scaleY = Math.min(cscaleX, cscaleY);
    if (pj.addPage(clip, new Rectangle(0, 0, realW, realH))) pageCount++;

    if (pageCount > 0) pj.send();
    clip.scaleX = clip.scaleY = 1;
    clip.rotation = 0;
    clip.x = orgX;
    clip.y = orgY;
    pj=null;
    }
    }
    }

  15. from5to7 says:

    This code works the first time you print, but the second time, if you change the orientation, you'll get a cutoff page.

  16. Marcos Cardoso says:

    Thanks man! It helps me a lot

Leave a Reply

Set your Twitter account name in your settings to use the TwitterBar Section.