Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GDI Detect Horizontal or Vertical Image

Status
Not open for further replies.

talon121

IS-IT--Management
Jan 23, 2003
22
US
Im using the itextsharp library,

Wondering on a multi-page tif, right now my code is producing vertical pages only... Even for pages within the TIF that are horizontal;

I am lost as to why, and a way to fix this....

[tt]
Public Function ConvertTIFtoPDF(ByVal SourceFilename As String, ByVal TargetFile As String) As Boolean

Try

If File.Exists(SourceFilename) Then
Dim document As Document = New Document(PageSize.A4, 50, 50, 50, 50)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New System.IO.FileStream(TargetFile, System.IO.FileMode.Create, System.IO.FileAccess.Write))
Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(SourceFilename)

Dim numberOfPages As Integer = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)
document.Open()
Dim cb As PdfContentByte = writer.DirectContent

Dim page As Integer = 0
Do While (page < numberOfPages)

bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page)
''Console.WriteLine("Page " & page & " Phs Dim:" & bitmap.PhysicalDimension.ToString)
'Console.WriteLine("Page " & page & " Size:" & bitmap.Size.ToString)


Dim stream As New System.IO.MemoryStream
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.ToArray)

' This step is really slow
img.ScalePercent((72.0F / bitmap.HorizontalResolution * 100))

img.SetAbsolutePosition(0, 0)
cb.AddImage(img)
document.NewPage()
page = (page + 1)


Loop
document.Close()


Return True

Else
Return False
End If


Catch ex As Exception
Return False
'Debug.WriteLine(ex.ToString)
End Try



End Function

[/tt]
 
Okay, fixed it.

Simple fix, just didnt know where to put the offending code...

[tt]
Public Function ConvertTIFtoPDF(ByVal SourceFilename As String, ByVal TargetFile As String) As Boolean

Try

If File.Exists(SourceFilename) Then
Dim document As Document = New Document(PageSize.A4, 50, 50, 50, 50)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New System.IO.FileStream(TargetFile, System.IO.FileMode.Create, System.IO.FileAccess.Write))
Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(SourceFilename)

Dim numberOfPages As Integer = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)



document.Open()
Dim cb As PdfContentByte = writer.DirectContent

Dim page As Integer = 0
Do While (page < numberOfPages)




bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page)
'Console.WriteLine("Page " & page & " Width and Height: " & bitmap.Width & "x" & bitmap.Height)



''Console.WriteLine("Page " & page & " Phs Dim:" & bitmap.PhysicalDimension.ToString)
'Console.WriteLine("Page " & page & " Size:" & bitmap.Size.ToString)



Dim stream As New System.IO.MemoryStream
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.ToArray)

If img.Width > img.Height Then
img.RotationDegrees = 90
End If

' This step is really slow
img.ScalePercent((72.0F / bitmap.HorizontalResolution * 100))

img.SetAbsolutePosition(0, 0)
cb.AddImage(img)
document.NewPage()
page = (page + 1)


Loop
document.Close()


Return True

Else
Return False
End If


Catch ex As Exception
Return False
'Debug.WriteLine(ex.ToString)
End Try



End Function
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top