Recompress an existing PDF using color detection in VB.NET

This example shows how to recompress an existing PDF document containing various images from a scanner.


VB.NET
'We assume that GdPicture has been correctly installed and unlocked.
Dim oGdPicturePDF As New GdPicturePDF()
oGdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2)
oGdPicturePDF.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG)
Dim oGdPictureImaging As New GdPictureImaging()
Dim status As GdPictureStatus = oGdPicturePDF.LoadFromFile("c:\input.pdf", True)
If status = GdPictureStatus.OK Then
Dim PageCount As Integer = oGdPicturePDF.GetPageCount()
For Page As Integer = 1 To PageCount
oGdPicturePDF.SelectPage(Page)
For Image As Integer = 1 To oGdPicturePDF.GetPageImageCount()
Dim imageID As Integer = oGdPicturePDF.ExtractPageImage(Image)
If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
oGdPictureImaging.ColorDetection(imageID, True, True, True)
If oGdPicturePDF.ReplaceImage(oGdPicturePDF.GetPageImageResName(Image - 1), imageID, False) <> GdPictureStatus.OK Then
MessageBox.Show("Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
oGdPictureImaging.ReleaseGdPictureImage(imageID)
Else
MessageBox.Show("Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Next
Next
status = oGdPicturePDF.SaveToFile("c:\output.pdf", True)
If status = GdPictureStatus.OK Then
MessageBox.Show("Done!", "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("The PDF file can't be saved. Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
oGdPicturePDF.CloseDocument()
oGdPictureImaging.Dispose()
Else
MessageBox.Show("The PDF file can't be loaded. Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
oGdPicturePDF.Dispose()

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.