Force landscape printing with PrintJob
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;
}
Posted in Actionscript, Flash, Tips/Tricks
April 11th, 2007 at 4:40 pm
Thank you for figuring this out, I found it very useful!
June 13th, 2007 at 4:53 am
This solution you posted on flashcoders many months ago really helped last summer, so just wanted to say thanks
October 25th, 2007 at 11:55 pm
Hi, tnx for this example, it really helped me too.
December 19th, 2007 at 1:12 pm
[...] 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 [...]
February 27th, 2008 at 12:47 pm
This help me a lot, please keep up the good work.
July 7th, 2008 at 1:04 pm
Hello, thank you very much!
Very good code, you are a blessed person!
August 4th, 2008 at 4:17 am
Been looking for something like this for ages! Any chance of this in AS3?
September 18th, 2008 at 9:09 pm
[...] 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 [...]
October 23rd, 2008 at 10:06 am
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
February 25th, 2009 at 3:46 am
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?
March 18th, 2009 at 4:37 am
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..
September 10th, 2009 at 10:15 am
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
October 27th, 2009 at 2:09 am
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.