Printing in C#, need code to accomplish this

K

kalyan

Hi All

I have a table with 10 rows and 15 columns...when i click on a button
each row should be printed in a single page with the line break to the
corresponding columns of that particular row...

as an example

page-1
--------
record1--col1
col2
.....
.....


page-2
--------
record2--col1
col2
.....

can any body help me in this

Thanks & Regards
Kalyan
 
P

Peter Duniho

kalyan said:
Hi All

I have a table with 10 rows and 15 columns...when i click on a button
each row should be printed in a single page with the line break to the
corresponding columns of that particular row...

Start here:

http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx

See the Graphics class for specific methods you can use to actually draw
your data to the Graphics instance you get in the PrintPage event. You
are likely to find MeasureString() and DrawString() of the most use.
MeasureString() will tell you how much room a given printed row of data
takes (ie, one of your table's columns), so that you can calculate the
new position for drawing the next row, and DrawString() of course will
draw the actual string.

If you have questions after reading that and the related pages (you do
need to learn more than just the event...there's a handful of lines of
code needed to get the printing started, but the sample code at that
document page includes that, so it shouldn't be that hard), please feel
free to post those more specific questions here, and we will try to help.

Pete
 
K

kalyan

Start here:

http://msdn2.microsoft.com/en-us/library/system.drawing.printing.prin...

See the Graphics class for specific methods you can use to actually draw
your data to the Graphics instance you get in the PrintPage event. You
are likely to find MeasureString() and DrawString() of the most use.
MeasureString() will tell you how much room a given printed row of data
takes (ie, one of your table's columns), so that you can calculate the
new position for drawing the next row, and DrawString() of course will
draw the actual string.

If you have questions after reading that and the related pages (you do
need to learn more than just the event...there's a handful of lines of
code needed to get the printing started, but the sample code at that
document page includes that, so it shouldn't be that hard), please feel
free to post those more specific questions here, and we will try to help.

Pete

Helo Pete

Thanks for u r reply...but here my application is a web
application...any more ideas...?

Thanks & Regards
Kalyan
 
K

Kevin Spencer

You're going to have problems in a web application. There is a CSS
specification for printing, but it is not supported entirely well by any
browser. Page Breaks, for example, are not supported by Internet Explorer,
even version 7. FireFox does a pretty good job with it, but all browsers
that I know of ignore Orientation styles.

On the other hand, if you want to target IE 7 only, you can go with the XPS
(eXtensible Print Specification) that Microsoft released recently. The .Net
Framework has support for creating XPS documents. XPS provides everything
needed to print a document, and XPS documents can be displayed just like
HTML or XML documents in IE 7 without any issues. OTOH, IE7 is the only
browser that I know of which supports XPS straight out of the box.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
K

kalyan

You're going to have problems in a web application. There is a CSS
specification for printing, but it is not supported entirely well by any
browser. Page Breaks, for example, are not supported by Internet Explorer,
even version 7. FireFox does a pretty good job with it, but all browsers
that I know of ignore Orientation styles.

On the other hand, if you want to target IE 7 only, you can go with the XPS
(eXtensible Print Specification) that Microsoft released recently. The .Net
Framework has support for creating XPS documents. XPS provides everything
needed to print a document, and XPS documents can be displayed just like
HTML or XML documents in IE 7 without any issues. OTOH, IE7 is the only
browser that I know of which supports XPS straight out of the box.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net









- Show quoted text -

Hi Kevin

So you mean to say that i should opt for Window.print for my
application...any more examples (piece of code preferrable)

Thanks & Regards
 
P

Peter Duniho

Kevin said:
You're going to have problems in a web application. There is a CSS
specification for printing, but it is not supported entirely well by any
browser. Page Breaks, for example, are not supported by Internet Explorer,
even version 7.

Can you clarify this? I use the "page-break-after" style for page
breaks in IE7 without any trouble. I'm pretty sure it worked in IE6
too, though I haven't bothered to go back and check. I don't do a lot
of HTML print layout so I've never bothered to try the other page-break
styles, but given that "page-break-after" works, I suspect they do too.

Pete
 
G

G.Doten

Peter said:
Can you clarify this? I use the "page-break-after" style for page
breaks in IE7 without any trouble. I'm pretty sure it worked in IE6
too, though I haven't bothered to go back and check. I don't do a lot
of HTML print layout so I've never bothered to try the other page-break
styles, but given that "page-break-after" works, I suspect they do too.

Pete

I'm glad you asked that, Pete. I've been doing the same thing since IE
5, first time was about 6 years ago (also with page-break-before).
Worked great (well, as nice looking as CSS "hard-copy reporting"
capabilities allow, as Kevin points out).
 
K

Kevin Spencer

Hi Peter,

I'd love to see your CSS, because I haven't been able to get page-breaking
to work in IE 7. In my case, I've got HTML tables inside divs, one table
inside each div, and each div should have a page break before it, except for
the first page. I'm using an external style sheet in the page header, rather
than an inline style, if that makes any difference.

Here's a sample (from the style sheet):

@page following
{
page-break-after: auto;
page-break-before: always;
page-break-inside: avoid;
margin: auto;
}
@page first
{
page-break-after: always;
page-break-before: avoid;
page-break-inside: avoid;
}

....and some snippets of HTML:

<div id="first" style="page:first">
<div class="positioning" style="width:993px;">
<table class="tableHeader" cellpadding="0" cellspacing="0" style="
width:993px; color:#000000; background-color:; border-width:1px;
border-color:#000000;font-family:Microsoft Sans Serif; font-size:9pt;
font-weight:bold;">
<tr>
....
</tr>
</table>
</div>
</div>
<div style="width:993px; page:following">
....

Any ideas why these divs with tables in them are not breaking pages in IE 7?
As I mentioned, the breaks occur correctly in FireFox, after each div. I
also tried simply using an @page directive, with page-break-after: always;
and got the same results.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
P

Peter Duniho

Kevin said:
Hi Peter,

I'd love to see your CSS, because I haven't been able to get page-breaking
to work in IE 7.

Well, that's simple enough to provide, since I don't do anything fancy.
As an example, if I have a line-break and want the page-break to
happen there as well, I have <br style="page-break-after: always">.

In my case, I've got HTML tables inside divs, one table
inside each div, and each div should have a page break before it, except for
the first page. I'm using an external style sheet in the page header, rather
than an inline style, if that makes any difference.

Could be. Just because IE is respecting the page-break in an inline
style, that wouldn't necessarily mean that IE is handling the
pre-defined style correctly. I do very little HTML authoring myself,
and use CSS in a very simple, sparing way, so there could be all sorts
of IE bugs that I'm unaware of.

I had the impression that Microsoft made a strong effort to bring IE's
compliance with CSS up-to-date for IE7, and I would guess that they
have. But your use of the page-break-XXX styles are clearly more
complicated than what I'm doing, and so it could very well be that there
are still problems in IE7 with using them.

Pete
 
K

Kevin Spencer

Thanks for the info, Peter. It's always good to find out what does work in
IE 7 as well as what still doesn't. IE 7 is a significant improvement, but
still has a long way to go, from my experience. Admittedly, the problem
areas are less-travelled, but they do exist. I tend to wander into them a
lot for some reason!

I'm quite thrilled with the XPS format, however. I just hope it gets widely
adopted. OTOH, CSS Print styles have been around for awhile, and are
specific to HTML, so they really need to be broadly supported, which is not
even yet achieved in FireFox (e.g. Orientation style).

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top