简体   繁体   中英

Javascript Canvas in ASP.net

I'm trying to draw an image on a canvas in a Visual Studio application and then have that image get displayed on a PDF file after clicking a button. I'm using javascript and C#. So far I created a Web Page called “A” and in the .aspx page I created a canvas and button, but I'm unsure how I can get this image in the .aspx.cs page (where it says “ Image Here ”) so it can write to the PDF file. I'm pretty sure my code currently puts my canvas image in base 64. Any help is greatly appreciated. Thanks!

//A.aspx:
//Create canvas and save as base 64
<head>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#saveButton').click(saveDrawing);
        });
    </script>  
</head>
<canvas id ="canvas1" class="pad" width="198" height="55"></canvas>
  <input type="button" name="saveButton" id="saveButton" value="Save Drawing">
  <script type="text/javascript">
      function saveDrawing() {
          var canvas = document.getElementById("canvas1");
          var context = canvas.getContext("2d");
          var imgData = canvas.toDataURL();
          //window.open(imgData);  //Not sure if I need this or not
      }      

//A.aspx.cs: Code Behind
public void Method(string A)
{
    PdfStamper pdfStamper = new PdfStamper(
        pdfReader, 
        new FileStream(newFile, FileMode.Create)
    );
    AcroFields pdfForm = pdfStamper.AcroFields;
    //This is where I need to get the image from A.aspx
    pdfForm.SetField("Picture is", (**Image Here**)); 
}

I made a jsFiddle with the basics of what you need to do. Since you are using Asp.Net, your whole page is in a form. When you click the submit button it should post the data from the canvas in a hidden field and you can grab the data from imageData in the page load.

I am not sure if you will have to do any conversion of the data before you try to save it to the PDF but this should give you a good start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM