How To: Export a certain page of PDF to JPG file.

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
tjimenez
Posts: 8
Joined: Fri Feb 22, 2013 8:05 pm

How To: Export a certain page of PDF to JPG file.

Post by tjimenez »

Hello,

We are testing your "VintaSoftPDF.NET SDK" & "VintaSoftImaging.NET SDK" to get the following:

Create a JPG file from a page of PDF file, similary to your "Document > Save Current Page As..." from "VintaSoft PDF Reader Demo v3.1"

If we have next portion of code:

Code: Select all

Private imageViewer1 As Vintasoft.Imaging.ImageViewer
Private _fileStream As System.IO.Stream = Nothing
Private _document As Vintasoft.Pdf.PdfDocument
Private _useEmbeddedThumbnails As Boolean = True
Private _titlePrefix As String = "VintaSoft PDF Reader Demo v3.1 {0}"
Private _isFirstOpenedPage As Boolean = False

_fileStream = New System.IO.FileStream(strArchivoPDF, System.IO.FileMode.Open, System.IO.FileAccess.Read)
_document = Vintasoft.Pdf.PdfDocumentController.OpenDocument(_fileStream)
_document.RenderingSettings.DrawPdfAnnotations = True
_document.RenderingSettings.DrawVintasoftAnnotations = True
_document.RenderingSettings.UseEmbeddedThumbnails = _useEmbeddedThumbnails
_fileStream.Position = 0

Dim openFileThread As New System.Threading.Thread(AddressOf OpenFileAsynchronously)
openFileThread.Start()

_isFirstOpenedPage = True
Text = String.Format(_titlePrefix, String.Format("- {0}", System.IO.Path.GetFileName(strArchivoPDF)))
If _document.IsEncrypted Then
    Text += " (SECURED)"
End If
' #############################
' Let's go to generate JPG's...
' #############################
firstPage = 1
lastPage = _document.Pages.Count

For pageNo = 1 To _document.Pages.Count
        strNombreArchivoJPG = "PAG_" & Format(pageNo, "000") & ".jpg"
        .../...
Next
We have been reviewing the help, not just find a way to do this easily, without being as complicated as the example.
Can you help us ?
Thanks.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by Alex »

Hello,

Your code does not have part where you save image of PDF page to JPEG file.

Here is an example that shows how to save each PDF page as JPEG file using image collection and JpegEncoder:

Code: Select all

Imports System.IO
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Codecs

Module Module1

    Sub Main()
        SavePdfPagesAsJpeg("d:\PdfTest.pdf", "d:\jpegDir\")
    End Sub

    Public Sub SavePdfPagesAsJpeg(ByVal pdfFileName As String, ByVal jpegDir As String)
        ' create image collection
        Dim imageCollection As New ImageCollection()
        ' add PDF document to collection
        imageCollection.Add(pdfFileName)

        ' create JpegEncoder
        Dim jpegEncoder1 As New JpegEncoder()
        ' set quality of output JPEG files
        jpegEncoder1.Settings.Quality = 90

        ' save images of image collection to TIFF file using TiffEncoder
        Dim vsImage as VintasoftImage
        For i As Integer = 0 To imageCollection.Count - 1
            ' get image of PDF page as VintasoftImage object
            vsImage = imageCollection(i)
            ' save VintasoftImage object as JPEG file
            vsImage.Save(Path.Combine(jpegDir, String.Format("page{0}.jpg", i)), jpegEncoder1)
        Next

        ' free resources
        jpegEncoder1.Dispose()
        imageCollection.Dispose()
    End Sub

End Module
Best regards, Alexander
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by Alex »

Here is an example that shows how to save each PDF page as JPEG file using PdfDocument and VintasoftImage:

Code: Select all

Imports System.IO
Imports Vintasoft.Imaging
Imports Vintasoft.Pdf

Module Module1

    Sub Main()
        SavePdfPagesAsJpeg("d:\PdfTest.pdf", "d:\jpegDir\")
    End Sub

    Public Sub SavePdfPagesAsJpeg(ByVal pdfFileName As String, ByVal jpegDir As String)
        ' open existing PDF document
        Dim pdfDocument As New PdfDocument(pdfFileName)

        ' for each PDF page
        For i As Integer = 0 To pdfDocument.Pages.Count - 1
            ' get image of PDF page as VintasoftImage object
            Using vsImage As VintasoftImage = pdfDocument.Pages(i).Render()
                ' save VintasoftImage object as JPEG file
                vsImage.Save(Path.Combine(jpegDir, String.Format("page{0}.jpg", i)))
            End Using
        Next

        ' free resources
        pdfDocument.Dispose()
    End Sub

End Module
Best regards, Alexander
tjimenez
Posts: 8
Joined: Fri Feb 22, 2013 8:05 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by tjimenez »

Okay, it's a lot easier than I thought.
Thank you very much for your efficiency.
tjimenez
Posts: 8
Joined: Fri Feb 22, 2013 8:05 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by tjimenez »

Ok, I'll do something like this, but adding JPEGencoder :

Code: Select all

Imports System.IO
Imports Vintasoft.Imaging
Imports Vintasoft.Pdf
Imports Vintasoft.Imaging.Codecs

Module Module1

    Sub Main()
        SavePdfPagesAsJpeg("d:\PdfTest.pdf", "d:\jpegDir\")
    End Sub

    Public Sub SavePdfPagesAsJpeg(ByVal pdfFileName As String, ByVal jpegDir As String)
        ' open existing PDF document
        Dim pdfDocument As New PdfDocument(pdfFileName)
        Dim JPEGencoder As New JpegEncoder()

        JPEGencoder.Settings.Quality = 90
        JPEGencoder.Settings.SaveAsGrayscale = False

        ' for each PDF page
        For i As Integer = 0 To pdfDocument.Pages.Count - 1
            ' get image of PDF page as VintasoftImage object
            Using vsImage As VintasoftImage = pdfDocument.Pages(i).Render()
                ' save VintasoftImage object as JPEG file
                vsImage.Save(Path.Combine(jpegDir, String.Format("page{0}.jpg", i)), JPEGencoder)
            End Using
        Next

        ' free resources
        pdfDocument.Dispose()
    End Sub

End Module
This works fine, but.. How to set DPI to 150, for example ?
Thanks for advance.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by Alex »

How to set DPI to 150, for example ?
You need change the rendering settings of PDF document if you want to render PDF pages with custom settings.

Here is an example:

Code: Select all

...
Dim pdfDocument As New PdfDocument(pdfFileName)
pdfDocument.RenderingSettings.Resolution = New Resolution(150, 150)
...
Best regards, Alexander
tjimenez
Posts: 8
Joined: Fri Feb 22, 2013 8:05 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by tjimenez »

It works perfect, Alex !!!
Thanks!

I have not been able to find this information in the "Vintasoft.Imaging.chm".
At least, in a way so clear as you explain ...

Have you got any document with more information than this one ?
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: How To: Export a certain page of PDF to JPG file.

Post by Alex »

Hello,

Thank you for appreciating of my explanations.

Developer can get information from many sources: We continuously improve and extend our documentation. New version of documentation will be available with version 7.0 of SDK.

Please ask your questions and we will explain how to solve your task in the best way.

Best regards, Alexander
Post Reply