Aspose给excel,word,pdf加图片水印

 Aspose给excel,word,pdf加图片水印,水印图片不是透明的,如何将图片置于文字的下面从而不会盖住文字

根据使用spire.office.dll来加水印的方法来看,给word,pdf, ppt加水印的时候不用特别设置图片位置也不会遮挡文字的,给Excel加水印的话是通过在页眉中添加图片来实现,下面列举了如何添加图片水印的方法,可以自行参考下:

word图片水印:

//新建一个word文档对象并加载需要添加水印的word文档
Document document = new Document();
document.LoadFromFile("Test.docx");


//新建一个图片水印对象并添加为待设置水印的图片

PictureWatermark picture = new PictureWatermark();
picture.Picture = System.Drawing.Image.FromFile("logo.png");

//根据需要设置图片的大小,将其设置为文档的水印
picture.Scaling = 10;
document.Watermark = picture;

//保存文档
document.SaveToFile("图片水印.docx",FileFormat.Docx2010);

excel图片水印:

using Spire.Xls;
using System.Drawing;
using System;

namespace AddWaterMarktoExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载示例文档
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            //设置文本和字体大小
            Font font = new System.Drawing.Font("仿宋", 40);
            String watermark = "内部文档";         
 
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                //调用DrawText() 方法插入图片
                System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth);
                //将图片设置为页眉
                sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
                sheet.PageSetup.LeftHeader = "&G";
                //将显示模式设置为Layout
                sheet.ViewMode = ViewMode.Layout;
            }
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");
        }

        private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width)
        {
            //定义图片宽度和告诉
            Image img = new Bitmap((int)width, (int)height);
            Graphics drawing = Graphics.FromImage(img);
            //获取文本size
            SizeF textSize = drawing.MeasureString(text, font);
            //文本显示样式及位置
            drawing.TranslateTransform(((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
            drawing.RotateTransform(-45);
            drawing.TranslateTransform(-((int)width - textSize.Width) / 2, -((int)height - textSize.Height) / 2);
            drawing.Clear(backColor);
            Brush textBrush = new SolidBrush(textColor);
            drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
            drawing.Save();
            return img;
        }
    }
}

PDF图片水印:

//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Spire.Presentation.pdf");

//获取PDF文档的第一页
PdfPageBase page = pdf.Pages[0];

//获取图片并将其设置为页面的背景图
Image img = Image.FromFile("Logo.png");
page.BackgroundImage = img;

//指定背景图的位置和大小
page.BackgroundRegion = new RectangleF(200, 200, 200, 200);

//保存文档
pdf.SaveToFile("ImageWaterMark.pdf");

PPT图片水印:

//加载PPT文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010);


//为第一张幻灯片设置背景图片类型和样式
ppt.Slides[0].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[0].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;


//获取图片并将其设置为页面的背景图
Image img = Image.FromFile("Logo.png");
IImageData image = ppt.Images.Append(img);
ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

//保存文档
ppt.SaveToFile("ImageWatermark.pptx", FileFormat.Pptx2010);

代码内容参考自spire教程。