要在.NET Core中添加多行多列的水印,可以使用OpenXml库来在PPT中插入文本框(可以用于水印)。
首先,确保已经在项目中引用了DocumentFormat.OpenXml和DocumentFormat.OpenXml.Presentation这两个NuGet包。
然后,可以使用以下代码示例在PPT中添加多行多列的水印:
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;
public class PptWatermark
{
public static void AddWatermark(string pptFilePath, string watermarkText)
{
using (PresentationDocument presentationDocument = PresentationDocument.Open(pptFilePath, true))
{
PresentationPart presentationPart = presentationDocument.PresentationPart;
SlidePart slidePart = presentationPart.SlideParts.First();
// 创建一个新的形状,并将其添加到幻灯片的列表中
Shape shape = CreateWatermarkShape(watermarkText);
slidePart.Slide.CommonSlideData.ShapeTree.AppendChild(shape);
presentationPart.Presentation.Save();
}
}
private static Shape CreateWatermarkShape(string watermarkText)
{
Shape shape = new Shape();
NonVisualShapeProperties nvShapeProperties = new NonVisualShapeProperties();
NonVisualDrawingProperties nvDrawingProperties = new NonVisualDrawingProperties()
{
Id = 1, Name = ""
};
ShapeNonVisualProperties shapeNonVisualProperties = new ShapeNonVisualProperties(nvDrawingProperties);
nvShapeProperties.Append(shapeNonVisualProperties);
ShapeProperties shapeProperties = new ShapeProperties();
A.Transform2D transform = new A.Transform2D();
A.Offset offset = new A.Offset() { X = 0, Y = 0 };
transform.Append(offset);
A.TextBody textBody = new A.TextBody();
A.BodyProperties bodyProperties = new A.BodyProperties();
A.ListStyle listStyle = new A.ListStyle();
A.Paragraph paragraph = new A.Paragraph();
A.TextRun textRun = new A.TextRun();
A.RunProperties runProperties = new A.RunProperties() { Language = "en-US" };
A.Text text = new A.Text(watermarkText);
textRun.Append(runProperties);
textRun.Append(text);
paragraph.Append(textRun);
textBody.Append(bodyProperties);
textBody.Append(listStyle);
textBody.Append(paragraph);
shapeProperties.Append(transform);
shapeProperties.Append(textBody);
shape.Append(nvShapeProperties);
shape.Append(shapeProperties);
return shape;
}
}
然后,你可以调用AddWatermark方法来将水印添加到PPT中:
string pptFilePath = "YourPptFilePath.pptx";
string watermarkText = "Your Watermark Text";
PptWatermark.AddWatermark(pptFilePath, watermarkText);
请注意,这个示例中,水印文本添加到幻灯片的左上角,你可以根据需要修改offset的值来调整水印的位置。