How To: Analyze the position of each image

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: Analyze the position of each image

Post by tjimenez »

Hello,

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

Explore each image in the PDF file to know his position (X,Y,width and height -rounding box-)

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
    For imageNo as integer = 1 to _document.Pages(pageNo).Images.Count - 1
            MsgBox(_document.Pages(pageNo).Images(imageNo).Width)
            MsgBox(_document.Pages(pageNo).Images(imageNo).Height)
           _document.Pages(pageNo).Images(imageNo).GetImage.Save("c:\Page" & pageNo & " image" & imageNo & ".jpg")
    Next
'we need to explore the position (x and y and rounding box) of each image in the PDF file
.../...
Next
----------------------------------------------------------------------------------------------------------------------------------------------
We have been reviewing the help, not just find a way to do this easily ???
We don't understand who it works...
Can you help us ?
Thanks.
Last edited by tjimenez on Mon Feb 25, 2013 3:13 pm, edited 1 time in total.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: How To: Analyze the position of each image

Post by Alex »

Hello,

PDF document contains image resources. You need to use the PdfDocument.Images property if you want to get information about image resources used in PDF document.

PdfDocument.Images property returns a collection of the PdfImageResource objects. The PdfImageResource class contains information about image size.


Each image resource can be used several times, for example, the same image resource can be used on several PDF pages. You need to use the PdfPage.ImageExtractor property if you want to get information about image resources used in PDF page.

PdfPage.ImageExtractor property returns an instance of the ImageExtractor class. The ImageExtractor class has the Images property. ImageExtractor.Images property returns an array of the ContentImage objects. The ContentImage class contains information about image size and position of the image on the PDF page.

Here is an example that demonstrates how to get information about position and size of image resources for each PDF page.

Code: Select all

Imports System.Drawing
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree
Imports Vintasoft.Pdf.Content.ImageExtraction

Module Module1

    Sub Main()
        ' open existing PDF document
        Dim pdfDocument As New PdfDocument("d:\PdfTest.pdf")

        ' for each PDF page
        For i As Integer = 0 To pdfDocument.Pages.Count - 1
            Console.WriteLine(String.Format("PDF page {0}", i))

            GetInfoAboutImageResourcesOfPdfPage(pdfDocument.Pages(i))
        Next

        ' free resources
        pdfDocument.Dispose()

        Console.ReadLine()
    End Sub

    Public Sub GetInfoAboutImageResourcesOfPdfPage(ByVal page As PdfPage)
        Dim contentImages As ContentImage() = page.ImageExtractor.Images
        For i As Integer = 0 To contentImages.Length - 1
            Console.WriteLine(String.Format("  Image X={0}, Y={1}, Width={2}, Height={3}", _
                                            contentImages(i).Region.LeftTop.X, _
                                            contentImages(i).Region.LeftTop.Y, _
                                            contentImages(i).Region.Bounds.Width, _
                                            contentImages(i).Region.Bounds.Height))
        Next
    End Sub

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

Re: How To: Analyze the position of each image

Post by tjimenez »

Okay, good explanation.
Thank you very much.
Post Reply