I have a big PDF file and want to copy some pages into a new PDF file using iTextSharp. This works fine with the following code.
Dim sourceFullFilePathAndName As String = "src.pdf"
Dim outputFullFilePathAndName As String = "cpy.pdf"
Dim pageFirst As Integer = 5
Dim pageLast As Integer = 10
Using rdr As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sourceFullFilePathAndName)
Dim docSrc As New iTextSharp.text.Document(rdr.GetPageSizeWithRotation(1))
Using fs = New System.IO.FileStream(outputFullFilePathAndName, System.IO.FileMode.Create)
Dim copy As New iTextSharp.text.pdf.PdfCopy(docSrc, fs)
docSrc.Open()
Dim bookmarks = New List(Of Dictionary(Of String, Object))() 'New ArrayList() '// changed
For pagenumber As Integer = pageFirst To pageLast
Dim ip = copy.GetImportedPage(rdr, pagenumber)
If pagenumber = pageFirst Then '// changed
Dim h = ip.Height '// changed
Dim test = New Dictionary(Of String, Object) 'New Hashtable() '// changed
test.Add("Action", "GoTo") '// changed
test.Add("Title", "Page1 0 H 0") '// changed
test.Add("Page", "1 XYZ 0 " & h.ToString & " 0") '// changed
bookmarks.Add(test) '// changed
End If '// changed
copy.AddPage(ip)
Next
copy.Outlines = bookmarks '// changed - got exception here!
docSrc.Close()
End Using
rdr.Close()
End Using
But I can not find out how to add a bookmark for each new page. I have found some samples like Bookmark to specific page using iTextSharp 4.1.6 but could not solve my problem.
UPDATE:
I added the code of how I tried to create the bookmarks marked with '// changed
Btw. I am using iTextSharp v5.4.4.
I am getting the following exception:
Unable to cast object of type 'System.Collections.ArrayList' to type 'System.Collections.Generic.IList1[System.Collections.Generic.Dictionary
2[System.String,System.Object]]'.
So I changed again using a List of Dictionary instead of a HashTable. Now I am not getting an error but I am also getting no bookmarks.