Это решение Я попытался добавить водяной знак в Word.
Код: Выделить всё
public class Watermark
{
private Application wordApp;
private Document doc;
public Watermark(Application wordApp, Document doc)
{
this.wordApp = wordApp;
this.doc = doc;
}
public void AddCenteredTextWatermark(string watermarkText)
{
foreach (Section section in doc.Sections)
{
// Access the primary header in each section to add the watermark
HeaderFooter header = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
// Clear any existing watermark shapes in the header
foreach (Shape shape in header.Shapes)
{
if (shape.Type == MsoShapeType.msoTextEffect)
{
shape.Delete();
}
}
// Add the watermark shape to the header
Shape watermarkShape = header.Shapes.AddTextEffect(
MsoPresetTextEffect.msoTextEffect1,
watermarkText,
"Arial", // Font name
50, // Font size
MsoTriState.msoTrue, // Bold
MsoTriState.msoFalse, // Not italic
0, // Position from the left
0 // Position from the top
);
// Format the watermark appearance
watermarkShape.Fill.Solid();
watermarkShape.Fill.ForeColor.RGB = (int)WdColor.wdColorGray25;
watermarkShape.Line.Visible = MsoTriState.msoFalse;
watermarkShape.WrapFormat.Type = WdWrapType.wdWrapBehind;
watermarkShape.Rotation = -45;
// Center the watermark on the page
watermarkShape.Left = (float)(doc.PageSetup.PageWidth - watermarkShape.Width) / 2;
watermarkShape.Top = (float)(doc.PageSetup.PageHeight - watermarkShape.Height) / 2;
// Set positioning relative to the page
watermarkShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
watermarkShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
// Lock aspect ratio and align text to prevent distortion
watermarkShape.LockAspectRatio = MsoTriState.msoTrue;
watermarkShape.TextEffect.Alignment = MsoTextEffectAlignment.msoTextEffectAlignmentCentered;
// Prevent users from resizing the watermark shape
watermarkShape.LockAspectRatio = MsoTriState.msoTrue;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-wordfile