Here's the method:
Code: Select all
Public Function VoidPDF() As Boolean
Dim OldImageID As Integer
Dim OldStatus As GdPictureStatus = Nothing
Dim Failed As Boolean = False
Dim Status As GdPictureStatus = Nothing
Using NewPDF As New GdPicturePDF
NewPDF.NewPDF()
Using OldGDPicImage As New GdPictureImaging
Using OldPDF As New GdPicturePDF
Status = OldPDF.LoadFromFile(_OldFile, False)
If Status = GdPictureStatus.OK Then
OldPDF.SelectPage(1)
OldImageID = OldPDF.RenderPageToGdPictureImage(300.0, False)
If OldImageID <> 0 Then
' Create an Image from the old PDF and write VOID on it
Dim myImage As Image = OldGDPicImage.GetBitmapFromGdPictureImage(OldImageID)
Dim g As Graphics = Graphics.FromImage(myImage)
g.DrawString("VOID", _VoidFont, Brushes.DarkRed, New PointF(1100, 3480))
g.Dispose()
g = Nothing
' Go create my new PDF
Using NewGDPicImage As New GdPictureImaging
Dim NewImageID As Integer = NewGDPicImage.CreateGdPictureImageFromBitmap(myImage)
NewPDF.AddImageFromGdPictureImage(NewImageID, False, True)
If NewPDF.GetStat() <> GdPictureStatus.OK Then
NewPDF.CloseDocument()
NewGDPicImage.ReleaseGdPictureImage(NewImageID)
MsgBox("Failed to Add Image")
Failed = True
End If
NewGDPicImage.ReleaseGdPictureImage(NewImageID)
End Using ' NewGDPicImage
GC.Collect()
End If
End If
End Using ' OldPDF
End Using ' OldGDPicImage
If Not Failed Then
NewPDF.SaveToFile(_NewFile)
If NewPDF.GetStat() <> GdPictureStatus.OK Then
MsgBox("Failed to Save New PDF")
End If
End If
NewPDF.CloseDocument()
NewPDF.Dispose()
End Using ' NewPDF
GC.Collect()
Return Not Failed
End Function
1) Opens a single-page PDF (about 1 meg in size...these are 300dpi PDFs)
2) Creates a GDPIcture Image from that page.
3) I write some text on the image.
4) I create a new PDF using the new image.
5) Close everything down and clean up.
The resulting PDF is also roughly 1meg in size as expected...however, I'm inside a loop that's converting 10,000 images and every time this function fires, I lose about 1meg of ram. I must be doing something very wrong - if anyone can see anything that I've obviously missed, your comments would be very much appreciated.
Kindest regards,
Elkay.