Hi Rich,

I did a quick test with Flex - see my code below for Flex version, just change the image source to try it out...

The problem is that I am using an Image component - this is where it differs from Flash because I tried dynamically loading an image with Loader in Flash and that was okay too. Anyway, in Flex it appears I have to call UIComponent.validateNow() to make sure any changes to the properties that affect drawing / layout are done immediately - Grrrr, so much for simplicity and Flash = Flex as far as AS3 code is concerned.

So, your code works fine, I guess mine was probably nearly okay and that's another flippin' gotcha.

   Thanks

   Glen

   <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute">
<mx:Script>
       <![CDATA[
var twoPages:Boolean;

           function printPages():void {
               var pj:PrintJob = new PrintJob();
               var pagesToPrint:int = 0;
               var options:PrintJobOptions = new PrintJobOptions();
               options.printAsBitmap = true;
var origWidth:int = printedImg.width;
               var origHeight:int = printedImg.height;
               var origRotation:Number = printedImg.rotation;
var rect:Rectangle = new Rectangle(0, 0, origWidth, origHeight); var pScaleX:Number;
               var pScaleY:Number;
if(pj.start()) {
                   //add state to restore
//what's the orientation?
                   if(PrintJobOrientation.LANDSCAPE == pj.orientation) {
                       printedImg.rotation = -90;
                   }
//new logic accounts for twoPages and orientation when scaling
                   if (twoPages) {
if(PrintJobOrientation.LANDSCAPE == pj.orientation) {
                           pScaleX = pj.pageWidth / origWidth;
                           pScaleY = pj.pageHeight / (origHeight/2);
                          } else {
                           pScaleX = pj.pageWidth / (origWidth/2);
                           pScaleY = pj.pageHeight / origHeight;
                       }
                   } else {
                       pScaleX = pj.pageWidth / origWidth;
                       pScaleY = pj.pageHeight / origHeight;
                   }
//choose scale amount based on rotation //this overrides stage-based scale transformation, if any
                   if(pScaleX < pScaleY) {
                       printedImg.scaleX = pScaleX;
                       printedImg.scaleY = pScaleX;
                   } else {
                       printedImg.scaleX = pScaleY;
                       printedImg.scaleY = pScaleY;
                   }
                   printedImg.validateNow();
                   try {
                       if(twoPages) {
                           rect.width = origWidth/2
                           pj.addPage(printedImg, rect, options);
                           pagesToPrint++;
rect.x = rect.width = origWidth/2
                           pj.addPage(printedImg, rect, options);
                           pagesToPrint++;
                       } else {
                           pj.addPage(printedImg, rect, options);
                       }
                   }
                   catch (err:Error) {
                       trace("printJob error " + err.message);
                   }
if(pagesToPrint > 0) { pj.send(); } //bizzarly, landscape mode distorts the mc width!
                   //I am logging a bug
//try this both portrait and landscape with no other changes.
                   trace("origWidth:", origWidth);
trace(printedImg.width, printedImg.height, printedImg.rotation); printedImg.width = origWidth;
                   printedImg.height = origHeight;
                   printedImg.rotation = origRotation;
trace("rescale");
                   trace("origWidth:", origWidth);
trace(printedImg.width, printedImg.height, printedImg.rotation);
               } else {
                   trace("print job cancelled");
//resets here don't improve bug of cancelling after starting job
               }
           }
//printBtn.addEventListener(MouseEvent.CLICK, onPrint, false, 0, true);
           function onPrint(evt:Event):void {
               if (twoP.selected) {
                   trace("true");
                   twoPages = true;
               } else {
                   twoPages = false;
               }
               printPages();
           }
       ]]>
   </mx:Script>
   <mx:Canvas id="printedImg">
       <mx:Image source="images/large/1950/01 January 1950-001.jpg"/>
   </mx:Canvas>
   <mx:HBox>
       <mx:Button label="Print" click="onPrint(event)"/>
       <mx:CheckBox id="twoP" label="Two Pages"/>
   </mx:HBox>
</mx:Application>

Rich Shupe wrote:
Glen,

On 4/21/08 10:11 AM, "Glen Pike" <[EMAIL PROTECTED]> wrote:

    Again, quick experiment.

    If I do all my scaling of the image when it is loaded, rather than inside
the started printjob, it works, so I think the problem is with Flex / Flash
Player not updating my sprites / images on stage in time for them to be
printed.

No, I now think even more that it's the post-calculation rect. See my other
email and let me know if I'm right.

I did notice a bug, however, which may be in the Flash Player. Or, I'm too
rushed to notice how to fix it. Only when the orientation is changed to
landscape does the sprite scale and not restore. See my comments. I'm
looking for info and will log a bug.

    Incidentally FlexPrintJob will do all the scaling for you, but this
results in it printing more pages than I want for a 2 page spread (6), so I
still have to scale my image, which makes the FlexPrintJob class redundant.

Good. I feel better. Heh.

Rich
http://www.LearningActionScript3.com





--

Glen Pike
01326 218440
www.glenpike.co.uk <http://www.glenpike.co.uk>

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to