• IText&Html2canvas js截图 绘制 导出PDF


    Html2canvas JS截图

    HTML

    1 <div  id="divPDF"> 
    2 需要截图的区域
    3 </div>

    JS

     1 <script src="../Js/html2canvas.js"></script>
     2 <script type="text/javascript">
     3 
     4         function getPDF() {
     5              html2canvas($('#divPDF'),
     6              {
     7                  onrendered: function (canvas) {
     8                      var imgUrl = canvas.toDataURL();//获取截图的Base64编码
     9                  }
    10              });
    11          }
    12 
    13 </script>

     后台使用图片 Base64编码转换为图像

     1         // <summary>
     2         /// Base64编码转换为图像
     3         /// </summary>
     4         /// <param name="base64String">Base64字符串</param>
     5         /// <returns>转换成功返回图像;失败返回null</returns>
     6         public string Base64ToImage(string imgName, string base64String, string path)
     7         {
     8             base64String = base64String.Replace("data:image/png;base64,", "");
     9             MemoryStream ms = null;
    10             System.Drawing.Image image = null;
    11             string imgUrl = path + "\" + imgName + ".png";
    12             byte[] imageBytes = Convert.FromBase64String(base64String);
    13             ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
    14             ms.Write(imageBytes, 0, imageBytes.Length);
    15             image = System.Drawing.Image.FromStream(ms, true);
    16             image.Save(imgUrl);
    17             return imgUrl;
    18         }

    给PDF文件添加水印 IText  WaterMark

     1         public void AddWaterMark(string fileLocation, string path, int x, int y)
     2         {
     3             string WatermarkLocation = path + "\watermark.png";
     4             Document document = new Document();
     5             PdfReader pdfReader = new PdfReader(fileLocation);
     6             PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(fileLocation.Replace(".pdf", "[temp][file].pdf"), FileMode.Create));
     7 
     8             iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
     9             img.SetAbsolutePosition(x, y); // set the position in the document where you want the watermark to appear (0,0 = bottom left corner of the page)
    10             PdfContentByte waterMark;
    11             for (int page = 1; page <= pdfReader.NumberOfPages; page++)
    12             {  
    13                 waterMark = stamp.GetOverContent(page);
    14                 waterMark.AddImage(img);
    15             }
    16             stamp.FormFlattening = true;
    17             stamp.Close();
    18             pdfReader.Close();
    19             // now delete the original file and rename the temp file to the original file
    20             File.Delete(fileLocation);
    21             File.Move(fileLocation.Replace(".pdf", "[temp][file].pdf"), fileLocation);
    22 
    23         }
  • 相关阅读:
    互联网史上第一约架 罗永浩对话王自如
    The Open Group Announces Launch of the TOGAF® Standard, 10th Edition
    从零开始学PHP系列(0.1)语法简记
    QtQt使用鼠标钩子Hook(支持判断按下、弹起、滚轮方向)
    QtQPropertyAnimation的使用(支持放大、移动、透明动画)
    OsgOsgEarth加载在线地图(myMap.earth)
    关于UE4对象静态/动态的销毁问题整理(AddToRoot、TWeakObjectPtr)
    cesanta elk安装及测试
    RocketMQ的原理和实战!
    LINQ:GroupBy
  • 原文地址:https://www.cnblogs.com/IT-Bear/p/4601711.html
Copyright © 2020-2023  润新知