On Thu, Feb 25, 2010 at 2:12 PM, Greg Hile <[email protected]> wrote:

>  I have been looking at various ways to print a webpage to a PDF file with
> no user intervention.
>
> I have looked at iTextSharp and I am looking at PDF995. It looks like it
> costs only $10 and I had it recommended to me.
>
>
>
> Would anyone know about PDF995 or iTextSharp and if so, would they share
> their experience or recommendation?
>
>
>
> Greg
>

I believe I found the gist of this problem on EggheadCafe.  To get the image
to run through iTextSharp I use a class similar to this one:
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports System.Threading

Public Class PageSnapshot
    Private _bmp As Bitmap = Nothing
    Public ReadOnly Property Image() As Bitmap
        Get
            Return _bmp
        End Get
    End Property

    Private _url As String = Nothing
    Private _mre As New ManualResetEvent(False)
    Private _timeout As Int32 = 15
    Private _thumbWidth As Int32
    Private _thumbHeight As Int32
    Private _width As Int32
    Private _height As Int32
    Private _absolutePath As String

    Public Sub New(ByVal url As String _
                   , ByVal width As Int32 _
                   , ByVal height As Int32 _
                   , ByVal thumbWidth As Int32 _
                   , ByVal thumbHeight As Int32)
        _url = url
        _width = width
        _height = height
        _thumbHeight = thumbHeight
        _thumbWidth = thumbWidth
    End Sub

    Public Sub New(ByVal url As String _
                   , ByVal width As Int32 _
                   , ByVal height As Int32 _
                   , ByVal thumbWidth As Int32 _
                   , ByVal thumbHeight As Int32 _
                   , ByVal absolutePath As String)
        _url = url
        _width = width
        _height = height
        _thumbHeight = thumbHeight
        _thumbWidth = thumbWidth
        _absolutePath = absolutePath
    End Sub

    Protected Overrides Sub Finalize()
        If Not [Object].Equals(_bmp, Nothing) Then
            _bmp.Dispose()
        End If
    End Sub

    Public Function GetSnapShot() As Bitmap
        Dim fileName As String = _url.Replace("http://";, "") + ".jpg"
        fileName = System.Web.HttpUtility.UrlEncode(fileName)

        If Not [Object].Equals(_absolutePath, Nothing) _
           AndAlso File.Exists(_absolutePath + fileName) _
        Then
            'TODO: create a better cacheing mechanism here
            _bmp = CType(System.Drawing.Image.FromFile(_absolutePath +
fileName), Bitmap)
        Else
            Dim t As New Thread(New ThreadStart(AddressOf _getSnapShot))
            t.SetApartmentState(ApartmentState.STA)
            t.Start()
            _mre.WaitOne()
            t.Abort()
        End If
        Return _bmp
    End Function

    Private Sub _getSnapShot()
        Dim browser As New WebBrowser()
        Dim time As DateTime = DateTime.Now

        browser.ScrollBarsEnabled = False
        browser.Navigate(_url)
        AddHandler browser.DocumentCompleted _
            , New WebBrowserDocumentCompletedEventHandler(AddressOf
WebBrowser_DocumentCompleted)

        While (True)
            Thread.Sleep(0)
            Dim elapsedTime As TimeSpan = DateTime.Now - time
            If (elapsedTime.Seconds >= _timeout) Then
                _mre.Set()
            End If
            Application.DoEvents()
        End While
    End Sub

    Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e
As WebBrowserDocumentCompletedEventArgs)
        Dim browser As WebBrowser = CType(sender, WebBrowser)
        browser.ClientSize = New Size(_width, _height)
        browser.ScrollBarsEnabled = False
        _bmp = New Bitmap(browser.Bounds.Width, browser.Bounds.Height)
        browser.BringToFront()
        browser.DrawToBitmap(_bmp, browser.Bounds)
        Dim img As Image = _bmp.GetThumbnailImage(_thumbWidth, _thumbHeight,
Nothing, IntPtr.Zero)
        'TODO: make configurable for other-than-jpeg file formats
        Dim fileName As String = _url.Replace("http://";, "") + ".jpg"
        fileName = System.Web.HttpUtility.UrlEncode(fileName)

        If Not [Object].Equals(_absolutePath, Nothing) AndAlso Not
File.Exists(_absolutePath + fileName) Then
            img.Save(_absolutePath + fileName)
        End If
        _bmp = CType(img, Bitmap)
        browser.Dispose()

        If Not [Object].Equals(_mre, Nothing) Then
            _mre.Set()
        End If
    End Sub

    Public Shared Function GetSiteThumbnail(ByVal url As String _
                                            , ByVal width As Int32 _
                                            , ByVal height As Int32 _
                                            , ByVal thumbWidth As Int32 _
                                            , ByVal thumbHeight As Int32) As
Bitmap
        Dim thumb As New PageSnapshot(url, width, height, thumbWidth,
thumbHeight)
        Dim b As Bitmap = thumb.GetSnapShot

        If [Object].Equals(b, Nothing) Then
            b =
CType(System.Drawing.Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("WebpageSnapshot.NotAvailable.bmp")),
Bitmap)
        End If

        Return b
    End Function

    Public Shared Function GetSiteThumbnail(ByVal url As String _
                                            , ByVal width As Int32 _
                                            , ByVal height As Int32 _
                                            , ByVal thumbWidth As Int32 _
                                            , ByVal thumbHeight As Int32 _
                                            , ByVal absolutePath As String)
As Bitmap
        Dim thumb As New PageSnapshot(url, width, height, thumbWidth,
thumbHeight, absolutePath)
        Dim b As Bitmap = thumb.GetSnapShot

        If [Object].Equals(b, Nothing) Then
            b =
CType(System.Drawing.Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("WebpageSnapshot.NotAvailable.bmp")),
Bitmap)
        End If

        Return b
    End Function
End Class


-- 
California, in a move apparently intended to evade creditors, has its name
legally changed to ``South Oregon.'' - Dave Barry

Reply via email to