Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivanatpr/c153cdd2a57f459afab2d96dcd8d7dc9 to your computer and use it in GitHub Desktop.
Save ivanatpr/c153cdd2a57f459afab2d96dcd8d7dc9 to your computer and use it in GitHub Desktop.
Slight alteration of original code (not mine) from here: http://www.anotherchris.net/csharp/parsing-xfdf-pdf-annotations/
//Note: quickly hacked together and only tested on one file
string xml = File.ReadAllText(@"sample.xfdf", Encoding.UTF8);
XDocument doc = XDocument.Load(new StringReader(xml));
var elements = doc.Root.Element(XName.Get("annots", "http://ns.adobe.com/xfdf/"))
.Elements(XName.Get("highlight", "http://ns.adobe.com/xfdf/"))
.OrderBy(e => int.Parse(e.Attribute("page").Value));
StringBuilder builder = new StringBuilder();
foreach (XElement element in elements)
{ var highlightText = element.Descendants(XName.Get("span", "http://www.w3.org/1999/xhtml")).FirstOrDefault()?.Value;
if (!string.IsNullOrWhiteSpace(highlightText))
{
var txt = String.Format("Page {0}: {1}", element.Attribute("page").Value, highlightText);
builder.AppendLine(txt);
}
}
Console.WriteLine(builder.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment