You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 调用Office.Interop.Word插入文本水印报空引用异常问题

错误原因
  • Word.Application实例未初始化:代码中wordApp变量仅声明为null,未执行实例化操作就直接调用Documents.Open方法,触发空引用。
  • 页眉对象获取方式错误:调用wordApp.Selection.HeaderFooter时,默认选中区域不是页眉,该对象为null,直接访问其下的Shapes属性抛出异常。
  • 未处理Section页眉的联动属性:如果Section页眉开启了「链接到前一条页眉」,可能导致多节文档水印添加异常。
修正后可运行代码
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;

// 初始化必要参数
object oMissing = System.Reflection.Missing.Value;
string filePath = "你的源文档路径.docx";
object oOutputFileName = "你的输出路径.pdf";
object oSavePDFFormat = Word.WdSaveFormat.wdFormatPDF;

// 实例化Word应用对象
Word.Application wordApp = new Word.Application();
Word.Document doc = null;

try
{
    // 打开文档
    doc = wordApp.Documents.Open(ref filePath, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    Word.Shape txWatermark = null;
    // 遍历所有节
    foreach (Word.Section section in doc.Sections)
    {
        // 获取当前节的主页眉,关闭链接到前一条页眉,避免联动
        Word.HeaderFooter header = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
        header.LinkToPrevious = false;

        // 往页眉中添加文本水印
        txWatermark = header.Shapes.AddTextEffect(
            MsoPresetTextEffect.msoTextEffect1,
            "Enter The Text Here", "Arial", 60f,
            MsoTriState.msoTrue,
            MsoTriState.msoFalse,
            0, 0, ref oMissing);

        // 配置水印样式
        txWatermark.Select(ref oMissing);
        txWatermark.Fill.Visible = MsoTriState.msoTrue;
        txWatermark.Line.Visible = MsoTriState.msoFalse;
        txWatermark.Fill.Solid();
        txWatermark.Fill.ForeColor.RGB = (int)Word.WdColor.wdColorGray30;
        txWatermark.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
        txWatermark.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
        txWatermark.Left = (float)Word.WdShapePosition.wdShapeCenter;
        txWatermark.Top = (float)Word.WdShapePosition.wdShapeCenter;
        txWatermark.Height = wordApp.InchesToPoints(2.4f);
        txWatermark.Width = wordApp.InchesToPoints(6f);
        // 设置水印置于文字下方
        txWatermark.ZOrder(MsoZOrderCmd.msoSendBehindText);
    }

    // 保存为PDF
    doc.SaveAs2(ref oOutputFileName, ref oSavePDFFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
finally
{
    // 关闭文档、退出Word应用,释放COM资源,避免残留进程
    if (doc != null)
    {
        doc.Close(ref oMissing, ref oMissing, ref oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
    }
    if (wordApp != null)
    {
        wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
    }
}
关键改动说明
  • 新增了Word.Application的实例化逻辑,避免根对象空引用
  • 直接通过Section.Headers获取页眉对象,不再依赖不可靠的Selection对象
  • 新增LinkToPrevious = false配置,保证多节文档每个节都能正确添加水印
  • 新增了水印置于文字下方的配置,避免遮挡正文
  • 新增COM资源释放逻辑,避免Word后台进程残留

内容的提问来源于stack exchange,提问作者eugenebobalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:24:03