How to dynamically set print page margins in Flash

2019-04-16 18:43发布

问题:

//I can get the right margins by defining a rectangle and giving it the following dimensions:

var rect1:Rectangle = new Rectangle(0, 0, 792,612); 

//When the print button is pressed the following code executes using the dimensions defined by rect1:

prntCover_btn.addEventListener(MouseEvent.CLICK, printCover);

function printCover(evt:MouseEvent):void {
    front_mc.visible = false;
        var myPrintJob:PrintJob = new PrintJob();
        var options:PrintJobOptions = new PrintJobOptions();
        options.printAsBitmap = true;
        front_mc.scaleX = 1;
        front_mc.scaleY = 1;
        myPrintJob.start();
        myPrintJob.addPage(front_mc, rect1, options);
        myPrintJob.send();

    } 

//U.S.paper is 792 = 11.5 inch wide paper. Would like to use A3 size so I did this after the line myPrintJob.start();

var margin_height:Number = (myPrintJob.paperHeight - myPrintJob.pageHeight)/2;
var margin_width:Number = (myPrintJob.paperWidth - myPrintJob.pageWidth)/2;

This is not working to place the mc correctly on the page. This is all the Adobe help provides. Also Googled and tried different variations but no success. Can anyone help?

Thanks in advance for any insight into this.

Annie

回答1:

You could clarify the question a bit because it is a bit unclear what you're trying to achieve... if I understood correctly, you probably want to print something in the middle of a larger paper.

You can get the paper size the user chose only after calling PrintJob.start() so you'll have to define the printArea parameter after that. As the printArea defines a rectangle relative to the DisplayObject being printed, in order to center the DisplayObject you'll have to make sure that the DisplayObject is in the center of the rectangle;

var myPrintJob:PrintJob = new PrintJob();
var options:PrintJobOptions = new PrintJobOptions();
options.printAsBitmap = true;
front_mc.scaleX = 1;
front_mc.scaleY = 1;
myPrintJob.start();

var marginWidth:Number = (myPrintJob.pageWidth - front_mc.width) / 2;
var marginHeight:Number = (myPrintJob.pageHeight- front_mc.height) / 2;
var rect:Rectangle = new Rectangle(-marginWidth, -marginHeight, myPrintJob.pageWidth, myPrintJob.pageHeight);

myPrintJob.addPage(front_mc, rect1, options);
myPrintJob.send();