locked
How to print iframe src as pdf using itextsharp in c# RRS feed

  • Question

  • User-1662430237 posted

    I need to print classic asp page as pdf using itextsharp. I used iframe to load classic asp page in aspx and now I want to print that iframe as pdf. So please help.

    Thanks.

    Friday, March 22, 2019 4:46 AM

All replies

  • User-893317190 posted

    Hi Pradeep Buddika,

    Not sure about itextsharp, but if you only want to print iframe , you could finish it in client side. Window object has a print method that could print the page as pdf.

    Below is my test code.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
          <iframe src="../Default.aspx" id="myframe" height="200"></iframe>
        </form>
        <input id="Button1" type="button" value="pring iframe" />
    </body>
        <script>
            window.onload = function () {
                document.getElementById("Button1").onclick = function () { // when clicking the button, print the iframe
    
                    document.getElementById("myframe").contentWindow.print();
                }   
            }
        </script>
    </html>

    The result.

    Best regards,

    Ackerly Xu

    Friday, March 22, 2019 6:46 AM
  • User-1662430237 posted

    Thank you very much Mr. Ackerly.

    Can we do this using internet explorer 11? Since most people use internet explorer in my Office.

    Friday, March 22, 2019 8:31 AM
  • User-893317190 posted

    Hi Pradeep Buddika,

    ie doesn't work , but I have a workaround, you could open a new window and print the new window.

    But in this way, it will open a new window.

     <script>
            window.onload = function () {
                document.getElementById("Button1").onclick = function () {
                    anotherwindow = window.open(document.getElementById("myframe").src,"abc");
                    
                   
                    anotherwindow.print();
                 
                }   
            }
        </script>

    Best regards,

    Ackerly Xu

    Friday, March 22, 2019 9:55 AM
  • User-1662430237 posted

    Thanks. Mr. Ackerly.

    My issue is still pending.

    Can we convert classic asp page (.asp) into pdf?

    Friday, March 22, 2019 11:16 AM
  • User-893317190 posted

    Hi Pradeep Buddika,

    Since your asp has been rendered in browser in an iframe , you only need to print the html in iframe.

    Have you tried to print it ? I am not sure what issue you have met.

    If you want to use itextsharp , then you shouldn't use iframe , you should use httpClient to get html string from server and then convert it using itextsharp.

    https://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp

      HttpClient client = new HttpClient();
               HttpResponseMessage message = client.GetAsync("http://localhost:53239/Default").Result ; //please the http address of your asp page
               string result = message.Content.ReadAsStringAsync().Result;

    Best regards,

    Ackerly Xu

    Monday, March 25, 2019 2:05 AM