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         }

 

posted @ 2015-06-26 10:30  kumat  阅读(2326)  评论(0编辑  收藏  举报