R
Risa-
Guest
I'm trying to convert html page to pdf.
As a current solution, it's OK to convert it to image first, then convert image to pdf.
The code below converts html to image by loading the page with webbrowser.
Imports System.Windows.Forms
Imports System.Drawing
Public Class HtmlCapture
Private _Web As WebBrowser
Private _Timer As Timer
Private _Screen As Rectangle
Private _ImgSize As System.Nullable(Of Size) = Nothing
'an event that triggers when the html document is captured
Public Delegate Sub HtmlCaptureEvent(sender As Object, url As Uri, image As Bitmap)
Public Event HtmlImageCapture As HtmlCaptureEvent
'class constructor
Public Sub New()
'initialise the webbrowser and the timer
_web = New WebBrowser()
_Timer = New Timer()
_Timer.Interval = 2000
_Screen = Screen.PrimaryScreen.Bounds
'set the webbrowser width and hight
_web.Width = _Screen.Width
_web.Height = _Screen.Height
'suppress script errors and hide scroll bars
_web.ScriptErrorsSuppressed = True
_web.ScrollBarsEnabled = False
'attached events
AddHandler _web.Navigating, AddressOf web_Navigating
AddHandler _web.DocumentCompleted, AddressOf web_DocumentCompleted
AddHandler _Timer.Tick, AddressOf tready_Tick
End Sub
#Region "Public methods"
Public Sub Create(url As String)
_ImgSize = Nothing
_web.Navigate(url)
End Sub
Public Sub Create(url As String, imgsz As Size)
Me._ImgSize = imgsz
_web.Navigate(url)
End Sub
#End Region
#Region "Events"
Private Sub web_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
'start the timer
_Timer.Start()
End Sub
Private Sub web_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs)
'stop the timer
_Timer.[Stop]()
End Sub
Private Sub tready_Tick(sender As Object, e As EventArgs)
'stop the timer
_Timer.[Stop]()
'get the size of the document's body
Dim body As Rectangle = _Web.Document.Body.ScrollRectangle
'check if the document width/height is greater than screen width/height
Dim docRectangle As New Rectangle() With { _
.Location = New Point(0, 0), _
.Size = New Size(If(body.Width > _Screen.Width, body.Width, _Screen.Width), If(body.Height > _Screen.Height, body.Height, _Screen.Height)) _
}
'set the width and height of the WebBrowser object
_Web.Width = docRectangle.Width
_Web.Height = docRectangle.Height
'if the imgsize is null, the size of the image will
'be the same as the size of webbrowser object
'otherwise set the image size to imgsize
Dim imgRectangle As Rectangle
If _ImgSize Is Nothing Then
imgRectangle = docRectangle
Else
imgRectangle = New Rectangle() With { _
.Location = New Point(0, 0), _
.Size = _ImgSize.Value _
}
End If
'create a bitmap object
Dim bitmap As New Bitmap(imgRectangle.Width, imgRectangle.Height)
'get the viewobject of the WebBrowser
Dim ivo As IViewObject = TryCast(_Web.Document.DomDocument, IViewObject)
Using g As Graphics = Graphics.FromImage(bitmap)
'get the handle to the device context and draw
Dim hdc As IntPtr = g.GetHdc()
ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, _
imgRectangle, docRectangle, IntPtr.Zero, 0)
g.ReleaseHdc(hdc)
End Using
'invoke the HtmlImageCapture event
RaiseEvent HtmlImageCapture(Me, _Web.Url, bitmap)
End Sub
#End Region
End Class
Then convert that image to pdf with the following code.
Private folder As OpenFileDialog = New OpenFileDialog()
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If folder.ShowDialog() = DialogResult.OK Then
Me.textBox1.Text = folder.FileName
End If
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrWhiteSpace(Me.textBox1.Text) Then
ConvertJPG2PDF(Me.textBox1.Text, "D:\ttest.pdf")
MessageBox.Show("Done!")
End If
End Sub
Private Sub ConvertJPG2PDF(ByVal jpgfile As String, ByVal pdf As String)
Dim document = New Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25)
Using stream = New FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None)
PdfWriter.GetInstance(document, stream)
document.Open()
Using imageStream = New FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim image = Image.GetInstance(imageStream)
If image.Height > iTextSharp.text.PageSize.A4.Height - 25 Then
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25)
ElseIf image.Width > iTextSharp.text.PageSize.A4.Width - 25 Then
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25)
End If
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE
document.Add(image)
End Using
document.Close()
End Using
End Sub
Private Sub AddHtml(ByVal doc As Document, ByVal writer As PdfWriter, ByVal _str As String)
Dim array As Byte() = System.Text.Encoding.UTF8.GetBytes(_str)
Dim stream As MemoryStream = New MemoryStream(array)
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, stream, CType(Nothing, Stream), System.Text.Encoding.UTF8, New SongFontFactory())
End Sub
Is there any way to convert html to pdf directly?
Thanks for your help.
Risa
remember make the reply as answer and vote the reply as helpful if it helps.
Continue reading...
As a current solution, it's OK to convert it to image first, then convert image to pdf.
The code below converts html to image by loading the page with webbrowser.
Imports System.Windows.Forms
Imports System.Drawing
Public Class HtmlCapture
Private _Web As WebBrowser
Private _Timer As Timer
Private _Screen As Rectangle
Private _ImgSize As System.Nullable(Of Size) = Nothing
'an event that triggers when the html document is captured
Public Delegate Sub HtmlCaptureEvent(sender As Object, url As Uri, image As Bitmap)
Public Event HtmlImageCapture As HtmlCaptureEvent
'class constructor
Public Sub New()
'initialise the webbrowser and the timer
_web = New WebBrowser()
_Timer = New Timer()
_Timer.Interval = 2000
_Screen = Screen.PrimaryScreen.Bounds
'set the webbrowser width and hight
_web.Width = _Screen.Width
_web.Height = _Screen.Height
'suppress script errors and hide scroll bars
_web.ScriptErrorsSuppressed = True
_web.ScrollBarsEnabled = False
'attached events
AddHandler _web.Navigating, AddressOf web_Navigating
AddHandler _web.DocumentCompleted, AddressOf web_DocumentCompleted
AddHandler _Timer.Tick, AddressOf tready_Tick
End Sub
#Region "Public methods"
Public Sub Create(url As String)
_ImgSize = Nothing
_web.Navigate(url)
End Sub
Public Sub Create(url As String, imgsz As Size)
Me._ImgSize = imgsz
_web.Navigate(url)
End Sub
#End Region
#Region "Events"
Private Sub web_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
'start the timer
_Timer.Start()
End Sub
Private Sub web_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs)
'stop the timer
_Timer.[Stop]()
End Sub
Private Sub tready_Tick(sender As Object, e As EventArgs)
'stop the timer
_Timer.[Stop]()
'get the size of the document's body
Dim body As Rectangle = _Web.Document.Body.ScrollRectangle
'check if the document width/height is greater than screen width/height
Dim docRectangle As New Rectangle() With { _
.Location = New Point(0, 0), _
.Size = New Size(If(body.Width > _Screen.Width, body.Width, _Screen.Width), If(body.Height > _Screen.Height, body.Height, _Screen.Height)) _
}
'set the width and height of the WebBrowser object
_Web.Width = docRectangle.Width
_Web.Height = docRectangle.Height
'if the imgsize is null, the size of the image will
'be the same as the size of webbrowser object
'otherwise set the image size to imgsize
Dim imgRectangle As Rectangle
If _ImgSize Is Nothing Then
imgRectangle = docRectangle
Else
imgRectangle = New Rectangle() With { _
.Location = New Point(0, 0), _
.Size = _ImgSize.Value _
}
End If
'create a bitmap object
Dim bitmap As New Bitmap(imgRectangle.Width, imgRectangle.Height)
'get the viewobject of the WebBrowser
Dim ivo As IViewObject = TryCast(_Web.Document.DomDocument, IViewObject)
Using g As Graphics = Graphics.FromImage(bitmap)
'get the handle to the device context and draw
Dim hdc As IntPtr = g.GetHdc()
ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, _
imgRectangle, docRectangle, IntPtr.Zero, 0)
g.ReleaseHdc(hdc)
End Using
'invoke the HtmlImageCapture event
RaiseEvent HtmlImageCapture(Me, _Web.Url, bitmap)
End Sub
#End Region
End Class
Then convert that image to pdf with the following code.
Private folder As OpenFileDialog = New OpenFileDialog()
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If folder.ShowDialog() = DialogResult.OK Then
Me.textBox1.Text = folder.FileName
End If
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrWhiteSpace(Me.textBox1.Text) Then
ConvertJPG2PDF(Me.textBox1.Text, "D:\ttest.pdf")
MessageBox.Show("Done!")
End If
End Sub
Private Sub ConvertJPG2PDF(ByVal jpgfile As String, ByVal pdf As String)
Dim document = New Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25)
Using stream = New FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None)
PdfWriter.GetInstance(document, stream)
document.Open()
Using imageStream = New FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim image = Image.GetInstance(imageStream)
If image.Height > iTextSharp.text.PageSize.A4.Height - 25 Then
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25)
ElseIf image.Width > iTextSharp.text.PageSize.A4.Width - 25 Then
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25)
End If
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE
document.Add(image)
End Using
document.Close()
End Using
End Sub
Private Sub AddHtml(ByVal doc As Document, ByVal writer As PdfWriter, ByVal _str As String)
Dim array As Byte() = System.Text.Encoding.UTF8.GetBytes(_str)
Dim stream As MemoryStream = New MemoryStream(array)
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, stream, CType(Nothing, Stream), System.Text.Encoding.UTF8, New SongFontFactory())
End Sub
Is there any way to convert html to pdf directly?
Thanks for your help.
Risa
remember make the reply as answer and vote the reply as helpful if it helps.
Continue reading...