You are viewing limited content. For full access, please sign in.

Discussion

Discussion

HOWTO: Convert PDF Bookmarks to Laserfiche Annotations

posted on September 23, 2019

This code quickly documents how you can extract all of the bookmarks in a PDF file and convert them into annotations (Sticky Notes) in Laserfiche Workflow 9.x and above. You may or may not need to install the iTextSharp PDF module as I believe it is installed with Workflow.

 

namespace WorkflowActivity.Scripting.ProcessPDFfile
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using Laserfiche.RepositoryAccess;
    using Laserfiche.RepositoryAccess.Common;
    using System.IO;
    using iTextSharp;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.Linq;

    /// <summary>
    /// Provides one or more methods that can be run when the workflow scripting activity is performed.
    /// </summary>
    public class Script1 : RAScriptClass92
    {

        DocumentInfo doc;

        public void recursive_search(IList<Dictionary<string, object>> kidsList)
        {
            for (int x = 0; x < kidsList.Count; x++)
            {
                string action = kidsList[x]["Action"].ToString().ToLower();
                string title = null;
                int page = 1;


                if (action == "goto")
                {
                    title = kidsList[x]["Title"].ToString().Trim();
                    page = int.Parse(kidsList[x]["Page"].ToString().Split(' ')[0]);
                    this.add_annotation(page,title);
                }
                else if (action == "uri")
                {
                    title = kidsList[x]["Title"].ToString().Trim();
                    this.add_annotation(page,title);
                }
                if (kidsList[x].ContainsKey("Kids"))
                {
                    IList<Dictionary<string, object>> subKidsList = (IList<Dictionary<string, object>>) kidsList[x]["Kids"];
                    this.recursive_search(subKidsList);
                }
            }
        }
        private void add_annotation(int pageno,string title){
            PageInfo pi = doc.GetPageInfo(pageno);
            NoteAnnotation na = new NoteAnnotation();
            na.Text = title;
            na.Position = new LfPoint(0,0);
            pi.AddAnnotation(na);
        }

        private void get_bookmarks(PdfReader opdf){
            using (PdfReader pdfReader = opdf)
            {
                IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfReader);

                for (int i = 0; i < bookmarks.Count; i++)
                {
                    string action = bookmarks[i]["Action"].ToString().ToLower();
                    string title = null;
                    int page = 1;

                    if(action == "goto")
                    {
                        title = bookmarks[i]["Title"].ToString().Trim();
                        page = int.Parse(bookmarks[i]["Page"].ToString().Split(' ')[0]);
                        this.add_annotation(page,title);
                    }
                    else if (action == "uri")
                    {

                        title = bookmarks[i]["URI"].ToString().Trim();
                        this.add_annotation(page,title);

                    }

                    if (bookmarks[i].ContainsKey("Kids"))
                    {
                        IList<Dictionary<string, object>> kids = (IList<Dictionary<string, object>>)bookmarks[i]["Kids"];
                        this.recursive_search(kids);
                    }
                    }
                }
        }
        protected override void Execute()
        {

            // Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            // Get bound entry as DocumentInfo object
            string tmpPath = Path.GetTempPath();
            FileStream lfFile;
            LaserficheReadStream lfStream;
            doc = (DocumentInfo)this.BoundEntryInfo;

            if (doc.PageCount > 0){
            string outputFile = Path.GetTempFileName();
            string contentType;
            using (lfStream = doc.ReadEdoc(out contentType)){
                using (lfFile = File.Create(outputFile)){
                    lfStream.CopyTo(lfFile);
                }
            }

            if(File.Exists(outputFile)){
                using(PdfReader pdfReader = new PdfReader(outputFile)){

                    this.get_bookmarks(pdfReader);

                }
                FileInfo tmpFile = new FileInfo(outputFile);
                tmpFile.Delete();
            }
            }
        }
    }
}

 

3 0
replied on September 23, 2019

Just a quick note, "installed with Workflow" is not the same as "guaranteed to be there through upgrades". The only script references that are guaranteed to be there through upgrades are the default ones in script activities (so any version of LFSO or RepositoryAccess that was installed with previous versions of WF - at the time of this writing, that's LFSO 8.0 through 9.0 and Repository Access 9.1, 9.2, 10.0, 10.2 and 10.4). Besides those, you should understand where the files are coming from, how they're licensed and how you'd get them if Laserfiche stopped installing them.

1 0
replied on September 24, 2019

Another thing to note is that iTextSharp has been depreciated, so changes are guaranteed to come at some point.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.