I really thought this was going to be easy but I’ve been trying for two days and haven’t been able to figure this out. I need to print a MovieClip but I’d like to center it on the page. Has anyone done this in AS3?I’ve tried to define a rectangle object as the printArea and adding a page sized behind the content of the movieclip and nothing seems to work. With this code, I can get the MovieClip scaled to fit a page, and centered, but the width & height seem to clip off half of the MovieClip when its printed.
[AS]
var ts:BitmapData = takeSnapshot();
var _screengrab:Bitmap = new Bitmap(ts, “auto”, true);
//good, now we need make sure it’s printable…
var _screenshot:MovieClip = new MovieClip();
_screenshot.addChild(_screengrab);
_screenshot.x = -1*(_screenshot.width+1);
_screenshot.y = -1*(_screenshot.height+1);
addChild(_screenshot);
//New Print Job
var _pj:PrintJob = new PrintJob();
//Set Up Options
var _options:PrintJobOptions = new PrintJobOptions();
_options.printAsBitmap = true;
if (_pj.start())
{
var _scaleRatio:Number = 1.0;
var _scaleRatio2:Number = 1.0;
var _originalSW:Number = _screenshot.width;
var _originalSH:Number = _screenshot.height;
if(_pj.orientation.toLowerCase() == "landscape"){
_screenshot.rotation = 0;
//we need to size this bitmap data to make sure it all fits on the page.
if(_originalSW > _pj.pageWidth){
_scaleRatio = _pj.pageWidth/_originalSW;
_screenshot.scaleX = _scaleRatio;
_screenshot.scaleY = _scaleRatio;
}
if(_originalSH > _pj.pageHeight){
_scaleRatio2 = _pj.pageHeight/_originalSH;
if(_scaleRatio2 < _scaleRatio){
_screenshot.scaleX = _scaleRatio2;
_screenshot.scaleY = _scaleRatio2;
}
}
} else {
_screenshot.rotation = 90;
if(_originalSH > _pj.pageWidth){
_scaleRatio = _pj.pageWidth/_originalSH;
_screenshot.scaleX = _scaleRatio;
_screenshot.scaleY = _scaleRatio;
}
if(_originalSW > _pj.pageHeight){
_scaleRatio2 = _pj.pageHeight/_originalSW;
if(_scaleRatio2 < _scaleRatio){
_screenshot.scaleX = _scaleRatio2;
_screenshot.scaleY = _scaleRatio2;
}
}
}
try
{
trace("PRINT",_screenshot.x, _screenshot.y, _screenshot.width,_screenshot.height, _pj.orientation.toLowerCase(),_pj.pageWidth, _pj.pageHeight);
var sx:Number = - ((_pj.pageWidth-_screenshot.width)/2);
var sy:Number = - ((_pj.pageHeight-_screenshot.height)/2);
var bounds:Rectangle = new Rectangle(sx, sy, _pj.pageWidth, _pj.pageHeight);
trace("BOUNDS: ",bounds.x, bounds.y, bounds.width, bounds.height, bounds.topLeft, bounds.bottomRight);
_pj.addPage(_screenshot, bounds, _options);
}
catch (error:Error)
{
// Do nothing.
}
try
{
_pj.send();
}
catch (error:Error)
{
// do nothing...
}
}
else
{
//printjob cancelled by user
}[/AS]