有时,Document.Descendants< DocumentFormat.OpenXml.Wordprocessing.Hyperlink>()不会返回word文档中的每个超链接 [英] Sometimes Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>() won't return every hyperlink in a word document
问题描述
我有以下代码, 尝试查询每个自定义超链接,替换它的uri或完全删除链接,取决于参数。
我发现了一些word文档,这个方法会跳过一些链接,b / c Document.Descendants< DocumentFormat.OpenXml.Wordprocessing.Hyperlink>()未返回整个列表,对于同一个文件,如果我修改一点点,插入
每一个环节之前的一些文字,然后下面的代码工作正常。 谢谢。 Chang
public static bool FixWordLinks(字符串docFile,字符串版本,bool isDeleteLink)
{
试试
{
int count = 0;
使用(WordprocessingDocument包= WordprocessingDocument.Open(DOCFILE,真))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP; {
文献mainDocument = package.MainDocumentPart.Document;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;的foreach(DocumentFormat.OpenXml.Wordprocessing.Hyperlink超级链接中mainDocument.Descendants< DocumentFormat.OpenXml.Wordprocessing.Hyperlink>())
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; HyperlinkRelationship oldLinkRelationship = package.MainDocumentPart.HyperlinkRelationships.FirstOrDefault(C => c.Id
== hyperlink.Id.Value);
&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; count ++;
$
&NBSP;&NBSP;&NBSP;串oldUri = oldLinkRelationship.Uri.AbsoluteUri;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;如果(oldUri.StartsWith(QUOT;光环://" ;, StringComparison.OrdinalIgnoreCase))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if(isDeleteLink)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; DocumentFormat.OpenXml.Wordprocessing.Text newText =新DocumentFormat.OpenXml.Wordprocessing.Text(QUOT;删除");
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;否则
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; HyperlinkRelationship newHRL = package.MainDocumentPart.AddHyperlinkRelationship(新
URI("光环://2345分之2222/ 01-2334"),TRUE);
&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; hyperlink.Id.Value = newHRL.Id;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; package.MainDocumentPart.DeleteReferenceRelationship(oldLinkRelationship);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
}
}
Console.WriteLine(count);
返回true;
}
catch(例外情况)
{
返回false;
}
}
更多信息:mainDocument.Descendants< DocumentFormat.OpenXml.Wordprocessing.Hyperlink>()返回基于我如何创建链接单词中的超链接。 假设我要创建10个超链接,其中url类似于abc:// 121212121/1121-aasdsa-121 / basd-1 / object
如果我单击鼠标右键打开上下文菜单,并选择"超链接",输入显示文本和网址,链接将显示为带有下划线的常规链接对象,对于这种链接,上述方法可以正确返回10个链接。
但是,如果我输入10行文本,然后选择每行的第一个单词,转到插入|超链接菜单,输入url,链接不显示常规下划线,对于这种链接,上面的方法只会返回部分链接,可能是3或5.进一步
更多,我创建的第二种链接,现在我在链接之前输入一些文本,我会得到更多的链接返回,即使可能不是全部。所以看起来函数的行为完全取决于我是如何创建链接的?这是一个错误,如何
修复它?
任何人都可以提供帮助吗?
I have following code , try to query every custom hyperlink, replace its uri or remove the link completely, depends on the parameter.
I found out for some word document, this method will skip some links, b/c Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>() didn't return a whole list, for the same document, if I modify it a little bit, insert some text before each link, then following code will work fine. Thanks. Chang
public static bool FixWordLinks(string docFile, string version, bool isDeleteLink)
{
try
{
int count = 0;
using (WordprocessingDocument package = WordprocessingDocument.Open(docFile, true))
{
Document mainDocument = package.MainDocumentPart.Document;
foreach (DocumentFormat.OpenXml.Wordprocessing.Hyperlink hyperlink in mainDocument.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>())
{
HyperlinkRelationship oldLinkRelationship = package.MainDocumentPart.HyperlinkRelationships.FirstOrDefault(c => c.Id
== hyperlink.Id.Value);
count++;
string oldUri = oldLinkRelationship.Uri.AbsoluteUri;
if (oldUri.StartsWith("aura://", StringComparison.OrdinalIgnoreCase))
{
if (isDeleteLink)
{
DocumentFormat.OpenXml.Wordprocessing.Text newText = new DocumentFormat.OpenXml.Wordprocessing.Text("Deleted");
DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
run.AppendChild(newText);
hyperlink.Parent.InsertBefore(run, hyperlink);
hyperlink.Remove();
}
else
{
HyperlinkRelationship newHRL = package.MainDocumentPart.AddHyperlinkRelationship(new
Uri("aura://2222/2345/01-2334"), true);
hyperlink.Id.Value = newHRL.Id;
}
package.MainDocumentPart.DeleteReferenceRelationship(oldLinkRelationship);
}
}
}
Console.WriteLine(count);
return true;
}
catch (Exception ex)
{
return false;
}
}
More info: mainDocument.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>() returns the links based on how I create the hyperlink in word. Say I am going to create 10 hyperlink, with url like abc://121212121/1121-aasdsa-121/basd-1/object
If I click right mouse to bring up context menu, and select 'Hyperlink', enter the display text and url, the link will display like a regular link object with underline, for this kind of links, above method can return 10 links correctly.
However, if I enter 10 lines of text, then select first word of each line, go to Insert|Hyperlink menu, enter url, the link doesn't show underline like regular one, for this kind of links, above method will only return partial links, may be 3 or 5. Further more, with second kind of links I created, now I enter some text before the links, I will get more links returned, even though may not be all of them. So seems like the function behaves totally depends on how I created the links? Is this a bug, and how to fix it?
Any one can help?
这篇关于有时,Document.Descendants< DocumentFormat.OpenXml.Wordprocessing.Hyperlink>()不会返回word文档中的每个超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!