程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Word,Excel插入密集水印和二維碼,wordexcel水印

Word,Excel插入密集水印和二維碼,wordexcel水印

編輯:關於.NET

Word,Excel插入密集水印和二維碼,wordexcel水印


最近客戶要求開發一個功能:

                 在系統中上傳文件後,自動個文檔每頁加上密集水印和二維碼,打印紙質文檔後掃描二維碼自動打開系統電子檔,防止其他人隨意篡改。

 

簡單做了一個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****************************************/

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved