Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用asp.net和C#填写受保护文档上的word文档表单字段_C#_Asp.net_Ms Word - Fatal编程技术网

使用asp.net和C#填写受保护文档上的word文档表单字段

使用asp.net和C#填写受保护文档上的word文档表单字段,c#,asp.net,ms-word,C#,Asp.net,Ms Word,使用Visual Studio 2010和Microsoft Word 2010: 我有一个限制编辑的文档,在文档中编辑时只允许“填写表单” 在Word文档中,我从传统控件的“开发人员”选项卡添加了文本表单字段 我想做的是用数据填充其中的一些表单字段(比如它们的名称、地址等…我已经知道并已经从数据库中提取的东西) 我所尝试的: using System; using System.Configuration; using System.IO; using Microsoft.Office.Int

使用Visual Studio 2010和Microsoft Word 2010:

我有一个限制编辑的文档,在文档中编辑时只允许“填写表单”

在Word文档中,我从传统控件的“开发人员”选项卡添加了文本表单字段

我想做的是用数据填充其中的一些表单字段(比如它们的名称、地址等…我已经知道并已经从数据库中提取的东西)

我所尝试的:

using System;
using System.Configuration;
using System.IO;
using Microsoft.Office.Interop.Word;

var oWordApplication = new ApplicationClass();

object missing = System.Reflection.Missing.Value;
object fileName = ConfigurationManager.AppSettings["DocxPath"];
object newTemplate = false;
object docType = 0;
object isVisible = true;

var oWordDoc = oWordApplication.Documents.Add(fileName, newTemplate, docType, isVisible);

                if (oWordDoc.Bookmarks.Exists("txtName"))
                {
                    oWordDoc.Bookmarks["txtName"].Range.Text = "Test Field Entry from webform";
                }
我可以找到要编辑的字段,但在尝试修改文本时出现以下错误:

You are not allowed to edit this selection because it is protected.
这就是我要做的。 创建一个可以直接映射到word模板的类,然后将该类序列化为xml并使用下面的方法

    public static void ReplaceCustomXML(string fileName, string customXML)
    {
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true))
        {
            MainDocumentPart mainPart = wordDoc.MainDocumentPart;
            mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
            //Add a new customXML part and then add the content. 
            CustomXmlPart customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
            //Copy the XML into the new part. 
            using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream())) ts.Write(customXML);
        }
    }

    public static string SerializeObjectToString(this object obj)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            XmlSerializer x = new XmlSerializer(obj.GetType());
            x.Serialize(stream, obj);
            return Encoding.Default.GetString(stream.ToArray());
        }
    }
publicstaticvoidreplacecustomxml(字符串文件名,字符串customXML)
{
使用(WordprocessingDocument wordDoc=WordprocessingDocument.Open(文件名,true))
{
MainDocumentPart mainPart=wordDoc.MainDocumentPart;
mainPart.DeleteParts(mainPart.CustomXmlParts);
//添加新的customXML部件,然后添加内容。
CustomXmlPart CustomXmlPart=mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
//将XML复制到新部件中。
使用(StreamWriter ts=newstreamwriter(customXmlPart.GetStream()))ts.Write(customXML);
}
}
公共静态字符串序列化ObjectToString(此对象obj)
{
使用(MemoryStream stream=new MemoryStream())
{
XmlSerializer x=新的XmlSerializer(obj.GetType());
x、 序列化(流,obj);
返回Encoding.Default.GetString(stream.ToArray());
}
}
我建议您阅读本文,因为它使使用openxml更新文档变得简单


旁注:不支持在服务器上对Office应用程序(非交互式会话)使用互操作…好吧…废话:)所以它不起作用,还是麻烦太多?您可以使用OpenXML在服务器端处理较新的word文档(.docx)。不过,它比互操作稍微复杂一些。