As3 printing problem, blanks swf after print or ca

2019-01-29 05:51发布

问题:

ok back at another issues in as3 printing

//Function to print entire screen
function printFunction(event:MouseEvent):void
{
    var myPrintJob:PrintJob = new PrintJob();
    var oldScaleX:Number = root.scaleX;
    var oldScaleY:Number = root.scaleY;

    //Start the print job
    myPrintJob.start();

    //Figure out the new scale
    var newScaleX:Number = myPrintJob.paperWidth/root.width;
    var newScaleY:Number = myPrintJob.paperHeight/root.height;

    //Shrink in both the X and Y directions by the same amount (keep the same ratio)
    if(newScaleX < newScaleY)
        newScaleY = newScaleX;
    else
        newScaleX = newScaleY;

    root.scaleX = newScaleX;
    root.scaleY = newScaleY;

    //Print the page
    myPrintJob.addPage(Sprite(root));
    myPrintJob.send();

    //Reset the scale to the old values
    root.scaleX = oldScaleX;
    root.scaleY = oldScaleY;
}

I cant seem to find anything thats really helpful with this. When i click cancel on the print dialog box, i get error below and it blanks out my swf.

The error consists, that whenever i try to print and cancel it, or even when i do succesfully print, swf goes blank.

回答1:

There are two printing types, vector and bitmap. Because you are just passing in the root it will try to print everything as a vector. But what you might be seeing is that in some versions of the Flash player on some operating systems vector printing doesn't work. I normally create a bitmap snapshot of the displayobject that you want and print this.

var bitmapData:BitmapData = new BitmapData(root.width, root.height); bitmapData.draw(root); var printThis:Bitmap = new Bitmap(bitmapData);

Make sure you add it to the stage before you print so that preview works and mind the max bitmap data size. When you are finished delete the bitmap.



回答2:

To print as bitmap you can use print options as,

var printOption:PrintJobOptions = new PrintJobOptions();
printOption.printAsBitmap = true;

before addPage(...); line

But this is not the cause for blank SWF.

use following code,

function firePrint(e:MouseEvent):void
{
printImage(Img);
}
printBtn.addEventListener(MouseEvent.CLICK, firePrint, false, 0, true);

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;

var printOption:PrintJobOptions = new PrintJobOptions();
printOption.printAsBitmap = true;

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.scaleX = mc.scaleY = Math.min(cXscale,cYscale);

    if (pj.addPage(mc,new Rectangle(0,0,realW,realH)))
    {
        pageCount++;
    }

    if (pageCount > 0)
    {
        pj.send();
    }

    mc.scaleX = mc.scaleY = 100;
    mc.rotation = 0;
    mc.x = orgX;
    mc.y = orgY;
    pj = null;
}else
{
    //No printer or prnting is cancelled
}
}