Print Div. Content from a page with print preview using JavaScript in MVC

As the previous article, we have learned how to embed Google translator in MVC. Now we learn how to open print div from a page using JavaScript in MVC.  


View:-
@{
    Layout = null;
}

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title></title>
    <script type="text/javascript">
        function printmethod() {
            var contents = document.getElementById("divprint").innerHTML;
            var frame1 = document.createElement('iframe');
            frame1.name = "frame1";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head><title>neerajcodesolution.com</title>');
            frameDoc.document.write('</head><body>');
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();

                document.body.removeChild(frame1);
            }, 500);
            return false;
        }
    </script>
</head>
<body>
    <form id="form1">
 
        <div id="divprint">
            <span>
                Hi Friends ,
                <br />
                neerajcodesolutions.com refer to improve your code skills.<br />
                you can also learn many theoretical questions that will help you in interview time.<br />
                we also provide source code with all articles. please suggest me any improvement if you want
                <br />
                <br />
                Thanks<br />
                neerajcodesolutions.com
            </span>
        </div>
        <br />
        <input type="button" onclick="printmethod();" value="Print" />
    </form>
</body>

</html>

Output:-



Print Div. Content from a page with print preview using JavaScript in MVC Print Div. Content from a page with print preview using JavaScript in MVC Reviewed by NEERAJ SRIVASTAVA on 6:25:00 PM Rating: 5

No comments:

Powered by Blogger.