AVAILABLE NOW: Spring 2025 Release

Automatically Annotate and Bookmark PDF Files using Apryse

By Apryse | 2025 Jun 24

Sanity Image
Read time

3 min

Whether you're creating contracts for business-to-business agreements or putting together a searchable PDF repository, finding a fast and efficient PDF viewer is critical. Apryse provides the ability to extract text using OCR, annotate, bookmark, and add searchability to PDFs- all without the need for any manual inputs!

In this post we show how to use Apryse to add automation to your PDF files for annotation and bookmarking.

C# code:

using pdftron; 

using pdftron.PDF; 

using pdftron.PDF.Annots; 

using pdftron.SDF; 

 

namespace ConsoleApp1 

{ 

    internal class Program 

    { 

        static void Main(string[] args) 

        { 

            PDFNet.Initialize(PDFTronLicense.License); 

 

            FileSystemWatcher watcher = new FileSystemWatcher(); 

            watcher.Path = @"path_to_watch_folder"; 

            watcher.Filter = "*.pdf"; 

            watcher.Created += OnCreated; 

            watcher.EnableRaisingEvents = true; 

 

            Console.WriteLine("Press 'q' to quit the sample."); 

            while (Console.Read() != 'q') ; 

        } 

        private static void OnCreated(object source, FileSystemEventArgs e) 

        { 

            Console.WriteLine($"File: {e.FullPath} {e.ChangeType}"); 

            ProcessFile(e.FullPath); 

        } 

 

        private static void ProcessFile(string path) 

        { 

            if (IsFileLocked(path)) 

            { 

                Console.WriteLine("File is locked, waiting..."); 

                return; 

            } 

 

            AddFooterAndBookmarks(path); 

        } 

 

        private static bool IsFileLocked(string path) 

        { 

            try 

            { 

                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)) 

                { 

                    stream.Close(); 

                } 

            } 

            catch (IOException) 

            { 

                return true; 

            } 

            return false; 

        } 

 

        private static void AddFooterAndBookmarks(string path) 

        { 

 

            using (PDFDoc doc = new PDFDoc(path)) 

            { 

                doc.InitSecurityHandler(); 

 

                int pageNum = 1; 

                for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) 

                { 

                    Page page = itr.Current(); 

 

                    TextWidget footer = TextWidget.Create(doc, new Rect(0, 0, 600, 20), "footer"); 

                    footer.SetText($"Document automatically generated at {DateTime.Now}"); 

                    page.AnnotPushBack(footer); 

 

                    Bookmark bookmark = Bookmark.Create(doc, $"Page {pageNum}"); 

                    bookmark.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(page))); 

                    doc.AddRootBookmark(bookmark); 

 

                    pageNum++; 

                } 

 

                doc.Save(@"path_to_output.pdf”, SDFDoc.SaveOptions.e_linearized); 

            } 

        } 

    } 

} 

You now have a Microsoft Worker Service that automates bookmarking, annotating, and creating searchable PDF files using Apryse SDK. You can also make this into a Microsoft Windows Service.

To learn more, visit our documentation. 

Sanity Image

Apryse

Share this post

email
linkedIn
twitter