IText&Html2canvas js截图 绘制 导出PDF

本文详细介绍了如何利用HTML和JavaScript结合html2canvasJS库实现网页截图,并通过C#将Base64编码转换为图像,最后在PDF文件上添加水印的技术流程。包括截图代码实现、Base64编码转换图像方法及PDF文件水印添加步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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         }

 

转载于:https://www.cnblogs.com/IT-Bear/p/4601711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值