在某些情况下,为消除在屏幕上与打印纸张上的显示差异,打印显示对象时您可能需要调整显示对象的大小(或其它属性)。在打印之前调整显示对象的属性(例如使用 scaleX和 scaleY 属性来调整)时,请注意,如果对象缩放的尺度超出了为打印区域定义的矩形,该对象将被裁剪。页面打印完毕后,您可能还需要重置属性。
以下代码对 txt 显示对象的尺寸进行缩放(但不缩放绿色背景框),结果将按照指定矩形的尺寸对文本字段进行裁剪。打印后,文本字段将返回到在屏幕上显示的原始大小。如果用户从操作系统的“打印”对话框取消打印作业,则 Flash Player 或 AIR 中的内容将更改以警告用户作业已取消。
package { import flash.printing.PrintJob; import flash.display.Sprite; import flash.text.TextField; import flash.display.Stage; import flash.geom.Rectangle; public class PrintScaleExample extends Sprite { private var bg:Sprite; private var txt:TextField; public function PrintScaleExample():void { init(); draw(); printPage(); } private function printPage():void { var pj:PrintJob = new PrintJob(); txt.scaleX = 3; txt.scaleY = 2; if (pj.start()) { trace(">> pj.orientation: " + pj.orientation); trace(">> pj.pageWidth: " + pj.pageWidth); trace(">> pj.pageHeight: " + pj.pageHeight); trace(">> pj.paperWidth: " + pj.paperWidth); trace(">> pj.paperHeight: " + pj.paperHeight); try { pj.addPage(this, new Rectangle(0, 0, 100, 100)); } catch (error:Error) { // Do nothing. } pj.send(); } else { txt.text = "Print job canceled"; } // Reset the txt scale properties. txt.scaleX = 1; txt.scaleY = 1; } private function init():void { bg = new Sprite(); bg.graphics.beginFill(0x00FF00); bg.graphics.drawRect(0, 0, 100, 200); bg.graphics.endFill(); txt = new TextField(); txt.border = true; txt.text = "Hello World"; } private function draw():void { addChild(bg); addChild(txt); txt.x = 50; txt.y = 50; } } }