0

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.Dictionary2[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.

Community
  • 1
  • 1
afj
  • 179
  • 1
  • 5
  • 13
  • The link you added to your question solves your problem. However, you say that it doesn't. That's confusing. Show us what you've tried based on that example and why that doesn't solve your problem. – Bruno Lowagie Aug 18 '14 at 16:57
  • Are you still confused or is something missing? – afj Aug 22 '14 at 05:15
  • I'm not familiar with the programming language you're using, but reading your code as if it were pseudocode, I don't understand why you are creating an outline tree of which every entry points at page 1 of the new document: `test.Add("Page", "1 XYZ 0 " & h.ToString & " 0")`. You should keep track of the number of pages added to `copy` and use the appropriate page number. For instance: if *document1* has 5 pages, then the bookmark for the first page of *document2* should point to page 6 not to page 1. – Bruno Lowagie Aug 22 '14 at 08:24
  • No, it's not pseudocode, but it's VB.Net. I just wanted to see if it works with one bookmark for the first page. If it works, I have to modify it for adding multiple bookmarks. So if you could help me this problem, it would be great. – afj Aug 23 '14 at 08:07
  • This example works for me: http://itextpdf.com/sandbox/merge/MergeWithOutlines It explains how to create an outline tree with a hierarchy and how to add it to a `PdfCopy` instance. Don't expect me to help you converting this example to VB.Net. The last time I wrote Visual Basic code was in 1996. – Bruno Lowagie Aug 23 '14 at 08:23

1 Answers1

0

Try

    Dim oReader As PdfReader
    oReader = New PdfReader(file name)
    Dim book_mark As New List(Of Dictionary(Of String, Object))
    book_mark = SimpleBookmark.GetBookmark(oReader)
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Alessandro Mandelli Feb 06 '20 at 16:26