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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM