问题
I have a page from which the user will be able to print. However, the page which will get printed is not the one the user is viewing, but rather a new one I'd like to generate on the background and (possibly) only show the print dialog for it.
Just to make things clear:
- User is on "View.aspx" and clicks my Print button (not the browser's one).
- The Print button loads the content of "Printable.aspx" and displays a print dialog for it while the user is still on "View.aspx".
FYI, what I'm trying to avoid is to have the "Printable.aspx" open in a new window and then show its print dialog.
Thanks in advance.
回答1:
Use a combination of MEDIA tags in CSS to show/hide objects for printing.
<STYLE type="text/css">
@media print {
.PrintOnly {font-size: 10pt; line-height: 120%; background: white;}
}
@media screen {
.PrintOnly {display: none}
}
</STYLE>
You can make controls that are style Display:none
on media screen, so the user only sees them when printing.
<DIV class="PrintOnly">
This control will only show up during printing
</DIV>
Any of your controls can be classed as "PrintOnly" so you only see them when printing. You just need to have the css class defined once for "@media screen" and once for "@media print" to ensure they behave differently.
You can also bring in an entire stylesheet for print-only.
<LINK rel="stylesheet" type"text/css" href="screen.css" media="screen">
<LINK rel="stylesheet" type"text/css" href="print.css" media="print">
回答2:
When the Print button is clicked, add the Printable.aspx into a hidden panel of view.aspx. Respond by adding javascript into the onload event of view.aspx & print the hidden panel with window.print()
回答3:
I don't think what you are trying for is possible. But I could be wrong. Though the better way to approach this is to use a different css stylesheet.
回答4:
You could load the page into a hidden IFrame element, then use javascript to execute the print function of that page.
But, like @carter suggested, CSS is the better approach. Why load duplicate content more than once when you can just restyle the current content?
- It's also suggested in this question to use CSS: how to print the contents of a hidden iframe?
- This is a good resource for using the CSS way of printing: http://www.alistapart.com/articles/goingtoprint/
来源:https://stackoverflow.com/questions/3345688/printing-a-page-without-displaying-it