I need to add couple of text lines on the top of the page when print an UltraGrid. Also I need to show preview dialog before print . The print output should be something like this for example:
Number: 123456 Name: ABCD
Address: xxxxxxxx Zip: 12345
----------------------------------------------
| my UltraGrid data |
-----------------------------------------------
So I added UltraGridPrintDocument and UltraPrintPreviewDialog in my form. And I tried to use that "ultraGridPrintDocument.Header.TextLeft" property but it seems can only add one text line and also cannot be formatted to align those fields on the top. Here is my code snip:
ultraGridPrintDocument.Header.TextLeft = "Number...................";
ultraPrintPreviewDialog.Document = ultraGridPrintDocument;
ultraPrintPreviewDialog.ShowDialog(this);
So what is the right way to add text lines on top of the Grid and also make those text fields aligned? Should I put those text fields into another UltraGrid and add that on top of my data grid when print? Please provide some sample code if possible.
Thanks!
Hi,
The grid print operation creates it's own print job. So there's no way to add anything into the printout except by using the header and/or footer of the page. These will be repeated on each page, of course, so that might not be what you want.
If using the page header is okay, then you can use as many lines as you like by putting line breaks into your header text.
this.ultraGridPrintDocument1.Header.TextLeft = "Number: xxxxxx" + Environment.NewLine + "Address: xxxxxxx";
I am not sure what you mean by "cannot be formatted to align those fields on the top."
Thanks Mike. It seems to me using the header of the page is okay even it is repeated in each page. What I mean to "align the fields on the top" is to put those fields in column kind of alignment, something as this:
Number: 12345 Name: abcdefghijkl
Address: xxxxxxxxxxxxxxxxx Zip: 12345
I basically want "Number:...' and "Address:..." are aligned as first column and 'Name:..." and "Zip:..." are aligned as second column. And other fields are aligned as third column etc.. I figured out a way to do that by padding spaces on each field and then use the fixed width font such as "Courier New" for header to make that happen. Do you have any better way to do that?
Another question is since each line in the header might be very long, how to calculate which font size I should use to fit them in the width of the print page?
Well, you might try using tab characters in the string rather than spaces. But spaces allow you more control and there's really nothing wrong with using them, especially if you already wrote the code.
Another option would be to use the TextLeft, TextCenter, and TextRight as your columns.
As for measuring the text, that's not an easy thing to do. You'd have to measure using the printer's graphics, and I'm not even sure if you would use GDI or GDI+. I guess you would have to use the PagePrinting event of the UltraGridPrintDocument, since that event gives you the graphics. Then you could try using Infragistics.Win.DrawUtility.MeasureString to measure the strings and see if they fit on the page. But this would be very complicated and would require a lot of trial and error, I think.