Here are some sample codes on how to use PDFLib in your application

Display basic information on a PDF:
Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

Console.WriteLine("Information for {0}", myPDF.FileInfo.FullName)
Console.WriteLine("  {0}", myPDF.Version)
Console.WriteLine("  {0} pages", myPDF.PageCount)

If myPDF.IsLinearized Then
     Console.WriteLine("  is linearized")
End If

If myPDF.IsEncrypt Then
     Console.WriteLine("  encrypted")
     Console.WriteLine("    Filter:             {0}", myPDF.EncryptionInformation.Filter)
End If

Console.WriteLine("  pdf information:")
Console.WriteLine("    Title:             {0}", myPDF.DocumentInformation.Title)
Console.WriteLine("    Author:            {0}", myPDF.DocumentInformation.Author)
Console.WriteLine("    Subject:           {0}", myPDF.DocumentInformation.Subject)
Console.WriteLine("    Keywords:          {0}", myPDF.DocumentInformation.Keywords)
Console.WriteLine("    Creator:           {0}", myPDF.DocumentInformation.Creator)
Console.WriteLine("    Producer:          {0}", myPDF.DocumentInformation.Producer)
Console.WriteLine("    Creation Date:     {0}", myPDF.DocumentInformation.CreationDate)
Console.WriteLine("    Modification Date: {0}", myPDF.DocumentInformation.GetModificationDate)

myPDF.close


Display information for a page in the PDF:
Dim page As pdflib.clsPDFPage

Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

Console.WriteLine("  {0} pages", myPDF.PageCount)

For Each page In myPDF.GetPages
    Console.WriteLine("  information for page number {0}:", page.Number)
    Console.WriteLine("    User Unit:  {0}", page.UserUnit)
    Console.WriteLine("    Page Width: {0}", page.Width)
    Console.WriteLine("    Page Height: {0}", page.Height)
    Console.WriteLine("    Page Rotation: {0}", page.Rotation)
Next 

myPDF.close


Modify the PDF document information properties:
Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

myPDF.DocumentInformation.Keywords = "Some keywords here"
myPDF.DocumentInformation.Author = "Unknown"
myPDF.DocumentInformation.Title = "A Title"
myPDF.DocumentInformation.Producer = "pdflib"
myPDF.DocumentInformation.Subject = "A subject"
myPDF.DocumentInformation.Creator = "Unknown"

myPDF.Save()

myPDF.close


Modify the PDF Viewer Preferences:
Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

If myPDF.ViewerPreferences Is Nothing Then
    'If the document do not contain preferences, with create them
    myPDF.CreateViewerPreferences()
End If

'We add some viewer preferences
myPDF.ViewerPreferences.HideWindowUI = True
myPDF.ViewerPreferences.CenterWindow = True
myPDF.ViewerPreferences.HideMenubar = True
myPDF.ViewerPreferences.HideToolbar = True
myPDF.ViewerPreferences.NumCopies = 5
myPDF.ViewerPreferences.DisplayDocTitle = True
myPDF.ViewerPreferences.PageLayout = PageLayoutTypes.TwoPageLeft
myPDF.ViewerPreferences.PageMode = PageModeTypes.UseThumbs

myPDF.Save()

myPDF.close


Add annotations to a PDF:
Dim page As pdflib.clsPDFPage

Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

Dim myAnnotation As New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)

myAnnotation.Location(300, 300, 300, 300)
myAnnotation.Contents = String.Format("This is an annotation for the PDF {0}", myPDF.FileInfo.Name)
myAnnotation.SetCharacteristic = pdflib.AnnotationFlags.ReadOnlyFlag Or pdflib.AnnotationFlags.Locked

'We add the annotation on each page of the PDF!
For Each page In myPDF.GetPages
     page.AddAnnotation(myAnnotation)
Next

myPDF.Save("C:\Test\test with annotations.pdf")

myPDF.close


Add images to a PDF:
Dim page As pdflib.clsPDFPage

Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

Dim myImage As New pdflib.clsPDFImage("C:\Test\test_image.jpg")

'We add the image on each page of the PDF!
For Each page In myPDF.GetPages
      page.AddImage(myImage, 200, 200, 100, 100)
Next

'We add another image only on the first page
Dim image2 As New pdflib.clsPDFImage("C:\Test\test_image2.jpg")
page = myPDF.GetPage(1)
page.AddImage(image2, 100, 100, 25, 25)

myPDF.Save("C:\Test\test with images.pdf")

myPDF.close


Erase and rotate a page:
Dim page As pdflib.clsPDFPage

Dim myPDF As New pdflib.ClsPDF("C:\Test\test.pdf")

page = myPDF.GetPage(1)
If Not page Is Nothing Then
     'We rotation page 1
     page.Rotation = pdflib.PageRotationTypes.Rotation_180
End If

page = myPDF.GetPage(2)
If Not page Is Nothing Then
     'We erase the content of page 2
     page.EraseContent()
End If

myPDF.Save("C:\Test\test with page changes.pdf")

myPDF.close


Extract all images that are embedded in the PDF:
Dim count As Integer
Dim imagelist As List(Of clsPDFImage)
Dim image As clsPDFImage
Dim page As clsPDFPage

Dim myPDF As New ClsPDF("C:\Test\test.pdf")

'We get the images of the pdf
imagelist = myPDF.GetImages

'We save them
count = 0
For Each image In imagelist
    count += 1
    image.Save("C:\Test\pdf_images\" + "_image_" + count.ToString)
Next

'Get Images from page 1 only
page = myPDF.GetPage(1)
imagelist = page.GetImages

myPDF.close

Last edited Jul 9, 2013 at 9:21 PM by charlesgiguere, version 25

Comments

ghextreme Apr 24, 2014 at 10:57 AM 
I can't use .PNG file on adding images on an existing PDF. Did I missed another codes for it?

siva524 May 6, 2013 at 10:27 AM 
can i Generate Thumbnail Images from PDF Documents with this library how it is to get thumbnail?

siva524 May 6, 2013 at 10:15 AM 
is it possible to save the pdf total page as a png or jpeg image?