How do I read / write PDF annotations to a separate file?

Q:

I’m currently trying to write out all of the annotations to a file separate from the PDF so the annotations can be managed separately from the report. I’m able to iterate through all of the pages and find all of the annotations, but when I try to write to a file, I’m not seeing what I expected. What is the best way to save all of the annotations to a separate file?

PDFDoc currentReport = mPDFView.GetDoc();

PageIterator itr = currentReport.GetPageIterator();

string annotationFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, “annotationfile.dat”);

StdFile outfile = new StdFile(annotationFilePath, StdFileOpenMode.e_append_mode);

using (FilterWriter fwriter = new FilterWriter(outfile))

{

for (; itr.HasNext(); itr.Next())

{

pdftron.PDF.Page page = itr.Current();

int pageAnnots = page.GetNumAnnots();

for (int i = 0; i < pageAnnots; i++)

{

IAnnot a = itr.Current().GetAnnot(i);

pdftron.SDF.Obj o = a.GetSDFObj();

o.Write(fwriter);

}

}

fwriter.FlushAll();

}

A:

Actually PDFNet offers a much simpler way to serialize / load annotations to/from PDF :slight_smile: This is shown in FDF sample project (the snippet should also be part of WinRT SDK):

http://www.pdftron.com/pdfnet/samplecode.html#FDF

e.g. in C#: http://www.pdftron.com/pdfnet/samplecode/FDFTest.cs

For example:

using (FDFDoc doc_annots = pdfdoc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only)){

doc_annots.SaveAsXFDF(“annots.xfdf”);

}

You can also load/merge annotations from XFDF into PDF:

using (FDFDoc fdf = new FDFDoc(FDFDoc.CreateFromXFDF(“annots.xfdf”)) {

pdfdoc.FDFMerge(pdfdoc);

}

XFDF is an open format for annotation interchange and is now part of ISO32000 (for old spec which is separate from PDF spec please see http://partners.adobe.com/public/developer/en/xml/XFDF_Spec_3.0.pdf)

For more info on recent additions to this api (e.g. save/load xfdf from a string) please see: https://groups.google.com/d/msg/pdfnet-sdk/tEKoY1IBTy0/VPLdiuUURSYJ