Sunday, March 12, 2006

Print preview in C#

while I was playing a while with the print preview function that we took in the programming.. I faced a problem in making it "Multi" page... I tried everything... but I failed... so... I googled for a solution for that... & here is what I got from

The person who wrote that article made a class called "MultipadPrintDocument " (as his application was called Multipad)

Here is some explanation as mentioned by him for how the print preview works...

MultipadPrintDocument implements the virtual methods OnBeginPrint() and OnPrintPage(). OnBeginPrint() sets the pointer to the current character (_offset) to 0 and the current page number to 1.
OnPrintPage() has one parameter of type PrintPageEventArgs. When the system calls OnPrintPage(), we have to typeset the page using the Graphics object in the PrintPageEventArgs parameter. We typeset the page by printing lines of text until the page is full or until we reach the end of the text. The PrintPageEventArgs parameter has a property MarginBounds which contains the page size. Unfortunately, the dimensions are expressed in hundredths of an inch.
In .NET, there is no GraphicsUnit which represents a hundredth of an inch. Because we're going to measure strings in GraphicsUnit.Document (1/300th of an inch), we convert the page dimensions by multiplying them with 3. Now we're ready to start printing the lines of the text.

First, we create a StringBuilder which will hold the text of a single line. We also create a GenericTypographic StringFormat object. It's very important to use GenericTypographic because otherwise, MeasureString() and DrawString() wouldn't behave as expected. We also set the tab stops: if a line of text contains tabs, the text will be formatted correctly (the tabs will not be replaced with spaces, they will act as real tabs). Finally, we set the PageUnit property of the Graphics object to GraphicsUnit.Document.

Before we start filling the page with lines, we check to see if there is enough space to print at least four lines: one line of text and three lines for the page number (two empty lines above the page number). If there isn't enough room, we simply leave the page blank. This situation should rarely occur.

Now we're ready to break up the text in lines. We read the text string one character at a time. We add the character to the line buffer (the StringBuilder object), except if the character is a NewLine or Eos (end of string). Note that NewLine is "\r\n" in Windows or "\n" in Unix, so we have to keep in mind that we don't simply skip one character but Environment.NewLine.Length characters (yes, we strive to perfection). If the character is a space or a tab, we save this position because when the line is full, we don't want to break the line in the middle of a word, no, we will print the line up to the last tab or space. For example, if we have the line "The quick brown ... lazy dog." and the line overflows at the 'd' of "dog", we will begin the next line with "dog" instead of "og". If the last character is a space or tab, we continue to add these characters. This way, the next line will never start with white space. If the line overflows or we encounter a NewLine or Eos, we print the text in the line buffer. Then we increment the y position of the next line and empty the line buffer. If there's still room for a new line of text and we still have text to print, we continue the loop; if not, we exit the loop, print the page number at the bottom of the page, increment the page number and set the property HasMorePages of the PrintPageEventArgs parameter to true if there's still text to print, otherwise we set it to false.

Here I am... modifying my code.. still I have some mistakes in the final view, but really my eyes are in their worst state now.. :S (Note: I lost my glasses which makes everything worse)
so.. I will go take a rest now & complete my code later..
sure I will tell u in case i discovered sth new...


Roaa said...

Here is the latest news about that issue.

My code has sth wrong & after all that.. it didn't work :S:S.. there is still a point I can't make.. how to make the G (object from graphics) draw on the 2nd page not the first one..mmmmm 8-)
I could make the multi-page thing.. but still how to tell G to draw in the next page..
I asked today the help of 3 mo3edeen.. & the answer was...
"dunno..I will try that & tell u later.."
seems its a very complicated issue :D :D..
Althought I thought its quite easy :)..

Anyway.. till then.. I haven't found a solution yet.. & I promise to send the code here as soon as this is done ISA..

Sally Abozaid said...

How r u doing ??

I think I heard Dr Ihab talking today abt multi-page printing ..
He was talking abt printing that Dr Roshdy gave us & then asked: "howwa eddalko el multi-page??" .. the answer came with a no & then he said: "akeed 3alashan da mawwal kebeer awe" .. anyway I think I'll ask him next time I see him isA & tell u the results if I did ..

Roaa said...

Thanks sally.. I really long for getting that code..
I got its logic..but still I have one problem, how to make G draw on the 2nd page not the 1st one.. 8-) I really wanna know...

Anyway, me too.. I want to post many things here, but I am waiting for the mid-terms to finish 3ala 7'eer ISA.. ya rab...