Word,Excel插入密集水印和二维码
2018-06-22 07:41:11来源:未知 阅读 ()
最近客户要求开发一个功能:
在系统中上传文件后,自动个文档每页加上密集水印和二维码,打印纸质文档后扫描二维码自动打开系统电子档,防止其他人随意篡改。
简单做了一个DEMO程序:
程序截图
最终效果图:
二维码生成用的:ThoughtWorks.QRCode
操作文档用的:Aspose
大致逻辑:首先生成好二维码图片,打开文档在文档页眉处追加二维码图片,同时在文档每一页追加水印(这个地方客户要求插入密集型多个水印,我看了标准微软Word操作,一次只能插入一个,这块实现我用了个勺方法,多次调用插入水印方法具体看后面代码)
/************************************Start***************************插入水印,二维码到Word逻辑********************************Start***********************************/
生成二维码代码段:
//生成二维码
protected string CreateQRCODE(string Codestring, string m_savefilepath, int QRCodeScale, int QRCodeVersion)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = QRCodeScale;
qrCodeEncoder.QRCodeVersion = QRCodeVersion;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Image image = qrCodeEncoder.Encode(Codestring, Encoding.UTF8);
string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
string filepath = m_savefilepath + filename;
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose();
if (QRCodeScale >= 3)
{
//二维码解码
var codeDecoder = CodeDecoder(filepath);
}
return filepath;
}
将二维码追加到文档代码段:
/// <summary>
/// 给word添加页眉页脚并将图片插入到页眉页脚中
/// </summary>
/// <param name="doc">目标word路径</param>
/// <param name="FilePath">保存后文档路径</param>
/// <param name="imgPath">图片路径</param>
protected string SetImageHeadOrFootDoc(Aspose.Words.Document doc,string FilePath,string imgPath,string Codestring)
{
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.CurrentSection;
Aspose.Words.PageSetup pageSetup = currentSection.PageSetup;
pageSetup.HeaderDistance = 20;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
// 创建其他页的标题
pageSetup.HeaderDistance = 20;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
//将绝对位置的图像插入头/左上角。
//从顶部/左边缘的页面的距离被设置为10点。
builder.InsertImage(imgPath, RelativeHorizontalPosition.Page, 280, RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through);
//创建页面以外的页面。
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
doc.Save(FilePath);
return FilePath;
}
将水印追加到文档中:
/// <summary>
/// 插入水印到Word文档
/// </summary>
/// <param name="doc">word文档路径</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="RH">水印相对水平位置</param>
/// <param name="RV">水印相对垂直位置</param>
/// <param name="VA">水印水平位置</param>
/// <param name="HA">水印垂直位置</param>
private static void InsertWatermarkTextToWord(Aspose.Words.Document doc, string watermarkText,RelativeHorizontalPosition RH,RelativeVerticalPosition RV,Aspose.Words.Drawing.VerticalAlignment VA,Aspose.Words.Drawing.HorizontalAlignment HA)
{
// 实例化一个水印图片
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, ShapeType.TextPlainText);
//设置水印文字的样式
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = "Arial";
watermark.Width = 200;
watermark.Height = 100;
// 水印的方向设置
watermark.Rotation = -20;
// 水印线条颜色填充
watermark.Fill.Color = Color.Gray ;
watermark.StrokeColor = Color.Gray ;
//设置水印图片放置在每页的正中间
watermark.RelativeHorizontalPosition = RH;
watermark.RelativeVerticalPosition = RV;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VA;
watermark.HorizontalAlignment = HA;
// 初始化一个段落,将水印追加到段落中
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark);
// 循环每页,将水印插入到文档每页中
foreach (Section sect in doc.Sections)
{
// 插入水印到文档每页
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}
}
/// <summary>
/// 插入水印到文档头部分
/// </summary>
/// <param name="watermarkPara">水印</param>
/// <param name="sect">文档区域</param>
/// <param name="headerType">文档头类型</param>
private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
//如果没有文档头,或者找不到头部分,自动创建一个新的文档头部分。
header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
//复制水印,并追加到文档头部分
header.AppendChild(watermarkPara.Clone(true));
}
密集型水印,多个水印的插入
//插入二维码水印到word(方法重复调用,每次插入位置不一样就可以实现了)
Aspose.Words.Document doc = new Aspose.Words.Document(DocPath);
InsertWatermarkTextToWord(doc, textFontContent.Text, RelativeHorizontalPosition.Page, RelativeVerticalPosition.Page, Aspose.Words.Drawing.VerticalAlignment.Center, Aspose.Words.Drawing.HorizontalAlignment.Left);
InsertWatermarkTextToWord(doc, textFontContent.Text, RelativeHorizontalPosition.Page, RelativeVerticalPosition.Page, Aspose.Words.Drawing.VerticalAlignment.Bottom, Aspose.Words.Drawing.HorizontalAlignment.Right);
InsertWatermarkTextToWord(doc, textFontContent.Text, RelativeHorizontalPosition.Page, RelativeVerticalPosition.Page, Aspose.Words.Drawing.VerticalAlignment.Bottom, Aspose.Words.Drawing.HorizontalAlignment.Left);
InsertWatermarkTextToWord(doc, textFontContent.Text, RelativeHorizontalPosition.Page, RelativeVerticalPosition.Page, Aspose.Words.Drawing.VerticalAlignment.Center, Aspose.Words.Drawing.HorizontalAlignment.Right);
/****************************************END***********************插入水印,二维码到Word逻辑***************************END****************************************/
/****************************************Start***********************插入水印,二维码到Excel逻辑***************************Start****************************************/
/// <summary>
/// 插入水印,二维码到excel文档
/// </summary>
/// <param name="FileName">目标文档路径</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="FilePath">保存后文档路径</param>
protected string InsertWatermarkIntoAndQRCodeToWorkBook(string FileName, string watermarkText,string FilePath,string imgPath)
{
//初始化工作簿
Workbook workbook = new Workbook(FileName);
//获取工作簿表集合
WorksheetCollection Wsheetc = workbook.Worksheets;
foreach (Worksheet sheet in Wsheetc) {
//插入水印
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 1, 1, 1, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 1, 1, 10, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 1, 1, 18, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 14, 1, 1, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 14, 1, 10, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 14, 1, 18, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 28, 1, 1, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 28, 1, 10, 1, 100, 100);
InsertWaterMarkToXLS(workbook, sheet, watermarkText, 28, 1, 18, 1, 100, 100);
//插入二维码
InsertQrCodeToXls(workbook, imgPath, sheet);
}
//保存excel文档
workbook.Save(FilePath);
return FilePath;
}
/// <summary>
/// 插入二维码到XLS页眉页脚
/// </summary>
/// <param name="workbook">目标XLS文档</param>
/// <param name="imgPath">二维码图片路径</param>
/// <param name="sheet">工作表</param>
protected void InsertQrCodeToXls(Workbook workbook, string imgPath, Worksheet sheet)
{
//添加页眉,并在页眉中添加图片
FileStream inFile;
byte[] binaryData;
inFile = new System.IO.FileStream(imgPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
Aspose.Cells.PageSetup pageSetup = sheet.PageSetup;
pageSetup.SetHeaderPicture(1, binaryData);
pageSetup.SetHeader(1, "&G");
pageSetup.SetHeader(2, "&A");
}
/// <summary>
/// 插入水印到XlS
/// </summary>
/// <param name="workbook">目标Excel文档</param>
/// <param name="sheet">目标工作簿</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="upperLeftRow"></param>
/// <param name="top">距离顶部距离</param>
/// <param name="upperLeftColumn"></param>
/// <param name="left">距离左边距离</param>
/// <param name="height">水印高度</param>
/// <param name="width">水印宽度</param>
protected void InsertWaterMarkToXLS(Workbook workbook, Worksheet sheet, string watermarkText, int upperLeftRow, int top, int upperLeftColumn, int left, int height, int width)
{
//添加水印
Aspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect2, watermarkText, "Arial Black", 50, false, true, upperLeftRow, top, upperLeftColumn, left, height, width);
//用艺术字体填充
MsoFillFormat wordArtFormat = wordart.FillFormat;
//设置颜色
wordArtFormat.ForeColor = System.Drawing.Color.Gray;
//设置透明度
wordArtFormat.Transparency = 0.5;
//设置线条不可见
MsoLineFormat lineFormat = wordart.LineFormat;
lineFormat.IsVisible = false;
}
/****************************************END***********************插入水印,二维码到Excel逻辑***************************END****************************************/
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- asp.net实现导出DataTable数据到Word或者Excel的方法 2020-02-28
- Asp.net中将Word文件转换成HTML的方法 2019-12-19
- GridView导出Excel的实现案例 2019-09-17
- ASP.NET中生成Excel遇到的问题及改进方法 2019-09-08
- asp.net下将页面内容导入到word模板中的方法 2019-09-08
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash