Export annotation data from PDFs in C# .NET

PDF

To export data from a PDF annotation, follow the steps below:

  1. Create a GdPicturePDF object.
  2. Load the PDF file with the LoadFromFile method.
  3. Loop through all PDF pages.
  4. Select the PDF page from where to get the annotation data with the SelectPage method.
  5. Get the total number of annotations in the PDF document with the GetAnnotationCount method.
  6. Loop through all annotations.
  7. Use a method to get the annotation data and save it to a variable. For more information, refer to the guide on getting PDF annotation properties.

The following code example gets the index number, type, and contents of all annotations and saves them to a CSV file:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Create a `StringBuilder` variable to store data.
StringBuilder data = new StringBuilder();
// Add headers to the first line.
String[] headers = { "Index", "Annotation Type", "Contents", "Page" };
data.AppendLine(string.Join(";", headers));
// Get the number of pages.
int pageCount = gdpicturePDF.GetPageCount();
// Loop through all pages.
for(int page = 1; page <= pageCount; page++)
{
// Select a PDF page.
gdpicturePDF.SelectPage(page);
int annotCount = gdpicturePDF.GetAnnotationCount();
// Loop through all annotations.
for (int i = 0; i < annotCount; i++)
{
// Get the current annotation type.
string annotType = gdpicturePDF.GetAnnotationSubType(i);
// Get the current annotation contents.
string annotContents = gdpicturePDF.GetAnnotationContents(i);
// Add a new line to the `StringBuilder` with the annotation data.
String[] newLine = { i.ToString(), annotType.ToString(),
annotContents.ToString(), page.ToString() };
data.AppendLine(string.Join(";", newLine));
}
}
// Save the collected data to a CSV file.
String formData = @"C:\temp\output.csv";
File.AppendAllText(formData, data.ToString());