Hi,
My purpose is I want to convert PDF to Tif file. If page is black and white then convert to 1BPP, and if page is color then do not do anything.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oGdPictureImaging As New GdPictureImaging
oGdPictureImaging.SetLicenseNumber("XXX") 'Replace XXX by a valid demo or commercial KEY
Dim ImageNo As Integer = 0 'Number of extracted bitmaps
Dim TiffID As Integer = 0 'the multipage TIFF document ID
Dim hPDF As Integer = oGdPictureImaging.PdfReaderLoadFromFile("c:\input.pdf")
Dim Status As GdPictureStatus = oGdPictureImaging.GetStat
If oGdPictureImaging.GetStat = GdPictureStatus.OK Then
For i As Integer = 1 To oGdPictureImaging.PdfReaderGetPageCount(hPDF)
If Status = GdPictureStatus.OK Then
If oGdPictureImaging.PdfReaderSelectPage(hPDF, i) Then
For j As Integer = 1 To oGdPictureImaging.PdfReaderGetImageCount(hPDF)
If Status = GdPictureStatus.OK Then
Dim ImageID As Integer = oGdPictureImaging.PdfReaderExtractImage(hPDF, j)
Status = oGdPictureImaging.GetStat
If Status = GdPictureStatus.OK Then
ImageNo += 1
If ImageNo = 1 Then
'Start the Multipage tiff document
TiffID = ImageID
Status = oGdPictureImaging.TiffSaveAsMultiPageFile(TiffID, "c:\output.tif", TiffCompression.TiffCompressionAUTO)
Else
'Add image to multipage tiff document
Status = oGdPictureImaging.TiffAddToMultiPageFile(TiffID, ImageID)
oGdPictureImaging.ReleaseGdPictureImage(ImageID)
End If
End If
End If
Next
Else
MsgBox("Error selecting page: " + Str(i))
Status = oGdPictureImaging.GetStat
End If
End If
Next
oGdPictureImaging.PdfReaderCloseDocument(hPDF)
Else
MsgBox("Can't open PDF")
End If
If TiffID <> 0 Then oGdPictureImaging.TiffCloseMultiPageFile(TiffID)
If Status = GdPictureStatus.OK Then
MsgBox("operation done with success !")
Else
MsgBox("an error occurred")
End If
End Sub 'GD Key
oGdPictureImaging.SetLicenseNumber("xxx")
'taken file handle
Dim hPDF As Integer = oGdPictureImaging.PdfReaderLoadFromFile(Application.StartupPath & "\test_backwhite2.pdf")
'display on viewer
GdViewer1.DisplayFromPDFHandle(hPDF)
'check if file type is pdf
If GdViewer1.GetDocumentType = GdPicture.DocumentType.DocumentTypePDF Then
'for each page of pdf file
For I As Integer = 1 To GdViewer1.PageCount
'select page
oGdPictureImaging.PdfReaderSelectPage(hPDF, I)
'get bitmap counts
If oGdPictureImaging.PdfReaderGetImageCount(hPDF) <= 1 Then
ImageID = oGdPictureImaging.PdfReaderExtractImage(hPDF, 1)
Else
'render on a viewer
dpi = GetFileDPI(Application.StartupPath & "\test_backwhite2.pdf", I)
ImageID = GdViewer1.PdfRenderPageToGdPictureImage(dpi, I)
End If
If I = 1 Then
TiffImageID = ImageID
Status = oGdPictureImaging.TiffSaveAsMultiPageFile(TiffImageID, Application.StartupPath & "\test_backwhite2.tif", GdPicture.TiffCompression.TiffCompressionAUTO)
Else
Status = oGdPictureImaging.TiffAddToMultiPageFile(TiffImageID, ImageID)
oGdPictureImaging.ReleaseGdPictureImage(ImageID)
End If
Next I
oGdPictureImaging.TiffCloseMultiPageFile(TiffImageID)
End If
GdViewer1.CloseDocument() Function GetFileDPI(ByVal hpdf As integer, ByVal page As Int16) As Integer
Dim oGdPictureImaging As New GdPicture.GdPictureImaging
Dim dpiSize As Single = 0
Dim hDPI As Single
Dim vDPI As Single
Try
If hPDF > 0 Then
'Select Page
oGdPictureImaging.PdfReaderSelectPage(hPDF, page)
Dim ImageCount As Integer = oGdPictureImaging.PdfReaderGetImageCount(hPDF)
For i As Integer = 1 To ImageCount
Dim ImageID As Integer = oGdPictureImaging.PdfReaderExtractImage(hPDF, i)
If ImageID <> 0 Then
hDPI = oGdPictureImaging.GetHorizontalResolution(ImageID)
vDPI = oGdPictureImaging.GetVerticalResolution(ImageID)
If (hDPI >= vDPI) And (hDPI > dpiSize) Then
dpiSize = hDPI
Else
If (vDPI > dpiSize) Then
dpiSize = vDPI
End If
End If
oGdPictureImaging.ReleaseGdPictureImage(ImageID)
End If
Next
End If
Return dpiSize
Catch ex As Exception
End Try
End FunctionOption Strict On
Option Explicit OnFunction GetFileDPI(ByVal hpdf As integer, ByVal page As Int16) As Integerdpi = GetFileDPI(Application.StartupPath & "\test_backwhite2.pdf", I) Function ConvertPDFtoTif(ByVal Path As String) As Boolean
Using oGdViewer As GdPicture.GdViewer = New GdPicture.GdViewer()
Dim oGdPictureImaging As New GdPicture.GdPictureImaging()
Dim MultiTiffID, ImageID, DPI As Integer
Dim Status As GdPicture.GdPictureStatus = GdPicture.GdPictureStatus.OK
Dim InputFilePath, OutputFilePath As String
Try
InputFilePath = Path
OutputFilePath = Mid(InputFilePath, 1, Len(InputFilePath) - 3) + "tif"
oGdViewer.SetLicenseNumber("xxx") 'Please, replace XXX by a valid license key. You can get one month trial key from http://www.evaluation-gdpicture.com
oGdPictureImaging.SetLicenseNumber("xxx")
oGdViewer.PDFShowDialogForPassword = False
oGdViewer.SilentMode = True
Dim hPDF As Integer = oGdPictureImaging.PdfReaderLoadFromFile(InputFilePath)
If oGdViewer.DisplayFromPDFHandle(hPDF) = GdPicture.GdPictureStatus.OK Then
For i As Integer = 1 To oGdViewer.PageCount
If Status = GdPicture.GdPictureStatus.OK Then
If oGdPictureImaging.PdfReaderSelectPage(hPDF, i) = True Then
If oGdPictureImaging.PdfReaderGetImageCount(hPDF) <= 1 Then
ImageID = oGdPictureImaging.PdfReaderExtractImage(hPDF, 1)
Else
DPI = GetFileDPI(hPDF, CShort(i))
DPI = CInt(IIf(DPI = 0, CType(300, Integer), DPI))
ImageID = oGdViewer.PdfRenderPageToGdPictureImage(DPI, i)
End If
If i = 1 Then
MultiTiffID = ImageID
Status = oGdPictureImaging.TiffSaveAsMultiPageFile(MultiTiffID, OutputFilePath, GdPicture.TiffCompression.TiffCompressionAUTO)
Else
Status = oGdPictureImaging.TiffAddToMultiPageFile(MultiTiffID, ImageID)
oGdPictureImaging.ReleaseGdPictureImage(ImageID)
End If
End If
End If
Next
oGdPictureImaging.TiffCloseMultiPageFile(MultiTiffID)
oGdPictureImaging.ReleaseGdPictureImage(MultiTiffID)
oGdViewer.CloseDocument()
Return True
Else
Return False
End If
Catch ex As Exception
End Try
End Using
End Function
Function GetFileDPI(byval hPDF As Integer, ByVal page As Int16) As Integer
Dim dpiSize As Single = 0
Dim hDPI As Single
Dim vDPI As Single
Dim oGdPictureImaging As GdPicture.GdPictureImaging
Try
oGdPictureImaging = New GdPicture.GdPictureImaging
oGdPictureImaging.SetLicenseNumber("xxx") 'Replace XXX by valid commercial or demo KEY
If hPDF > 0 Then
If oGdPictureImaging.PdfReaderSelectPage(hPDF, page) = True Then 'Select Page1
Dim ImageCount As Integer = oGdPictureImaging.PdfReaderGetImageCount(hPDF)
For i As Integer = 1 To ImageCount
Dim ImageID As Integer = oGdPictureImaging.PdfReaderExtractImage(hPDF, i)
If ImageID <> 0 Then
hDPI = oGdPictureImaging.GetHorizontalResolution(ImageID)
vDPI = oGdPictureImaging.GetVerticalResolution(ImageID)
If (hDPI >= vDPI) And (hDPI > dpiSize) Then
dpiSize = hDPI
Else
If (vDPI > dpiSize) Then
dpiSize = vDPI
End If
End If
oGdPictureImaging.ReleaseGdPictureImage(ImageID)
End If
Next
End If
End If
Return CInt(dpiSize)
Catch ex As Exception
End Try
End FunctionReturn to Example requests & Code samples For GdPicture.NET
Users browsing this forum: No registered users and 0 guests