Skip to content
Guides

Import & Export Annotations in PDF

This sample shows how to set up the export and import of annotations. The document from which the annotations are exported is an xfdf file.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self exportAnnotaiton:myDocument];
}

// Export annotaiton
- (void)exportAnnotaiton:(CPDFDocument *)document {
  NSString *commandLineStr = @"";
  
  // Get Sandbox path to a pdf file that contains annotaiton
  NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"AnnotationImportExport"];
  
  // Copy file
  if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
      [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  NSString *exportFilePath = [NSString stringWithFormat:@"%@/%@.xfdf",writeDirectoryPath,@"ExportAnnotaiton"];

  // Export annotaiton
  [document exportAnnotationToXFDFPath:exportFilePath];

  commandLineStr = [commandLineStr stringByAppendingString:@"Done.\n"];
  commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in ExportAnnotaitonTest.xfdf\n"];
  
  // Import annotaiton using the annotaiton you just exported
  [self importAnnotaiton:exportFilePath];
}

// Import annotaiton
- (void)importAnnotaiton:(NSString *exportFilePath) {
  NSString *commandLineStr = @"";
  
  // Get Sandbox path for saving the PDFFile
  NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"AnnotationImportExport"];
  NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];

  // Copy file
  if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
      [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"ImportAnnotaitonTest"];

  if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
      [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];

  // Import annotaiton
  CPDFDocument *importDocument = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:documentFolder]];
  [importDocument importAnnotationFromXFDFPath:exportFilePath];

  // Save the document in the PDF file
  NSURL *importURL = [NSURL fileURLWithPath:writeFilePath];
  [importDocument writeToURL:importURL];

  commandLineStr = [commandLineStr stringByAppendingString:@"Done.\n"];
  commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in ImportAnnotaitonTest.pdf\n"];
}