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();
}
}
}
}
}