2 Replies Latest reply on Jun 18, 2010 11:55 PM by BhaskerChari

    FlexPrintJob/PrintJob doesn't print things that aren't directly visible on stage.

    en.cry.pt

      Hi,

       

      I had never used PrintJob before, but when I tried to use it I realized that it doesn't print things not directly visible on stage at printing time.

       

      For instance, if you have a Scroller containing a larger component it will only print the small section of the component that is directly visible.

       

      And if you print any Sprite not added to stage it will print blank pages.

      I need urgently to get around this, does anyone know how to dynamically print Sprites that haven't been added to stage ?

       

      Thanx in advance.

        • 1. Re: FlexPrintJob/PrintJob doesn't print things that aren't directly visible on stage.
          Flex harUI Adobe Employee

          I think you can put the Sprite in another Sprite that is on stage with

          visible=false and print the inner Sprite.

          • 2. Re: FlexPrintJob/PrintJob doesn't print things that aren't directly visible on stage.
            BhaskerChari Level 4

            Hi en.cry.pt,

            For printing components that you dont want to be visible on the screen you can do it by first adding the component to the stage by using addChild method and after you add the component to the printObject you can then remove it so that it is not visible on the stage. Please refer to the example below in the doPrint() function.

            <?xml version="1.0"?>
            <!-- printing\ADGPrint.mxml -->
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

             

                <mx:Script>
                    <![CDATA[
                        import mx.printing.*;
                        import mx.collections.ArrayCollection;
                        import mx.printing.PrintAdvancedDataGrid;
                               
                        // Create a PrintJob instance.
                        private function doPrint():void {
                            // Create an instance of the FlexPrintJob class.
                            var printJob:FlexPrintJob = new FlexPrintJob();
                           
                            // Initialize the PrintAdvancedDataGrid control.
                            var printADG:PrintAdvancedDataGrid =
                                new PrintAdvancedDataGrid();
                            // Exclude the PrintAdvancedDataGrid control from layout.
                            printADG.includeInLayout = false;
                            printADG.source = adg;

             

                            // Add the print-specific control to the application.               
                            addChild(printADG);
                           
                            // Start the print job.
                            if (printJob.start() == false) {               
                                // User cancelled print job.
                                // Remove the print-specific control to free memory.               
                                removeChild(printADG);
                                return;
                            }

             

                            // Add the object to print. Do not scale it.
                            printJob.addObject(printADG, FlexPrintJobScaleType.NONE);

             

                            // Send the job to the printer.
                            printJob.send();

             

                            // Remove the print-specific control to free memory.               
                            removeChild(printADG);
                        }
                       
                        [Bindable]
                        private var dpHierarchy:ArrayCollection = new ArrayCollection([
                          {Region:"Southwest", children: [
                             {Region:"Arizona", children: [
                                {Territory_Rep:"Barbara Jennings", Actual:38865, Estimate:40000},
                                {Territory_Rep:"Dana Binn", Actual:29885, Estimate:30000}]}, 
                             {Region:"Central California", children: [
                                {Territory_Rep:"Joe Smith", Actual:29134, Estimate:30000}]}, 
                             {Region:"Nevada", children: [
                                {Territory_Rep:"Bethany Pittman", Actual:52888, Estimate:45000}]}, 
                             {Region:"Northern California", children: [
                                {Territory_Rep:"Lauren Ipsum", Actual:38805, Estimate:40000},
                                {Territory_Rep:"T.R. Smith", Actual:55498, Estimate:40000}]}, 
                             {Region:"Southern California", children: [
                                {Territory_Rep:"Alice Treu", Actual:44985, Estimate:45000},
                                {Territory_Rep:"Jane Grove", Actual:44913, Estimate:45000}]}
                          ]}
                        ]);
                       
                    ]]>
                </mx:Script>

             

                <mx:Panel title="PrintAdvancedDataGrid Control Example"
                    height="75%" width="75%" layout="horizontal"
                    paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">

             

                    <mx:AdvancedDataGrid id="adg"
                        width="100%" height="100%">
                        <mx:dataProvider>
                            <mx:HierarchicalData source="{dpHierarchy}"/>
                        </mx:dataProvider>
                        <mx:columns>
                            <mx:AdvancedDataGridColumn dataField="Region"/>
                            <mx:AdvancedDataGridColumn dataField="Territory_Rep"
                                headerText="Territory Rep"/>
                            <mx:AdvancedDataGridColumn dataField="Actual"/>
                            <mx:AdvancedDataGridColumn dataField="Estimate"/>
                        </mx:columns>
                    </mx:AdvancedDataGrid>  

             

                    <mx:Button id="myButton"
                        label="Print"
                        click="doPrint();"/>
                </mx:Panel>   
            </mx:Application>

             

             

            For printing components which have a large size with scrolling you can do the same as shown in example below...The logic here is first you need to set the size of the component to its full size with out scrolling before adding it to the FlexPrintJob and after you send it to the PrintJob you can restore it to the orizinal size with scroll bars...during this time you dont see any momentary change of size...its not at all noticed by user(The momentary resize of container).

             

            Check the below sample for printing scrollable components:

             

            private function doPrintBoxTreatment(event:Event):void
               {
                var printJob:FlexPrintJob = new FlexPrintJob();
                if(printJob.start() != true) return;

                try
                            {
                             // Maintaining the original height in a variable to restore the container size after printing
                 var mainContainerHeight:int = mainContainer.height;
                 //Increasing the height of the desired component to be printed without scrolling.
                 mainContainer.height = mainContainer.measuredMinHeight;
                 mainContainer.verticalScrollPolicy = "off";
                 disclaimer.height = disclaimer.height + 30;
                     
                 //Adding the resized Component to our FlexPrintjob and then sending the print request
                 printJob.addObject(this,FlexPrintJobScaleType.MATCH_WIDTH);    
                 printJob.send();
                
                 //Resizing the component back to original size with scrolls
                 mainContainer.height = mainContainerHeight;
                 mainContainer.verticalScrollPolicy = "auto";    
                }
                catch(error:Error)
                            {
                             trace("Error : " + error.message);
                             mainContainer.height = mainContainerHeight;
                 mainContainer.verticalScrollPolicy = "auto";                          
                            }
               }

             

            If this post answers your question or helps, please kindly mark it as such.


            Thanks,

            Bhasker Chari