IText&Html2canvas js截图 绘制 导出PDF
2015-06-26 10:30
627 查看
Html2canvas JS截图
HTML
JS
后台使用图片 Base64编码转换为图像
给PDF文件添加水印 IText WaterMark
HTML
<div id="divPDF"> 需要截图的区域 </div>
JS
<script src="../Js/html2canvas.js"></script> <script type="text/javascript"> function getPDF() { html2canvas($('#divPDF'), { onrendered: function (canvas) { var imgUrl = canvas.toDataURL();//获取截图的Base64编码 } }); } </script>
后台使用图片 Base64编码转换为图像
// <summary> /// Base64编码转换为图像 /// </summary> /// <param name="base64String">Base64字符串</param> /// <returns>转换成功返回图像;失败返回null</returns> public string Base64ToImage(string imgName, string base64String, string path) { base64String = base64String.Replace("data:image/png;base64,", ""); MemoryStream ms = null; System.Drawing.Image image = null; string imgUrl = path + "\\" + imgName + ".png"; byte[] imageBytes = Convert.FromBase64String(base64String); ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); image = System.Drawing.Image.FromStream(ms, true); image.Save(imgUrl); return imgUrl; }
给PDF文件添加水印 IText WaterMark
public void AddWaterMark(string fileLocation, string path, int x, int y) { string WatermarkLocation = path + "\\watermark.png"; Document document = new Document(); PdfReader pdfReader = new PdfReader(fileLocation); PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(fileLocation.Replace(".pdf", "[temp][file].pdf"), FileMode.Create)); iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation); 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) PdfContentByte waterMark; for (int page = 1; page <= pdfReader.NumberOfPages; page++) { waterMark = stamp.GetOverContent(page); waterMark.AddImage(img); } stamp.FormFlattening = true; stamp.Close(); pdfReader.Close(); // now delete the original file and rename the temp file to the original file File.Delete(fileLocation); File.Move(fileLocation.Replace(".pdf", "[temp][file].pdf"), fileLocation); }
相关文章推荐
- [LeetCode][JavaScript]Summary Ranges
- Objc与JS间相互调用
- jsp 页面中 判断session是否失效
- jstree 取消选中父节点
- 用gson处理json时bean和json串的对应关系
- javascript变量初始化位置
- javascript实现倒计时(精确到秒)
- js计算2个日期之间相差天数
- href=“#”与href="javascript:void(0)" 的区别
- Extjs sencha cmd打包压缩部署 前台代码压缩
- 详解JavaScript立即执行函数表达式
- arguments对象验证函数的参数是否合法
- Why we made vorlon.js and how to use it to debug your JavaScript remotely
- JavaScript + CSS3 实现的海报画廊特效
- javascript规范
- Truthy Vs Falsy Values in JavaScript
- 02 js鼠标点击事件在各个浏览器中的写法及Event对象属性介绍
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解
- js判断是否为数组的函数: isArray()
- 自动适应屏幕宽度