steven sacks
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;
}
16 Responses to Force landscape printing with PrintJob
Leave a Reply Cancel reply
plug.dj on twitter- Just a reminder that we are going to do some scheduled maintenance this evening at 8pm EDT. Downtime will be approximately 2-4 hours.
- Rihanna - Diamonds http://t.co/PUQ4FSWT6M
- I'm in DJ Lyka on #plugdj http://t.co/G5nW0qj6bp cos screw #XboxReveal
- Je suis dans la salle Daynight Channel -UK Garage&Deep sur #plugdj. ♫ En Lecture: Holt Blackheath - Don't Cry http://t.co/gPzMX6w3SS
- Je suis en DJ dans la salle NiggaStyle sur #plugdj. En Lecture: Daft Punk - Get Lucky (Official Audio) http://t.co/h9pxhZru21 @webster04
Recent Comments
Archives
- February 2013
- June 2011
- April 2011
- March 2011
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- September 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- April 2007
- March 2007
- February 2007
- August 2006




Thank you for figuring this out, I found it very useful!
This solution you posted on flashcoders many months ago really helped last summer, so just wanted to say thanks
Hi, tnx for this example, it really helped me too.
[...] 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 [...]
This help me a lot, please keep up the good work.
Hello, thank you very much!
Very good code, you are a blessed person!
Been looking for something like this for ages! Any chance of this in AS3?
[...] 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 [...]
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
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?
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..
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
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.
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;
}
}
}
This code works the first time you print, but the second time, if you change the orientation, you'll get a cutoff page.
Thanks man! It helps me a lot