Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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
C# 用openxml修改超链接_C#_Openxml - Fatal编程技术网

C# 用openxml修改超链接

C# 用openxml修改超链接,c#,openxml,C#,Openxml,我正在尝试修改word文档中的超链接。超链接最初指向外部文档中的书签。我想做的是将其更改为指向一个内部书签,该书签与同一个锚点相同 这是我使用的代码。。。当我查看变量时,它似乎起作用,但当我查看保存的文档时,它与原始文档完全相同 我的机会没有持续下去的原因是什么 // read file specified in stream MemoryStream stream = new MemoryStream(File.ReadAllBytes("C:\\TEMPO\\smartbook\\text

我正在尝试修改word文档中的超链接。超链接最初指向外部文档中的书签。我想做的是将其更改为指向一个内部书签,该书签与同一个锚点相同

这是我使用的代码。。。当我查看变量时,它似乎起作用,但当我查看保存的文档时,它与原始文档完全相同

我的机会没有持续下去的原因是什么

// read file specified in stream 
MemoryStream stream = new MemoryStream(File.ReadAllBytes("C:\\TEMPO\\smartbook\\text1.docx"));
WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);

MainDocumentPart mainPart = doc.MainDocumentPart;

// The first hyperlink -- it happens to be the one I want to modify 
Hyperlink hLink = mainPart.Document.Body.Descendants<Hyperlink>().FirstOrDefault();
if (hLink != null)
{
    // get hyperlink's relation Id (where path stores)
    string relationId = hLink.Id;
    if (relationId != string.Empty)
    {
        // get current relation
        HyperlinkRelationship hr = mainPart.HyperlinkRelationships.Where(a => a.Id == relationId).FirstOrDefault();
        if (hr != null)
        {
            // remove current relation
            mainPart.DeleteReferenceRelationship(hr);
            // add new relation with relation 
            // mainPart.AddHyperlinkRelationship(new Uri("C:\\TEMPO\\smartbook\\test.docx"), false, relationId);
        }
    }

    // change hyperlink attributes
    hLink.DocLocation = "#";
    hLink.Id = "";
    hLink.Anchor = "TEST";
}
// save stream to a new file
File.WriteAllBytes("C:\\TEMPO\\smartbook\\test.docx", stream.ToArray());
doc.Close();
//读取流中指定的文件
MemoryStream stream=新的MemoryStream(File.ReadAllBytes(“C:\\TEMPO\\smartbook\\text1.docx”);
WordprocessingDocument doc=WordprocessingDocument.Open(stream,true);
MainDocumentPart mainPart=doc.MainDocumentPart;
//第一个超链接--正好是我要修改的超链接
Hyperlink hLink=mainPart.Document.Body.subjections().FirstOrDefault();
如果(hLink!=null)
{
//获取超链接的关系Id(路径存储的位置)
string relationId=hLink.Id;
if(relationId!=string.Empty)
{
//获取当前关系
HyperlinkRelationship hr=mainPart.HyperlinkRelationships.Where(a=>a.Id==relationId.FirstOrDefault();
如果(hr!=null)
{
//删除当前关系
主要部分。删除参考关系(hr);
//使用关系添加新关系
//mainPart.AddHyperlinkRelationship(新Uri(“C:\\TEMPO\\smartbook\\test.docx”),false,relationId);
}
}
//更改超链接属性
hLink.DocLocation=“#”;
hLink.Id=“”;
hLink.Anchor=“测试”;
}
//将流保存到新文件
writealBytes(“C:\\TEMPO\\smartbook\\test.docx”,stream.ToArray());
doc.Close();

编写流时,您尚未保存OpenXmlPackage

// types that implement IDisposable are better wrapped in a using statement
using(var stream = new MemoryStream(File.ReadAllBytes(@"C:\TEMPO\smartbook\text1.docx")))
{
   using(var doc = WordprocessingDocument.Open(stream, true))
   {
      // do all your changes
      // call doc.Close because that SAVES your changes to the stream
      doc.Close(); 
   }
   // save stream to a new file
   File.WriteAllBytes(@"C:\TEMPO\smartbook\test.docx", stream.ToArray());
} 
该方法明确说明:

保存并关闭OpenXml包以及所有底层零件流


您还可以将
AutoSave
属性设置为true,在这种情况下,调用
Dispose
时将保存OpenXMLPackage。我上面使用的
使用
语句将保证这种情况会发生。

非常感谢,它成功了!现在,在Word中,当我向一个内部书签添加超链接时,没有URL,只有一个“#”在文本前面,例如“#书签”。您知道我如何在self上创建URL而不是指定“C:\TEMPO\smartbook\test.docx”吗