将水印添加到Excel文件 [英] Adding watermark to excel file
问题描述
我有一个应用程序,该应用程序从excel文件中的工作表中读取数据,然后在同一文件的另一工作表中打印出来.
I have an application that reads data from a sheet in excel file prints the out in another sheet in the same file.
为了我自己的满意,我想在显示输出的那张纸上加一个水印.我正在使用C#和.NET
For my own satisfaction I want to add a watermark to that sheet where the output is displayed. I am working with C# and .NET
我需要粘贴任何特定的代码吗?我不确定您需要什么.请询问您是否需要更多详细信息
Do I need to paste any specific code? I am not sure what would you need. Please ask if you require more details
用于处理excel对象的库-:
Library used for handling excel objects -:
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
//creating an object of Application
Excel.Application excelApp = new Excel.Application();
//creating an object of Workbook
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path, 0,false,
5, "", "", false, Excel.XlPlatform.xlWindows,
"", true, false, 0, true, false, false);
//creating an object of Sheet
Excel.Sheets excelSheets = (Excel.Sheets)excelWorkbook.Sheets;
推荐答案
我一直在解决此问题,并且由于找不到解决方案,因此我正在发布自己的工作代码.该解决方案通过创建临时图像并将其插入幻灯片来工作.创建代码的步骤基于此: http://smallbusiness.chron.com/put-watermarks-photos-word-45076.html 链接.
I've been working on this issue and since I didn't find a solution anywhere I'm posting my working code. This solution works by creating temp image and inserting it on slides. Steps to create code are based on this: http://smallbusiness.chron.com/put-watermarks-photos-word-45076.html link.
public void AddWaterMarkToPowerPoint(string filePath)
{
string waterMarkText = "Top secret";
PowerPoint.Application powerPointApp = new PowerPoint.Application();
PowerPoint.Presentations pres = powerPointApp.Presentations;
string imagePath = null;
try
{
PowerPoint.Presentation pptPresentation = pres.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
PowerPoint.PageSetup pageSetup = pptPresentation.PageSetup;
float pageWidth = pageSetup.SlideWidth;
float pageHeight = pageSetup.SlideHeight;
CreateTempWaterMarkImage(waterMarkText, ref imagePath, (int)pageHeight, (int)pageWidth);
for (int i = 1; i <= pptPresentation.Slides.Count; i++)
{
PowerPoint.Slide slide = pptPresentation.Slides[i];
PowerPoint.Shapes shapes = slide.Shapes;
PowerPoint.Shape shape = shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 0, 0, pageWidth, pageHeight);
shape.Fill.UserPicture(imagePath);
shape.Fill.Transparency = 0.7f;
shape.Line.Transparency = 1;
shape.ZOrder(MsoZOrderCmd.msoBringToFront);
}
pptPresentation.SaveAs(filePath);
pptPresentation.Close();
}
catch (Exception ex)
{
//log exception
throw;
}
finally
{
// Cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
powerPointApp.Quit();
//remove temp image
if (imagePath != null)
File.Delete(imagePath);
}
}
private void CreateTempWaterMarkImage(string waterMarkText, ref string imagePath, int pageHeight, int pageWidth)
{
float angleRotation = (float)((Math.Atan2((double)pageHeight, (double)pageWidth) * 180) / Math.PI);
float fontSize = (float)Math.Sqrt(Math.Pow(pageHeight, 2) + Math.Pow(pageWidth, 2)) / 50;
using (Bitmap newie = new Bitmap(pageWidth, pageHeight))
{
using (Graphics gr = Graphics.FromImage(newie))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.TranslateTransform((float)pageWidth / 2f, (float)pageHeight / 2f);
gr.RotateTransform(-angleRotation);
Font font = new Font("Arial", fontSize, FontStyle.Regular);
SizeF textSize = gr.MeasureString(companyName, font);
gr.DrawString(waterMarkText, font, SystemBrushes.GrayText, -textSize.Width, -textSize.Height);
}
string fileName = Path.GetRandomFileName();
imagePath = Path.GetTempPath() + @"\" + fileName + ".png";
newie.Save(imagePath, ImageFormat.Png);
}
}
这篇关于将水印添加到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!