473,698 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 (anotherpagenee ded)
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 3241
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.P rint event. This
event passes a System.Drawing. Printing.PrintP ageEventArgs 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 PrintPageEventA rgs class has a member called "HasMorePag es." 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.goo glegroups.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 (anotherpagenee ded)
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 "sugggestio ns 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 "HasMorePag es." 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
"HasMorePag es" 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.goo glegroups.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 "sugggestio ns 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
SimplePrintDocu ment 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, EasyPrintDocume nt.
That class would have a pages collection Using that class would follow
the pattern below:

EasyPrintDocume nt epd = new EasyPrintDocume nt();

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.goog legroups.com...
Kevin Spencer wrote:
>I'm confused. You say you want "sugggestio ns 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
SimplePrintDocu ment 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, EasyPrintDocume nt.
That class would have a pages collection Using that class would follow
the pattern below:

EasyPrintDocume nt epd = new EasyPrintDocume nt();

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 "instructio ns" 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
2155
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 really stuck here (probably the ol' Not seeing the tree cause of the forest thingy). But I just can not get this to work. The following is my Test example code. Just needs a Button1 on a blank Form. Add in PrintDocument1, PrintPreviewDialog1,...
2
3460
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 type fonts. So I assume it's possible to send Font Type and the text to the printer, and the printer should digest and print faster. Still, barcode info needs to be sent as an image. When I print fifty copies of the same label it prints really...
5
10135
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 System.Drawing; using System.Drawing.Printing; using System.IO;
8
9624
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 help. The problem is this: If I print a rectangle which begins at (0,0) and the margins are also set to 0 (l:0, t:0, r:0, b:0) then it prints fine (ok, not quite because 0,0 is inside the none printable area but I corrected for that by checking...
4
9285
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 this differences increases with the increase number of labels. The code is as follwods: Here rdr = OleDbDataReader Font is Times New Roman, 12pt
6
482
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 trouble getting it to print on multiple pages. The second page will not print at all. Thanks in advance
3
31913
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 Printing.PaperSize("sdfgsfdg", 850, 1100)
1
5709
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
4
2484
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 top of the previous one. So my loop is working, I have the hasmorepages= true set right after each cycle till the loop finishes. Hopefully someone can point out my error. Thanks David DeWitt this part calls the printing code
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.