0

I am using the buffer to write the pdf file to the page in asp.net, I wonder its overwriting the html DOM elements. Can anyone help me out how can i render the pdf on the page. Even i tried using the Web user control. It does but shows the scroll bar at the side.

My target is to render the pdf file in the page and fire the window.print() function in javascript.

Please have a look at the given code below.

string path = Session["fullname"].ToString();                        
                    System.Net.WebClient client = new System.Net.WebClient();
                    Byte[] buffer = client.DownloadData(path);

            if (buffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
                Response.Flush();

            }
            if ((System.IO.File.Exists(path)))
            {
                System.IO.File.Delete(path);

            }                             

Thanks in Advanced.

trytaher
  • 1
  • 5
  • What exactly do you want to achieve ? –  Oct 20 '14 at 07:45
  • If you want to show the pdf on page then you can use the iframe to show the pdf on page and then fire the javascript event from the client side. Here is [link](http://stackoverflow.com/questions/11603277/display-pdf-in-iframe) –  Oct 20 '14 at 07:46
  • I want to display pdf in a page or popup once the pdf is rendered with above code then instantly it must fire print dialog as we press Ctrl+P for the print. These things should happen when i click on a single button click. i.e print. – trytaher Oct 20 '14 at 09:11
  • I already used IFrame but it displays scroll bar in the printing page. Please let me know if there is any alternate option to avoid the scroll bar. – trytaher Oct 20 '14 at 09:12
  • Do you want to keep the iFrame open and also perform a print operation? If the iFrame contents are bigger than the iFrame then how will the user view the full contents?Now, if you only want to print the report, then that's a different story. It is a bit unclear what you are trying to achieve. – Chris Oct 20 '14 at 10:43
  • @Chris is right what if the contents are bigger than the iframe ? And if you dont want to show the content to the user then you can directly print the document without showing it up on the screen –  Oct 20 '14 at 13:11
  • Yes Chris, The user need not have to see the file, he will directly take a print of it. – trytaher Oct 20 '14 at 13:21

1 Answers1

0

You cannot display PDF content within an HTML page unless you have a plugin installed on the client. The alternative is to convert the PDF file into individual images in JPEG format for example and return them to the browser. See also Embed Pdf in html5

Copy and paste the following code within the head tags:

<script language="Javascript1.2">
  <!--
  function printpage() {
  window.print();
  }
  //-->
</script>

Add onload="printpage()" to body tag:

<body onload="printpage()">
Community
  • 1
  • 1
Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Hi Tarik, No matter wheather its image or PDF. It renders on the page. But my target is to fire the window.print once its rendered. – trytaher Oct 20 '14 at 09:16