8 Replies Latest reply on Jan 20, 2010 9:04 PM by Flex harUI

    Printing with PrintJob

    BUJOE

      Hi there!

       

      I have got a search dialog and I want to print the search criterias (labels) on a cover page and the search result (datagrid) following the cover page.

       

      Up to now, I used the FlexPrintJob class. Everything works well.

       

      Now I have to move to PrintJob because I need to force the pages containing the search result to be printed out in landscape, even if the user has selected to print out portrait within the operating-system-related print dialog.

       

      Further restriction: I can't embed fonts.

       

      But let's start with a simple question:

       

      How can I get this working? The page remains blank when I try this.

       

      var pj:PrintJob = new PrintJob();
      if (pj.start())
      {
           var testLabel:Label = new Label();
           testLabel.width = 400;
           testLabel.text = "TEST";
           var container:UIComponent = new UIComponent();
           container.addChild(testLabel);
           pj.addPage(container);
           pj.send();
      }

       

      Thanks, Cheers!

        • 1. Re: Printing with PrintJob
          Flex harUI Adobe Employee

          You will need to addChild(container) and call container.validateNow()

          • 2. Re: Printing with PrintJob
            BUJOE Level 1

            Changed my code to:

             

            var pj:PrintJob = new PrintJob();
            if (pj.start())
            {
                 var testLabel:Label = new Label();
                 testLabel.width = 400;
                 testLabel.text = "TEST";
                 var container:UIComponent = new UIComponent();
                 container.addChild(testLabel);

             

                 addChild(container);

                 container.validateNow();


                 pj.addPage(container);
                 pj.send();
            }

             

             

            What I get now is a blank page with a grey-filled square - suppose this stands for the Label component.

             

            Is the statement order ok? I think so. Hmm!?

            • 3. Re: Printing with PrintJob
              BUJOE Level 1

              By the way:

               

              The addChild call seems do have no effect in my case - and I don't even understand it why it has to be used. What caused that grey-filled square to appear on the print-out is the validateNow call.

              • 4. Re: Printing with PrintJob
                BUJOE Level 1

                In order to make the grey square disappear, I had to accordingly set the hight and width of the component:

                 

                container.width = pj.pageWidth;

                container.height = pj.pageHeight;

                 

                I also recognized that it prints images (Image component) when added to the container, but the text of the Label component is still missing. Hmm!?

                 

                P.S. As I said, I can't embed fonts, since this seems to solve such problems, according to solutions found on the web.

                 

                Cheers

                • 5. Re: Printing with PrintJob
                  Flex harUI Adobe Employee

                  UIComponent is not a good container.  Maybe use Canvas instead.

                  • 6. Re: Printing with PrintJob
                    BUJOE Level 1

                    Ok, with the following code I get to the attached result:

                     

                    var pj:PrintJob = new PrintJob();
                    if (pj.start())
                    {
                    var container:VBox;

                    try {
                      var testLabel:Label = new Label();
                      testLabel.width = 400;
                      testLabel.text = "TEST";
                      container = new VBox();
                      container.width = pj.pageWidth;
                      container.height = pj.pageHeight;
                      container.setStyle("backgroundColor", "#FFFFFF");
                      container.addChild(testLabel);
                      addChild(container);
                      container.validateNow();
                      pj.addPage(container);
                      pj.send();
                    }
                    catch (error:Error)
                    {
                      Alert.show(error.toString());
                    }
                    finally {
                      // clean-up code
                      removeChild(container);
                    }
                    }

                     

                    I can't understand where the grey bar comes from?!

                    • 7. Re: Printing with PrintJob
                      BUJOE Level 1

                      My next step, which was the original problem I tried to solve, is the rotation to landscape of the content. Since I can't make use of embedded fonts I see only the solution to create an image of the page and rotate it 90 degrees. The following code works in that way that it makes the rotation, but the content disappears and instead is shown a grey bar. Without rotation the page is shown as attached.

                       

                      var pj:PrintJob = new PrintJob();
                      if (pj.start())
                      {
                      var container:VBox;
                      var vBox:VBox;

                      try {
                            var testLabel:Label = new Label();
                            testLabel.width = 400;
                            testLabel.text = "TEST";
                            container = new VBox();
                            container.width = pj.pageWidth;
                            container.height = pj.pageHeight;
                            container.setStyle("backgroundColor", "#FFFFFF");
                            container.addChild(testLabel);
                            addChild(container);
                            container.validateNow();
                          
                            var bitmap:BitmapData =

                                ImageSnapshot.captureBitmapData(testLabel);
                            var img:Image = new Image();

                            // comment out following line for no rotation
                            img.rotation = 90;
                            img.width = 400;
                            img.height = 20;
                            img.source = new BitmapAsset(bitmap);  

                            vBox = new VBox();  
                            vBox.addChild(img);
                            vBox.width = pj.pageWidth;
                            vBox.height = pj.pageHeight;
                            addChild(vBox);
                            vBox.validateNow();
                         
                            pj.addPage(vBox);
                            pj.send();
                           }
                      catch (error:Error)
                      {
                        Alert.show(error.toString());
                      }
                      finally {
                         // clean-up code
                         removeChild(container);
                         removeChild(vBox);
                      }
                      }

                       

                      Do I have to call something when I rotate the component?

                      • 8. Re: Printing with PrintJob
                        Flex harUI Adobe Employee

                        Instead of container.validateNow() try this.validateNow().