none
Printing to "Fit to Printable Area" and to "Fit to Margins"

    Question

  •  

    I have a grid control in which I have placed some text, drawings, and images in it using XML. 

    Now, I wanted to print everything I placed in the grid (which are what I'm seeing on the window I have created) to fit to the page's printable area and/or to the margins I have set for the media. Can anybody help me on how could this be done? I'm actually new to WPF and C# so, pardon me if my question seem a little vague or unclear. All helps would be very much appreciated.

    Thursday, November 08, 2007 1:42 AM

Answers

  • Ahhh -- let me suggest another approach.

     

    If you want to print a Grid that's already on the screen, you probably don't want to mess around with the size of the Grid when you're printing the Grid because the size will also change on the screen.

     

    Instead, try this approach, which creates a VisualBrush based on the Grid:

     

    When you're ready to print, create a Rectangle and set its Width and Height properties to the size you want to print.  (Remember, 96 units to the inch, so the Width will be something like 624 and the Height 864 to print on 8.5 x 11" paper with 1-inch margins.)  Create a VisualBrush based on the displayed Grid and set it to the Fill property of the Rectangle.  Now call Measure on the Rectangle using an infinite size.  Get the DesiredSize of the Rectangle.  Call Arrange on the Rectangle based on the Point where the upper-left corner is to be displayed on the page -- something like (96, 96) depending on the margins you want -- and the DesiredSize.  Now you can pass the Rectangle object to PrintVisual.

     

     

     

     

     

    Thursday, November 08, 2007 1:58 PM

All replies

  • This blog entry might help you get started:

     

    http://www.charlespetzold.com/blog/2006/02/230940.html

     

    Thursday, November 08, 2007 2:41 AM
  • Wow! I'm so honored that the author of the book I'm reading is the one answering my concern.

     

    Anyways, I've tried doing the samples on the book but I couldn't get my grid to fit/expand on the entire printable area.

    The grid actually has a definite size defined already. How do I alter the size of the grid and all its contents to fit to the entire printable area?

     

     

     

     

    Thursday, November 08, 2007 7:28 AM
  • Ahhh -- let me suggest another approach.

     

    If you want to print a Grid that's already on the screen, you probably don't want to mess around with the size of the Grid when you're printing the Grid because the size will also change on the screen.

     

    Instead, try this approach, which creates a VisualBrush based on the Grid:

     

    When you're ready to print, create a Rectangle and set its Width and Height properties to the size you want to print.  (Remember, 96 units to the inch, so the Width will be something like 624 and the Height 864 to print on 8.5 x 11" paper with 1-inch margins.)  Create a VisualBrush based on the displayed Grid and set it to the Fill property of the Rectangle.  Now call Measure on the Rectangle using an infinite size.  Get the DesiredSize of the Rectangle.  Call Arrange on the Rectangle based on the Point where the upper-left corner is to be displayed on the page -- something like (96, 96) depending on the margins you want -- and the DesiredSize.  Now you can pass the Rectangle object to PrintVisual.

     

     

     

     

     

    Thursday, November 08, 2007 1:58 PM
  •  

    This is really great! It works! 

     

    But I have a little problem though, I can't seem to print my whole grid if I set my upper-left corner point to (0,0). In this situation, I've set the Width and Height properties of my rectangle to the PrintableAreaWidth and PrintableAreaHeight respectively (no margins set just yet). My grid and all its contents did expand but when I printed it, a part of the left and top sides of my grid were not printed.

     

    I have another question too. Does the PrintDialog's PrintableAreaWidth and PrintableAreaHeight considered standard or applicable to all printers or paper sizes? Or do these properties vary from printer to printer or from paper to paper?

    Monday, November 12, 2007 9:43 AM
  • The PrintableAreaWidth and PrintableAreaHeight properties are misnomers.  They actually return the total size of the page in 1/96th inch.  The properties will reflect the actual paper size in the printer, and also whether you've selected Portrait or Landscape printing.

     

    Unfortunately, you don't have access to the size of the unprintable margins of the page, and you just have to impose some kind of reasonable minimum (for example, 1/2 inch).  The PrintWithMargins program in Chapter 17 of "Applications = Code + Markup" contains a dialog box to set margins, and shows how to use those margins.

     

     

     

    Monday, November 12, 2007 2:13 PM
  • Thank you very, very much. This is really a big help. I truly appreciate it.

     

    Thank you! =)

     

    Wednesday, November 21, 2007 9:47 AM
  • The unprintable margins are available through PrintCapabilities:

     

    PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);

    double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth/printArea.ActualWidth,

    capabilities.PageImageableArea.ExtentHeight/printArea.ActualHeight);

    printArea.LayoutTransform = new ScaleTransform(scale, scale);

    Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

    printArea.Measure(sz);

    printArea.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

    dlg.PrintVisual(printArea, "?");

     

    This will fit the printArea visual within the available print canvas.

     

    Cheers

    Friday, February 15, 2008 2:49 PM
  • got an interesting one...
    im using this method to build code to do paging of my content and print it... filling a rectangle with differnet parts of my canvas etc which has numerous lines and shapes on it.

    Using this method should preserve the vector based data and result in a very clean printout. And this is true, if i print to an XPS document. But, when I print to anything else - Acrobat PDF printer, or a laser printer i have here- for some reason, the data is rasterized. The laser output looked pretty good, but is def more grainy than a normal laser printout. And with the PDF, I can zoom in on it and see jaggies right away.

    yall see any reason this should be happening?

    one more thing- during one test, i managed to print my entire *very long* canvas in one sheet of paper... and strangely enough, it looks like the PDF it produces had a mix of rasterized and vector graphics.. and the fonts were all non-rasterized. Crazy!
    Thursday, August 21, 2008 12:36 AM
  • ok ive figured out the rasterizing part... dont use any translucent parts, and a couple other items, now my vector data is staying vectors in my pdf output.

    Nelis, the printcapabilities is very handy, but I dont know if this this might be a bug I'm experiencing - Using the acrobat print to pdf, the capabilities is populated fine if i use a standard page size. but if i define a new page size, ie a page modified to be 100 inches long, the capabilities is then null. I'm trying to emulate printing on a long "continuous feed" printer, but no matter what I do, the page may show up as 100" long, but hte content on each page is still truncated to approx 11"... as if there were some hardcoded page length value stored somewhere. Im wondering if the null printcapabilities is causing the xps writer to default to a generic page length for the output, instead of utilizing the full lengh of the page.

    any ideas?

    Sunday, September 28, 2008 2:02 PM
  • minor correction- the capabilities is populated, but the PageImageableArea is null in the case I discussed...
    • Proposed as answer by nischay Saturday, November 08, 2008 10:51 PM
    Sunday, September 28, 2008 2:18 PM
  • and i finally figured out, it looks like acrobat ignores the length if it gets too long, i just set it to a smaller (but still long) value of 44 inches and it prints fine. so I guess its hackin time if i want to print longer pages...
    Sunday, September 28, 2008 2:28 PM
  • Hello,

    Just for my concern how do you print you grid to PDF files from code?

    regards

    serge


    Your knowledge is enhanced by that of others.
    Tuesday, June 21, 2011 2:14 PM