By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Simple printing and graphics

P: n/a
The method for printing documents in .Net can be confusing, especially
for newer users. I would like to create a way to simplify this
process. My idea would be implemented using a PrintDocument (much like
the current model), but my PrintDocument would have a Pages collection
such that each time you need to have an additional page, you would just
add another page to the collection and then use the page object for the
actual drawing etc. Finally, when you wanted to submit the document to
the printer, you would call the Print method. The following pseudo
code shows what I intend:

PrintDocument pd = new PrintDocument();

//Add the first page
Page p = pd.AddPage();

//Here, draw things, data, etc. to the page
p.DrawString(x, y, font, "string to be drawn");
p.DrawCircle(x, y, radius);

if (anotherpageneeded)
p = pd.AddPage();

//More drawing to the page here

//Finally, submit the document to the printer
pd.Print();
My question, is how to represent or "store" the pages graphics. Should
I create a Graphics object internally to the page object? Or should I
create a "pseudo" drawing language and use something like a hashtable
to store each drawing command. Can a Graphics object be created and
then somehow "copied" to a destination graphics object?

I'm just looking for a suggestion on how to persist in each page object
the necessary commands to render the page to the printer's graphics
object. I guess I'm wondering if a Graphics object can be created,
drawn to, and then copied or rendered onto another Graphics object?
And how do I create a Graphics object that is the same scale, etc. as
the one used by the printer?

Thanks for any suggestions,

Chris

Jul 5 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
First of all, everything in the printed page is graphics, including text.

The PrintDocument class is what does the Printing. When you call the Print
method of the PrintDocument class, it prints. How you get your graphics into
it is by wiring up an event handler to the PrintDocument.Print event. This
event passes a System.Drawing.Printing.PrintPageEventArgs instance, which
contains, among other things, the Graphics object you need to draw to.

It's a bit more complex than that, however. The PrintDocument class also has
a PageSettings and PrinterSettings instance in it. These determine which
Printer to print to, any additional Printer settings, and page information
about the page to print to. This includes (very important) the Printable
Area of the page, and the Margins of the page. The Graphics instance passed
will be relative to the Printing area of the page, but may or may not
(optionally) include the margins.

The Print event of the PrintDocument is the place where you can do your
paging. The PrintPageEventArgs class has a member called "HasMorePages." If
you set this to true, the handler will be called again. Since the handler
does the drawing, it can draw as many pages as you wish, just by changing
what it draws with each event.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@a14g2000cwb.googlegr oups.com...
The method for printing documents in .Net can be confusing, especially
for newer users. I would like to create a way to simplify this
process. My idea would be implemented using a PrintDocument (much like
the current model), but my PrintDocument would have a Pages collection
such that each time you need to have an additional page, you would just
add another page to the collection and then use the page object for the
actual drawing etc. Finally, when you wanted to submit the document to
the printer, you would call the Print method. The following pseudo
code shows what I intend:

PrintDocument pd = new PrintDocument();

//Add the first page
Page p = pd.AddPage();

//Here, draw things, data, etc. to the page
p.DrawString(x, y, font, "string to be drawn");
p.DrawCircle(x, y, radius);

if (anotherpageneeded)
p = pd.AddPage();

//More drawing to the page here

//Finally, submit the document to the printer
pd.Print();
My question, is how to represent or "store" the pages graphics. Should
I create a Graphics object internally to the page object? Or should I
create a "pseudo" drawing language and use something like a hashtable
to store each drawing command. Can a Graphics object be created and
then somehow "copied" to a destination graphics object?

I'm just looking for a suggestion on how to persist in each page object
the necessary commands to render the page to the printer's graphics
object. I guess I'm wondering if a Graphics object can be created,
drawn to, and then copied or rendered onto another Graphics object?
And how do I create a Graphics object that is the same scale, etc. as
the one used by the printer?

Thanks for any suggestions,

Chris

Jul 5 '06 #2

P: n/a
Kevin Spencer wrote:

<snipped big explanation of current .Net printing process>

Thank you for your response. I understand the .Net way of printing.
That was not my question. I wish to create a class for simplifying the
process. My original post described how I wish for the process to
occur.

My question was a request for suggestions on how to implement the
rendering of each page. Ultimately, behind the scenes, my class would
use the existing .Net printing infrastructure to actually print the
document. I am attempting to create a class to make it easier to
print. My thought was a document class with a Pages collection. The
user of the class could use that metaphor more easily than the event
driven nature of the .Net way.

Thanks again.

Chris

Jul 6 '06 #3

P: n/a
I'm confused. You say you want "sugggestions on how to implement the
rendering of each page." This would refer, I presume to "printing each
page?" That's not hard at all to do, which is why I reviewed how the
PrintDocument prints. It raises an event when you call the Print method. The
event passes a boolean "HasMorePages." If this is turned on in the event
handler, the event will fire again. So, let's say you've got an array, a
collection, an aggregate group of objects. All you have to do is set the
"HasMorePages" value to true until you get to the last item in the
Collection. As you keep track of the current position in the Collection, you
print each item in the same event handler.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Kevin Spencer wrote:

<snipped big explanation of current .Net printing process>

Thank you for your response. I understand the .Net way of printing.
That was not my question. I wish to create a class for simplifying the
process. My original post described how I wish for the process to
occur.

My question was a request for suggestions on how to implement the
rendering of each page. Ultimately, behind the scenes, my class would
use the existing .Net printing infrastructure to actually print the
document. I am attempting to create a class to make it easier to
print. My thought was a document class with a Pages collection. The
user of the class could use that metaphor more easily than the event
driven nature of the .Net way.

Thanks again.

Chris

Jul 6 '06 #4

P: n/a
Kevin Spencer wrote:
I'm confused. You say you want "sugggestions on how to implement the
rendering of each page." This would refer, I presume to "printing each
I'm sorry for the confusion, I guess my intent is not clear. I want to
wrap the .Net printing method into a simpler class. I understand how
the HasMorePages and the event model of the .Net PrintDocument classes
work. I want to create a class that wraps that functionality up into
an easy to use class. For example of what I am trying to do,
originally in the .Net 2.0 Beta there was a class called
SimplePrintDocument but it was not included in the final release. Here
is a link that describes that class:

http://wesnerm.blogs.com/net_undocum..._printing.html

I want to create a class similar to that which encapsulates the
complexity of the .Net net printing method.

My idea was to create a simple class called say, EasyPrintDocument.
That class would have a pages collection Using that class would follow
the pattern below:

EasyPrintDocument epd = new EasyPrintDocument();

EasyPrintPage page = epd.AddPage();

page.DrawText("Blah Blah", 100. 40); //draws text at location 100,
40 on this page.
page.DrawCircle(50, 50, 10); //draws circle of radius 10 at location
50, 50 on this page.

Once the user has added all the pages they need the document, they
would call a method on my class to acutally kick off the printing
process:

epd.PrintIt();

Behind the scenes, this would use the normal .Net printing methodology,
including the HasMorePages.

I want to *hide* that complexity in an easy to use class.

My question was, specifically, how to store the page images until the
user calls the PrintIt method of my class. In other words, can I
create a collection of Graphics objects, for example, and then use them
when it comes time to print my pages? How can I create a Graphics
object with the same dimensions of the printer?

I hope I have explained better what I want.

Thanks again for taking time to respond.

Jul 6 '06 #5

P: n/a
I'm still confused. The objects should draw themselves when they need to.
This is essentially what the Control.Paint method does. So, you don't store
images; you store the data that defines what the images are to be drawn. You
could store the images too, but that would be counter-productive in most
situations. You only need them when you need them. As for "Graphics," I'm
not sure you're talking about images or the Graphics class. If you're
talking about the Graphics class, as I said, it is provided in the Print
event handler (just like in the Paint event handler). You also have to
handle transparency and Z-Levels.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Kevin Spencer wrote:
>I'm confused. You say you want "sugggestions on how to implement the
rendering of each page." This would refer, I presume to "printing each

I'm sorry for the confusion, I guess my intent is not clear. I want to
wrap the .Net printing method into a simpler class. I understand how
the HasMorePages and the event model of the .Net PrintDocument classes
work. I want to create a class that wraps that functionality up into
an easy to use class. For example of what I am trying to do,
originally in the .Net 2.0 Beta there was a class called
SimplePrintDocument but it was not included in the final release. Here
is a link that describes that class:

http://wesnerm.blogs.com/net_undocum..._printing.html

I want to create a class similar to that which encapsulates the
complexity of the .Net net printing method.

My idea was to create a simple class called say, EasyPrintDocument.
That class would have a pages collection Using that class would follow
the pattern below:

EasyPrintDocument epd = new EasyPrintDocument();

EasyPrintPage page = epd.AddPage();

page.DrawText("Blah Blah", 100. 40); //draws text at location 100,
40 on this page.
page.DrawCircle(50, 50, 10); //draws circle of radius 10 at location
50, 50 on this page.

Once the user has added all the pages they need the document, they
would call a method on my class to acutally kick off the printing
process:

epd.PrintIt();

Behind the scenes, this would use the normal .Net printing methodology,
including the HasMorePages.

I want to *hide* that complexity in an easy to use class.

My question was, specifically, how to store the page images until the
user calls the PrintIt method of my class. In other words, can I
create a collection of Graphics objects, for example, and then use them
when it comes time to print my pages? How can I create a Graphics
object with the same dimensions of the printer?

I hope I have explained better what I want.

Thanks again for taking time to respond.

Jul 6 '06 #6

P: n/a
Kevin Spencer wrote:
I'm still confused. The objects should draw themselves when they need to.
Yes, but once the code exits the PrintPage event handler, you can no
longer affect that page. What I have in mind with my Pages collection
would allow the user to go back to a prior page and draw more on it
before submitting it to the printer. One scenario where this might be
useful would be in a multi page report and at the bottom you might want
some text that said: Page 1 of X where is not necesarily known in
advance. With .Net's printing method, you have to calculate the number
of pages ahead of time. I had envisioned being able to revisit all the
pages, and perhaps adding something like this after you know how many
pages you had.
This is essentially what the Control.Paint method does. So, you don't store
images; you store the data that defines what the images are to be drawn.
I diidn't intend to store an image or bitmap. I wanted to define a way
to persist the "instructions" for rendering a single page somehow.
situations. You only need them when you need them. As for "Graphics," I'm
not sure you're talking about images or the Graphics class. If you're
I was referring to the Graphics class.
talking about the Graphics class, as I said, it is provided in the Print
event handler (just like in the Paint event handler). You also have to
handle transparency and Z-Levels.
I realize there is a Graphics object in the event handler. As I stated
in my previous post, the user would not be interacting with the .Net
PrintDocument directly. He would be using my class and it's Pages
collection and the methods I make available. I am trying to create a
more intuitive metaphor for printing a document. In my mind, I have a
document. The document has pages and on the pages are text, images,
etc.

So in programming it, I would first create my document object. Then I
would add pages to it's pages collection. On each page I would use
methods of my class to add content. Somehow this content would be
persisted on each page. After I have assembled all my pages, I would
call a method on my class to actually submit the document to the
printer. The class would take care of creating the actual
PrintDocument object, setting up the PrintPage event handlers, setting
HasMorePages as requred. But the user would be isolated from that
complexity.

I was asking for suggestions on persisting each page until it was time
to submit to the printer. I only suggested the Graphics class or an
Image because I couldn't think of something else at the moment.

Thanks again.

Jul 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.