// // Copyright (c) 2001-2022 by PDFTron Systems Inc. All Rights Reserved. // using pdftron; using pdftron.Common; using pdftron.Filters; using pdftron.SDF; using pdftron.PDF; using pdftron.PDF.Annots; using System; using System.Collections.Generic; namespace AnnotationTestCS { /// /// Summary description for Class1. /// class Class1 { private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance(); static Class1() {} private static void AnnotationHighLevelAPI(PDFDoc doc) { for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) { Page page = itr.Current(); int num_annots = page.GetNumAnnots(); for (int i = 0; i < num_annots; ++i) { try { Annot annot = page.GetAnnot(i); if (!annot.IsValid()) continue; if (!annot.IsMarkup()) continue; var typesToRepair = new HashSet { Annot.Type.e_Circle, Annot.Type.e_Highlight, Annot.Type.e_Ink, Annot.Type.e_Line, Annot.Type.e_Polygon, Annot.Type.e_Polyline, Annot.Type.e_Square, Annot.Type.e_Squiggly, Annot.Type.e_StrikeOut, Annot.Type.e_Underline }; if (!typesToRepair.Contains(annot.GetType())) continue; // could apply to other annot types, even all Markup string contents = annot.GetContents(); // Adobe shows sticky note if there is any text, including whitespace. But Adobe does not show the note icon if a truly empty Contents entry. if (String.IsNullOrEmpty(contents)) continue; Text textIrtAnnot = Text.Create(doc, annot.GetRect(), contents); textIrtAnnot.GetSDFObj().Put("IRT", annot.GetSDFObj()); // set this as the page.AnnotPushBack(textIrtAnnot); // note that since we stored the amount of annots we will not iterate this new one. annot.GetSDFObj().Erase("Contents"); // no exceptions so erase the old contents // (optional) copy over extra info so it looks "better" in Adobe side panel. Markup mk = new Markup(annot.GetSDFObj()); Date dt = mk.GetCreationDates(); if(dt != null && dt.IsValid()) { textIrtAnnot.SetCreationDates(dt); } dt = annot.GetDate(); if (dt != null && dt.IsValid()) { textIrtAnnot.SetDate(dt); } string author = mk.GetTitle(); if(!String.IsNullOrWhiteSpace(author)) { textIrtAnnot.SetTitle(author); } } catch(Exception e) { // log if you like } } } } /// /// The main entry point for the application. /// [System.STAThread] static void Main(string[] args) { PDFNet.Initialize(PDFTronLicense.Key); try { using (PDFDoc doc = new PDFDoc(inputPath)) { doc.InitSecurityHandler(); AnnotationHighLevelAPI(doc); doc.Save(outputPath, SDFDoc.SaveOptions.e_linearized); } } catch (Exception e) { System.Console.WriteLine(e); } PDFNet.Terminate(); } } }