9 Replies Latest reply on Apr 25, 2011 10:34 AM by Flex harUI

    How can I set the margin when printing from Flex?

    JoshBeall Level 1

      Hi All,

       

      I'm using the FlexPrintJob class to print a particular object in my app when the user clicks a "print" button.  It works fine, except for one issue: it prints with no margin.  In the case of a physical printer, it seems to print as close to the edge of the paper as printer is able.  If you're printing to a PDF, you get a PDF file where the printed objects run right to the "edge" of the PDF page.

       

      I would like to specify an explicit margin of perhaps 0.75 inches.  If I have to do it in some other unit (points, pixels, etc.), that's fine.  How can I set the margin?

       

      Thanks!

       

        -Josh

        • 1. Re: How can I set the margin when printing from Flex?
          JoshBeall Level 1

          Here's what I'm doing.  Is this right?

           

          if(printer.start()){
               itemsToPrintContainer.paddingBottom = 100;
               itemsToPrintContainer.paddingTop = 100;
               itemsToPrintContainer.paddingLeft = 100;
               itemsToPrintContainer.paddingRight = 100;
               
               printer.addObject(itemsToPrintContainer);
               printer.send();
               
               itemsToPrintContainer.paddingBottom = 0;
               itemsToPrintContainer.paddingTop = 0;
               itemsToPrintContainer.paddingLeft = 0;
               itemsToPrintContainer.paddingRight = 0; 
          }
          

          • 2. Re: How can I set the margin when printing from Flex?
            JoshBeall Level 1

            Ok, this doesn't work if you have content that's too large to fit on a single page with 100 pixel margins.  In such a case, there is no bottom margin on the first page if there are multiple pages, and there are no top or bottom margins on any subsequent pages, except the last page, which has only a bottom margin.  This makes sense, if you think about how I added a "margin".  I took the object, and added padding on all sides.  That works fine if you're only printing one page. But if you have to chop that object up to span multiple pages, then you can visual how the "middle" pages would not have a top or bottom "margin" (i.e., padding), since the margin/padding only exists at the outside edges of the object, not at spots where it was chopped up to fit onto separate pages.

             

            So, I'm wondering if the "workaround" is to take an image snapshot of the object I want to print with mx.graphics.ImageSnapshot, and then chop that object into appropriately size "chunks", each of which will fit on a page.  The problem is that I have to assume the paper size that the user will be using.  In our case, 99% of our users will be printing to 8.5"x11" paper, but we're likely to have 8.5"x14", and A4 sizes from time to time.  I don't know how to accomodate those users unless I can come up with a better solution?

             

            Another problem is that I have been unable to figure out how to print the BitmapData object that you get with the mx.graphics.ImageSnapshot class.  I tried creating an Image component, setting it's source property to the BitmapData object, and printing that--but nothing printed.  I get the print dialog, but nothing spits out of the printer (when I print to a physical printer) and I don't get a PDF file created (when I print to a PDF printer).

             

            So, two questions:

             

            1) Is there a better solution that taking a snapshot, breaking it up, and printing that snapshot?

            2) If there is no better solution, how can I print BitmapData?

             

            Thanks for any input...

             

              -Josh

            • 3. Re: How can I set the margin when printing from Flex?
              Flex harUI Adobe Employee

              I still recommend a set of custom print views so you can control the page

              breaks.

               

              And calling validateNow

              • 4. Re: How can I set the margin when printing from Flex?
                JoshBeall Level 1

                Flex harUI wrote:

                 

                I still recommend a set of custom print views so you can control the page

                breaks.

                 

                And calling validateNow

                Ok, so how would I do that?  And why and when I call "validateNow?"

                • 5. Re: How can I set the margin when printing from Flex?
                  Flex harUI Adobe Employee

                  You would call validateNow before addObject.

                  • 6. Re: How can I set the margin when printing from Flex?
                    JoshBeall Level 1

                    Ok, and how would having separate views for printing help me to solve the fundamental problem I've got, which is creating a margin of a specific size on the printed output?

                    • 7. Re: How can I set the margin when printing from Flex?
                      Flex harUI Adobe Employee

                      Well, I'm guessing that you might be having some timing issue with changing

                      the margins and getting the old view to re-layout before addObject captures

                      the page.  By default new layouts are deferred which is why validateNow() is

                      used, but maybe there is some remaining issue.

                       

                      Another problem you might be hitting is that you have so many things in the

                      view that the margin plus the sizes of things don't all fit

                       

                      Plus, many of our UI widgets do not look good when printed.  I don't like

                      seeing ComboBoxes on my printout.  Or scrollbars.

                       

                      So what I typically do is build up a new set of views, replacing ComboBoxes

                      with Labels and seeing how much fits on one page, and then creating a second

                      view with other stuff.  Sometimes, new things are added to the views, like

                      the date and user's name, which you typically don't display prominently in

                      the interactive view.

                       

                      Those same views are used for print preview.

                       

                      And then at print time, I'm not trying to modify an existing view on the

                      fly, I'm just printing the print views.

                      • 8. Re: How can I set the margin when printing from Flex?
                        JoshBeall Level 1

                        I don't think I'm having any timing issues with things not finishing re-laying out before the job gets sent to the printer.  My problem is that I want to print a series of objects (all in one container) that are too big to fit on a single page, and I don't know how to make it the objects have a top/bottom margin on where they span across pages.

                         

                        What you propose sounds like a more "complete" solution that I will investigate, but I'm hoping to get an immediate solution that will at least allow users to print multiple pages with top and bottom margsin on all pages (not just a top margin on the first page and a bottom margin on the last page).  Is the only option to manually send multiple "pages" to the printer, trying to guess what the most likely paper size is, and only sending enough objects-per-page to the printer that will fit on that theoretical paper size?

                         

                          -Josh

                        • 9. Re: How can I set the margin when printing from Flex?
                          Flex harUI Adobe Employee

                          The page size is known once PrintJob.start() returns.

                           

                          There is no intelligence in the printing subsystem that will paginate your

                          view for you other than just chopping at the boundaries.  You will have to

                          come up with your own solution, and I am suggesting that the "complete"

                          solution will be faster than your attempt at some other solution.