Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

printing webpage

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike P - 23 Jul 2007 10:05 GMT
I am trying to use javascript to print my .NET page.  I am building up
some HTML using a stringbuilder and then using this javascript to try to
print it :

<input id="Button2" value="Print" onclick="javascript:window.print()">

The page is wider than a standard A4 page but should fit onto a page in
landscape view, yet when I try to print in landscape, I only get half of
the page across printing.

Any ideas how I can get around this?  Is there a better way to print web
pages using .NET?
Mark Rae [MVP] - 23 Jul 2007 10:47 GMT
> Is there a better way to print web pages using .NET?

I always use PDF. It's a bit more work, but you get the exact layout you
want every time...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Alan Silver - 23 Jul 2007 11:03 GMT
>I am trying to use javascript to print my .NET page.

Why? Every browser I have used has a print facility, so why not let the
user use that?

>  I am building up
>some HTML using a stringbuilder and then using this javascript to try to
[quoted text clipped - 3 lines]
>
>The page is wider than a standard A4 page

The, with all respect, you have designed the page badly. Web pages
should adapt themselves to the size of the output medium, whether that
is a browser window, printed page or anything else. This is one of the
basics of web standards.

> but should fit onto a page in
>landscape view, yet when I try to print in landscape, I only get half of
>the page across printing.
>
>Any ideas how I can get around this?  Is there a better way to print web
>pages using .NET?

Yes, develop standards-based fluid designs that work at any output size.
Also, if you mark up your (X)HTML with CSS, which is another basic of
standards-based design, then you can easily add styles for print, so you
can have a completely different design when printed if you like. The
most obvious use for this is to remove the extra links and banners from
the printed page, leaving just the basic content.

HTH

Signature

Alan Silver
(anything added below this line is nothing to do with me)

Mike P - 23 Jul 2007 12:20 GMT
Well, first of all, I did not design the page, I am working with some
code that was inherited by me.  I have now managed to change some of the
dimensions so that it prints on a single page in landscape format.  Is
there any way in my code to set the default page setup to landscape?
Alexey Smirnov - 23 Jul 2007 12:42 GMT
> Well, first of all, I did not design the page, I am working with some
> code that was inherited by me.  I have now managed to change some of the
> dimensions so that it prints on a single page in landscape format.  Is
> there any way in my code to set the default page setup to landscape?
>
> *** Sent via Developersdexhttp://www.developersdex.com***

try to set CSS programmatically

http://home.tampabay.rr.com/bmerkey/examples/landscape-test.html
Mike P - 23 Jul 2007 12:56 GMT
Alexey,

How would I be able to use CSS to set the setup to landscape?
Alexey Smirnov - 23 Jul 2007 13:13 GMT
> Alexey,
>
> How would I be able to use CSS to set the setup to landscape?
>
> *** Sent via Developersdexhttp://www.developersdex.com***

I think you can try following:

1) add the style as in the example I've sent you

<style type="text/css" media="print">
div.page  {
writing-mode: tb-rl;
height: 80%;
margin: 10% 0%;
}
</style>

2) put your page content between the <div id="toprint">...</div>

3) create js function to change the style of the print div

document.getElementById('toprint').className = 'page';
Alan Silver - 23 Jul 2007 15:48 GMT
>> Alexey,
>>
[quoted text clipped - 19 lines]
>
>document.getElementById('toprint').className = 'page';

What's the point of this? Why not just add a media type to the normal
style sheet? You don't need Javascript for this, it just makes life
complicated, and guarantees that some people will have problems with it.

Browsers all support printing. All modern browser support the media type
for stylesheets. CSS cannot control the user's printer settings, just
like it cannot control anything else about the user's machine. All you
are doing here is adding complexity that isn't going to work. Do it the
simple way and it will work.

Signature

Alan Silver
(anything added below this line is nothing to do with me)

Alan Silver - 23 Jul 2007 15:43 GMT
>Well, first of all, I did not design the page, I am working with some
>code that was inherited by me.

OK, sorry, didn't mean to cause any offence. Still might be worth either
redesigning the page, or getting the person who designed it to redo it
according to web standards. I can see from your questions and the
replies you are getting that you are going to end up with an
unnecessarily complicated spaghetti of code that is very likely to fail.
If the page were redesigned, you could have a simple and easy system
that is basically guaranteed to work anywhere.

>  I have now managed to change some of the
>dimensions so that it prints on a single page in landscape format.  Is
>there any way in my code to set the default page setup to landscape?

No, simply because your CSS cannot know anything about the client's
machine, their settings, their printer, their page size and a whole host
of other issues that could affect it.

If you make sure the design is fluid, it will fill the page,
irrespective of what printer, page size and settings the user has. You
won't need any Javascript functions and it will work.

CSS has a facility for specifying styles for printed media, so you can
set up a style sheet just for print. You can use that to make sure you
only display what is needed on paper.

HTH

Signature

Alan Silver
(anything added below this line is nothing to do with me)

Mike P - 23 Jul 2007 16:18 GMT
>>CSS has a facility for specifying styles for printed media, so you can
set up a style sheet just for print. You can use that to make sure you
only display what is needed on paper.

Can you recommend any good links with regard to using CSS for specifying
styles for printed media?
Alan Silver - 23 Jul 2007 16:48 GMT
>>>CSS has a facility for specifying styles for printed media, so you can
>set up a style sheet just for print. You can use that to make sure you
>only display what is needed on paper.
>
>Can you recommend any good links with regard to using CSS for specifying
>styles for printed media?

Learn from the original source...
http://www.w3schools.com/css/css_mediatypes.asp

Basically, media types are just like setting up normal styles, you just
add an extra bit of code to tell the browser only to use the styles when
printing. There's nothing magical about them. You just surround the bits
of CSS that apply to the printed media with...

@media print {
 /* put print styles here */
}

For example, the following sets different styles for screen and print...

<style type="text/css">
@media screen {
 p {
   color: #00f;
 }
}
@media print {
 p {
   color: #000;
 }
}
</style>

That rather simplistic style sheet will give blue text on screen and
black text when printed.

If you want a hand-holding walk through of setting up styles for print,
the rather excellent "Eric Meyer on CSS" has a chapter devoted to the
subject. This book is well worth buying anyway, as it gives a great
introduction to CSS.

HTH

Signature

Alan Silver
(anything added below this line is nothing to do with me)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.