Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Aug 23, 2012 6:34 pm

I am trying to create a pdf to combine data from database and appending multiple uploaded files. I need create bookmarks for the uploaded files. That is what I did. Got data from database, appendPage with all the uploaded files. Save as a pdf. then open the saved pdf again. Go find My CV and try to created a link to this place. Here is the code.

Dim str as string =""
For Each page As PdfPageBase In doc.Pages
str = page.ExtractText()

If Instr(str,"My CV")=0 then
Dim BookMark As PdfBookmark = doc.Bookmarks.Add("CV")
Dim BookmarkDest As New PdfDestination(page, New Point(0, 0))
BookMark1.DisplayStyle = PdfTextStyle.Bold
BookMark1.Action = New PdfGoToAction(BookmarkDest)
End If
Next page

The issues I got
1. Got error complain Dim BookmarkDest As New PdfDestination(page, New Point(0, 0)) Object not reference. I though the page already defined.

if I changed the code like this
page = doc.Pages.Add(PdfPageSize.A4, margin)
Dim BookMark As PdfBookmark = doc.Bookmarks.Add("CV")
Dim BookmarkDest As New PdfDestination(page, New Point(0, 0))
BookMark1.DisplayStyle = PdfTextStyle.Bold
BookMark1.Action = New PdfGoToAction(BookmarkDest)

2. Create more than one bookmark.

LijuanWu_Spire
 
Posts: 14
Joined: Tue Aug 07, 2012 6:30 pm

Fri Aug 24, 2012 6:21 am

Hi LijuanWu_Spire,

We have solved your problem. Please check the following code. Hope it can help you.
Code: Select all
Sub Main()

        Dim doc As New PdfDocument("..\..\Pagination.pdf")

        Dim newDoc As New PdfDocument()

        For Each page As PdfPageBase In doc.Pages

            Dim newPage As PdfPageBase = newDoc.Pages.Add(page.Size, New PdfMargins(0))
            page.CreateTemplate().Draw(newPage, PointF.Empty)
            Dim str As String = page.ExtractText()

            If str.IndexOf("Experiment") <> -1 Then

                Dim bookmark As PdfBookmark = newDoc.Bookmarks.Add("CV")
                bookmark.Action = New PdfGoToAction(New PdfDestination(newPage, PointF.Empty))

            End If
           Next
   
        newDoc.SaveToFile("result.pdf")
        System.Diagnostics.Process.Start("result.pdf")
    End Sub

If you have any problems, please feel free to contact us.
Best wishes.
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Aug 27, 2012 3:44 pm

Amy

Thanks for you quick reply! I really appreciate your help. By the way I used 2.1 office since the latest version not working after downloaded. Cannot unzip.

Your code worked. No error anymore but the bookmark is not created on the main page. So I changed my code like this
Basically I added cover for each document. Those cover with text "Applicant Curriculum Vita(CV)","Self-Statement(Cover)" etc. I appended all the pages together as one pdf. then save and load again from file. Then add the following code to create bookmarks. The code worked but with one issue. The bookmark is always point to the next page of the page should point. I think that is because the new page was added after the cover page which is the page with the text. the last bookmark didn't workout even I added one blank page to the end. Is there a way to point to the previous page.


Dim NewDoc As New PdfDocument


For Each page As PdfPageBase In doc.Pages
Dim newPage As PdfPageBase = NewDoc.Pages.Add(page.Size, margin)

Dim str As String = page.ExtractText
If str.IndexOf("Applicant Curriculum Vita(CV)") <> -1 Then
page.CreateTemplate().Draw(newPage, PointF.Empty)
Dim Bookmark As PdfBookmark = doc.Bookmarks.Add("CV")

Bookmark.Action = New PdfGoToAction(New PdfDestination(newPage, New Point(0, 0)))
End If

If str.IndexOf("Self-Statement(Cover)") <> -1 Then
page.CreateTemplate().Draw(newPage, PointF.Empty)
Dim Bookmark1 As PdfBookmark = doc.Bookmarks.Add("Self-Statement")
Bookmark1.Action = New PdfGoToAction(New PdfDestination(newPage, New Point(0, 0)))

End If


If str.IndexOf("Endorsement Letter(Cover)") <> -1 Then
page.CreateTemplate().Draw(newPage, PointF.Empty)
Dim Bookmark2 As PdfBookmark = doc.Bookmarks.Add("Endorsement")
Bookmark2.Action = New PdfGoToAction(New PdfDestination(newPage, New Point(0, 0)))

End If

If str.IndexOf("Division Chair Statement(Cover)") <> -1 Then
page.CreateTemplate().Draw(newPage, PointF.Empty)
Dim Bookmark3 As PdfBookmark = doc.Bookmarks.Add("Division Chair Statement")
Bookmark3.Action = New PdfGoToAction(New PdfDestination(newPage, New Point(0, 0)))

End If

LijuanWu_Spire
 
Posts: 14
Joined: Tue Aug 07, 2012 6:30 pm

Tue Aug 28, 2012 6:17 am

Hi LijuanWu_Spire,

You need to modify your code, remove "page.CreateTemplate().Draw(newPage, PointF.Empty)" from if statement inside, only add "page.CreateTemplate().Draw(newPage, PointF.Empty)" to before of "Dim str As String = page.ExtractText". Please try to do it.

If you have any problems, please feel free to contact us.

Best wishes,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Aug 28, 2012 2:21 pm

I did that first. The bookmarks were not added at all. So I changed it to inside the if statement the bookmarks were added. But the destination is off because the new page added each time.

By the way when I use page.Canvas.DrawingString or add pdfTable. It seems didn't add more than one page. Is there some property I can set so the PDFDocument pages will automaticall grow? or I have to check the heigth of the page?

LijuanWu_Spire
 
Posts: 14
Joined: Tue Aug 07, 2012 6:30 pm

Wed Aug 29, 2012 5:31 am

Hi LijuanWu_Spire,

I'm a little confused about your problem. I don't know that you want what kind of the effect. Could you provide me a sample pdf-document? Thanks.

Also I provide you my demo. Please check the attachment(bookmark for merged pdf.zip).

Yes, there is some property you can set so the PDFDocument pages will automaticall grow. I provide you two demos. Please check the attachment(drawString.zip,addPdfTable.zip).

Best wishes,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Sep 05, 2012 8:09 pm

Basically the code you gave to me didn't work well. However I made it work by counting the pages. I'llpost my working code here just in case somebody do the similar work.

This is what I did. I got data from DB and write out and text and also with tables. Then append all the other files. If it's word convert them to pdf and convert. Save the file as one pdf. Then open again to add bookmarks to each section. For example first part Application Information. Make bookmark to first appended fils CV, Self Supporting Doc etc.

Dim doc As New PdfDocument
Dim iPage as Integer 'This is used to coun
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left

DrawApplicant(doc.Sections.Add(), margin) 'This is the function to write out all the data from db count the page so the iPage was set inside this function

'Pending cv
Dim doc_cv As New PdfDocument

doc_cv.LoadFromFile("CV.pdf")
doc.AppendPage(doc_cv)
Dim iPageAfterCV as Integer
iPageAfterCV =doc_cv.Pages.Count + iPage + 1 'this is the page mark for the second appended file
'Append another one
Dim doc_ss As New PdfDocument

doc_ss.LoadFromFile(Session("strPath") & "\SelfStatement.pdf")
doc.AppendPage(doc_ss)
'make book marks

Dim NewDoc As New PdfDocument

Dim j As Integer = 0

For Each page As PdfPageBase In doc.Pages
If j = iPage + 1 Then

Dim BookMark1 As PdfBookmark = doc.Bookmarks.Add(" CV")
Dim BookmarkDest As New PdfDestination(page, New Point(0, 0))
BookMark1.DisplayStyle = PdfTextStyle.Regular
BookMark1.Action = New PdfGoToAction(BookmarkDest)
End If

If j = iPageAfterCV Then

Dim BookMark1 As PdfBookmark = doc.Bookmarks.Add(" Self-Statement")
Dim BookmarkDest As New PdfDestination(page, New Point(0, 0))
BookMark1.DisplayStyle = PdfTextStyle.Regular
BookMark1.Action = New PdfGoToAction(BookmarkDest)
End If

j = j + 1
Next page
End If

'Save
doc.SaveToFile("test.pdf")

Hope it will help.

LijuanWu_Spire
 
Posts: 14
Joined: Tue Aug 07, 2012 6:30 pm

Thu Sep 06, 2012 2:19 am

Dear LijuanWu_Spire,

We are sorry that inconvenience caused by us. Thanks for your kindly share. We believe your sample code will be very helpful for somebody.

Best wishes,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF