473,416 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Simple printing and graphics

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
6 3208
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mr. B | last post by:
This is driving me NUTZ!!! I've been screwing around on this for a week now. And I have tried to find examples similar to what I have (nada). Got lots of streaming a TXT file... bah! I am...
2
by: qumpus | last post by:
My program right now generates USPS style shipping label using System.Drawing.Graphics. It works fine except that the printer prints really slowly. I want to make my program take advantage of true...
5
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using...
8
by: Tinus | last post by:
Hello all, Because you have been so helpfull the last couple of times, I thought after testing and wasting more than 20 pages (and google-ling for 3 days :-( ). I would ask you again for your...
4
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And...
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
3
by: D Witherspoon | last post by:
No matter what I do the default paper size is always either A3 or 11 by 8.5 .. Here is the code. Dim dlg As DialogResult pd.DocumentName = "Weld Image" Dim pkPaperSize As New...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
4
by: DeWittds | last post by:
I have asked before and got little responce, So I try and try again. The code below prints the data in the same place but does not advance the page. I can see the lblnumber change but print in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.