Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am working on an assignment in which I have to fetch list of hyperlinks existing in a docx file. The file may contain links in headers/footers as well.
The approach I am using is through OpenXML. I have been able to detect Hyperlinks that are existing in the body of the docx file using the approach below :
IEnumerable<HyperlinkRelationship> hyperlinks = from r in wordDocx.MainDocumentPart.HyperlinkRelationships where ((r.RelationshipType == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink")) select r;
foreach (HyperlinkRelationship hr in hyperlinks)
{
 if (isValidURL(hr.Uri.ToString()))
 {
  lstBox.Items.Add(hr.Uri.ToString());
 }
}


Any suggestions on how to iterate through the header/footer document parts and check for existence of Hyperlinks?

Regards!
Posted

It has code in C# sections. Check if it helps you.
http://msdn.microsoft.com/en-us/library/ee413542(v=office.12).aspx#Y4700[^]
 
Share this answer
 
Comments
Karunish 18-Apr-11 8:26am    
Have tried that mate. Problem is I am not able to form Hyperlink relationship for the Header/footer part reference.
We need to iterate withing HeaderFooterReference class to create HyperlinkRelationship and then use LINQ Query to find the result. Here is how I resolved this problem.
protected ListBox GetHeaderFooterHyperLinks(WordprocessingDocument wordDocx)
{
     string headerFooterRelationshipID;
     foreach (HeaderFooterReferenceType hdrFtr in wordDocx.MainDocumentPart.Document.Descendants<HeaderFooterReferenceType>())
  {
         headerFooterRelationshipID = hdrFtr.Id.Value;
         IEnumerable<HyperlinkRelationship> hdrFtrLinks = from hdrFooter in wordDocx.MainDocumentPart.GetPartById(headerFooterRelationshipID).HyperlinkRelationships where (hdrFooter.RelationshipType == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink") select hdrFooter;
    foreach(HyperlinkRelationship hf in hdrFtrLinks)
    {
       lstBox.Items.Add(hf.Uri.ToString());
    }
  }
  return lstBox;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900