JS print combination function
- 2021-07-09 06:35:09
- OfStack
This article shares the JS printing combination function for everyone, which is comprehensive for your reference. The specific contents are as follows
1. Print locally-that is, print wherever you want
Solution:
Hide places you don't want to print
<
style type="text/css" media=print
>
.noprint{display : none }
Use when paging
.PageNext{page-break-after: always;}
Then add: class = "Noprint" to the page element that you don't want to print, so it won't appear in print and print preview.
Add where you want to paginate:
<
div class="PageNext"
>
<
/div
>
Just do it.
<
/style
>
Control where you don't want to print
<
p class="noprint"
>
Where you don't need to print
<
/p
>
2. Reference to build
WebBrowser is a browser control built into IE and does not require users to download it.
WebBrowser Control
<
object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'
>
<
/object
>
The usage of this component is listed as follows:
WebBrowser. ExecWB (1, 1) Open
Web. ExecWB (2, 1) Close all existing IE windows and open a new one
Web. ExecWB (4, 1) Save Web Page
Web. ExecWB (6, 1) Print
Web. ExecWB (7, 1) Print Preview
Web. ExecWB (8, 1) Print page setup
Web. ExecWB (10, 1) View page properties
Web. ExecWB (15, 1) appears to be revoked, pending confirmation
Web. ExecWB (17, 1) All selected
Web. ExecWB (22, 1) refresh
Web. ExecWB (45, 1) Close form without prompting
3. Examples
<head>
<script language="javascript">
<style type="text/css" media=print>
.noprint{display : none }
</style>
function printsetup()
{
// Print page setup
wb.execwb(8,1);
}
function printpreview(){
// Print page preview
wb.execwb(7,1);
}
function printit()
{
if(confirm(' Are you sure you want to print? '))
{
wb.execwb(6,6)
}
}
</script>
</head>
<body>
<p class="noprint">
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" id="wb" name="wb" width="0"></OBJECT>
<input type="button" name="button_print" value=" Print " onclick="javascript:printit()" />
<input type="button" name="button_setup" value=" Print page setup " onclick="javascript:printsetup();" />
<input type="button" name="button_show" value=" Print preview " onclick="javascript:printpreview();" />
</p>
</body>
3. JS realizes simple local printing of pages
function preview(oper)
{
if (oper < 10){
bdhtml=window.document.body.innerHTML;// Object for the current page html Code
sprnstr="<!--startprint"+oper+"-->";// Set the print start area
eprnstr="<!--endprint"+oper+"-->";// Set the end of printing area
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); // Backward from the start code htm
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));// Fetch forward from the end code html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
}
else {
window.print();
}
}
It is very simple to add the content to be printed in the middle of the page
<
!--startprint1--
>
XXXXX
<
!--endprint1--
>
Add a print button onclick=preview (1)
4. Control the margins of "vertical typing" and "horizontal typing" pages.
(1)
<
script defer
>
function SetPrintSettings() {
// -- advanced features
factory.printing.SetMarginMeasure(2) // measure margins in inches
factory.SetPageRange(false, 1, 3) // need pages from 1 to 3
factory.printing.printer = "HP DeskJet 870C"
factory.printing.copies = 2
factory.printing.collate = true
factory.printing.paperSize = "A4"
factory.printing.paperSource = "Manual feed"
// -- basic features
factory.printing.header = "This is MeadCo"
factory.printing.footer = "Advanced Printing by ScriptX"
factory.printing.portrait = false
factory.printing.leftMargin = 1.0
factory.printing.topMargin = 1.0
factory.printing.rightMargin = 1.0
factory.printing.bottomMargin = 1.0
}
</script>
function preview(oper)
{
if (oper < 10){
bdhtml=window.document.body.innerHTML;// Object for the current page html Code
sprnstr="<!--startprint"+oper+"-->";// Set the print start area
eprnstr="<!--endprint"+oper+"-->";// Set the end of printing area
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); // Backward from the start code htm
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));// Fetch forward from the end code html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
}
else {
window.print();
}
}
It is very simple to add the content to be printed in the middle of the page
<
!--startprint1--
>
XXXXX
<
!--endprint1--
>
Add a print button onclick=preview (1)
For more information about js printing function, click on the topic "Summary of js Printing Function" to learn