您的位置:首页 > Web前端 > JavaScript

IText&Html2canvas js截图 绘制 导出PDF

2015-06-26 10:30 627 查看
Html2canvas JS截图

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: