Extract bitmap from PDF to multipage TIFF

Example requests & Code samples for GdPicture Toolkits.
Post Reply
charuvas1
Posts: 38
Joined: Tue Dec 02, 2008 1:49 pm

Extract bitmap from PDF to multipage TIFF

Post by charuvas1 » Sat May 08, 2010 2:37 pm

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.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Extract bitmap from PDF to multipage TIFF

Post by Loïc » Sun May 09, 2010 12:49 pm

Hi,

Here a code to extract all bitmap from a PDF and save they within a multipage TIFF image.
This code use mixed compression technology, this means that each page of the multipage document will have it's own compression scheme depending of it's bitdepth.

Note: the code work only with the next GdPicture.NET release (We will publish it on Monday 10th May).

Code: Select all

    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
Kind regards,

Loïc

charuvas1
Posts: 38
Joined: Tue Dec 02, 2008 1:49 pm

Re: Extract bitmap from PDF to multipage TIFF

Post by charuvas1 » Mon May 10, 2010 2:46 pm

Hi

I checked your code in new release and it works fine for extracting each bitmap object. But I want some thing different,
if a page having more than one bitmap object then i want to render that page on a viewer and append/write that page to tif file.
Now for rendering a page you need dpi. I want to get the maximum dpi from the bitmap objects and then i want to render on a viewer. For that i made a function GetFileDPI in which i pass a pdf file handle and page no.

Code: Select all

        '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()


Code: Select all

  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 Function

If I do in this way i got a generic exception in second page onwards. After doing so many things i come to know if i extract image in current handle of a file it gives an exception. If in my Getfiledpi function i open a same file again and close file before returning dpi value, it works fine. Can you tell me why this happens? If you see code flow it should be work.


charu

charuvas1
Posts: 38
Joined: Tue Dec 02, 2008 1:49 pm

Re: Extract bitmap from PDF to multipage TIFF

Post by charuvas1 » Fri May 14, 2010 7:05 am

Hi

Anybody has any idea on this issue? Please help me...

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Extract bitmap from PDF to multipage TIFF

Post by Loïc » Mon May 17, 2010 10:30 am

Hi,

First I will suggest you to add theses lines on top as your VB file code:

Code: Select all

Option Strict On
Option Explicit On
You are using undeclared variables & you are doing too many error related to type cast. it is VIP you solve first these problem because this make your code absolutely incomprehensible.

Here an example:

Your function GetFileDPI takes an integer as first parameter (As I understood, a PDF handle):

Code: Select all

Function GetFileDPI(ByVal hpdf As integer, ByVal page As Int16) As Integer
Then, you call is passing a String value as first param. !!

Code: Select all

dpi = GetFileDPI(Application.StartupPath & "\test_backwhite2.pdf", I)

So:
- Please try first to solve all this issues.
- If the problem persists, give us a version of your latest code version for investigation

With best regards,

Loïc

charuvas1
Posts: 38
Joined: Tue Dec 02, 2008 1:49 pm

Re: Extract bitmap from PDF to multipage TIFF

Post by charuvas1 » Tue May 18, 2010 2:13 pm

Hi,

see my complete code without an error

Code: Select all

 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 Function

I want to send a file in which error occurs. It is PDF file so i am submitting a ticket in which forum link is mention.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Extract bitmap from PDF to multipage TIFF

Post by Loïc » Tue May 18, 2010 3:48 pm

Hi,

OK Charu I got it !

I understood what your problem was & found a bug related to image extraction.
It will be fixed for the next release (coming within few days).

Sorry for the inconvenient.

Kind regards,

Loïc

charuvas1
Posts: 38
Joined: Tue Dec 02, 2008 1:49 pm

Re: Extract bitmap from PDF to multipage TIFF

Post by charuvas1 » Thu May 20, 2010 11:07 am

Hi,

Thanks... I want to tell you for black and white DPI, it give wrong value. It returns 96 even if page was written on 200 DPI.
I already mention this in one of my tickets and some body told me that bug will be fix out in future. So please try to fix it out with my existence issue.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests