在powerpoint interop c#中创建水印

本文关键字:创建 powerpoint interop | 更新日期: 2023-09-27 18:30:01

我一直在尝试在powerpoint中创建水印我下面有一个代码,我可以在其中添加图片,现在我如何为图片创建透明度,使其看起来像水印

private void watermark_Click(object sender, RibbonControlEventArgs e)
{
    PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
    PowerPoint.SlideRange ppslr = ppApp.ActiveWindow.Selection.SlideRange;
    //ppApp.ActivePresentation.Slides.InsertFromFile("NepaSlide.pptx",2, 1,1);
    //PowerPoint.ShapeRange ppShR = ppApp.ActiveWindow.Selection.ShapeRange;
    int count= ppslr.Shapes.Count;
    PowerPoint.Shape shape = ppslr.Shapes[count];
    ppslr.Shapes.AddPicture("N-symbol.png",
            Microsoft.Office.Core.MsoTriState.msoFalse,
            Microsoft.Office.Core.MsoTriState.msoTrue,
            shape.Left, shape.Top, shape.Width, shape.Height);     
}

在powerpoint interop c#中创建水印

我知道这是一个老问题,但我没有找到解决方案,所以我自己写了代码。

public void AddWaterMarkToPowerPoint(string filePath)
        {
            string waterMarkText = "Top secret";
            PowerPoint.Application ppApp = new PowerPoint.Application();
            PowerPoint.Presentations pres = ppApp.Presentations;

            PowerPoint.Presentation pptPresentation = pres.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

            for (int i = 1; i <= pptPresentation.Slides.Count; i++)
            {
                var test = pptPresentation.Slides[i].CustomLayout.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 200, 200, 600, 100);
                test.TextFrame.TextRange.Text = waterMarkText;
                test.Rotation = -45;
                test.TextFrame.TextRange.Font.Color.RGB = Color.LightGray.ToArgb();
                test.TextFrame.TextRange.Font.Size = 48;
            }
            pptPresentation.SaveAs(filePath);
            pptPresentation.Close();

        }

此代码为演示文稿中的每张幻灯片添加文本。PowerPoint没有添加水印的能力,所以我们必须通过添加浅灰色文本来制作水印。