Flex printing using FlexPrintJob

วันนี้ว่างๆ ก็เลยนั่งลองหาดูว่า Flex มันติดต่อเครื่องพิมพ์ได้อย่างไร ก็เลยไปเจอเข้าว่ามันใช้ FlexPrintJob ในการทำงาน คล้ายๆกับ Java เลยที่ใช้ PrintJop แล้วก็ Draw Graphic ลงไปเอา

ส่วน FlexPrintJob ก็คล้ายๆกัน แต่เอา Component ยัดลงไปได้เลย

  1. Create an instance of the FlexPrintJob class
  2. var printJob:FlexPrintJob = new FlexPrintJob();
  3. Start the print job:
  4. printJob.start();
  5. Add one or more objects to the print job and specify how to scale them:
  6. printJob.addObject(myObject, FlexPrintJobScaleType.MATCH_WIDTH);
  7. Send the print job to the printer:
  8. printJob.send();
  9. Free up any unneeded objects.


ตัวอย่างคับ

<?xml version="1.0"?>
<!-- printing\DGPrint.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 
    <mx:Script>
        <![CDATA[
            import mx.printing.*;
 
            // Create a PrintJob instance.
            private function doPrint():void {
                // Create an instance of the FlexPrintJob class.
                var printJob:FlexPrintJob = new FlexPrintJob();
 
                // Start the print job.
                if (printJob.start() != true) return;
 
                // Add the object to print. Do not scale it.
                printJob.addObject(myDataGrid, FlexPrintJobScaleType.NONE);
 
                // Send the job to the printer.
                printJob.send();
            }
        ]]>
    </mx:Script>
 
    <mx:VBox id="myVBox">
        <mx:DataGrid id="myDataGrid" width="300">
            <mx:dataProvider>
                <mx:Object Product="Flash" Code="1000"/>
                <mx:Object Product="Flex" Code="2000"/>
                <mx:Object Product="ColdFusion" Code="3000"/>
                <mx:Object Product="JRun" Code="4000"/>
            </mx:dataProvider>
        </mx:DataGrid>
        <mx:Button id="myButton" 
            label="Print" 
            click="doPrint();"/>
    </mx:VBox>
</mx:Application>

source http://livedocs.adobe.com