简体   繁体   English

ASP.net中的Javascript画布

[英]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. 我正在尝试在Visual Studio应用程序的画布上绘制图像,然后在单击按钮后将该图像显示在PDF文件上。 I'm using javascript and C#. 我正在使用javascript和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. 到目前为止,我创建了一个名为“ A”的网页,并在.aspx页面中创建了一个画布和按钮,但是我不确定如何在.aspx.cs页面中获得此图像(在其中显示“ Image Here ”)。 ),以便可以将其写入PDF文件。 I'm pretty sure my code currently puts my canvas image in base 64. Any help is greatly appreciated. 我很确定我的代码当前将我的画布图像放在base 64中。我们将不胜感激。 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. 我做了一个jsFiddle ,其中包含您需要做的基本工作。 Since you are using Asp.Net, your whole page is in a form. 由于您使用的是Asp.Net,因此您的整个页面都采用表格形式。 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. 当您单击提交按钮时,它应该将画布中的数据发布到隐藏字段中,并且您可以在页面加载中从imageData中获取数据。

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. 我不确定在尝试将数据保存到PDF之前是否必须进行数据转换,但这应该为您提供一个良好的开端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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