Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Dec 21, 2023 9:07 am

Hi,
I am working on HTML string to PDF without a plugin where in my code I generate and html file and pass that string to my PDF convert method where I need to add the header text on each page. The problem is that the created header space text is coming in the specified font but my html string is not coming correctly my code is as below. And even when trying to add a rupee symbol using CultureInfo("en-IN") the rupee symbol is not getting showing instead showing this (¹ 8,64,999.00) I am using the License version with the latest release spire.pdf 9.12.0 version

Imports Spire.Additions.Html
Imports Spire.Pdf
Imports Spire.Pdf.AutomaticFields
Imports Spire.Pdf.Conversion.Compression
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.HtmlConverter
Imports File = System.IO.File
Imports System.Security.Policy
Imports System.Drawing.Printing
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Reflection.Emit
Imports Spire
Imports Spire.Pdf.Graphics.Fonts

Dim culture As New CultureInfo("en-IN")

Public Function CreateHeaderTemplate(doc As PdfDocument, margins As PdfMargins) As PdfPageTemplateElement
'get page size
Dim pageSize As SizeF = doc.PageSettings.Size
'MsgBox(pageSize.Width.ToString)
'create a PdfPageTemplateElement object as header space
Dim headerSpace As New PdfPageTemplateElement(pageSize.Width, margins.Top)
Try
headerSpace.Foreground = False
'declare two float variables
Dim x As Single = 0 'margins.Left
Dim y As Single = 0
Dim headerImage As PdfImage = PdfImage.FromFile(Application.StartupPath & "\OBS-Logo-Col-Otl.png")
'MsgBox(headerImage.Width.ToString + vbCrLf + headerImage.Height.ToString)
Dim width As Single = headerImage.Width / 1.7
Dim height As Single = headerImage.Height / 1.8
'MsgBox(width.ToString + vbTab + height.ToString + x.ToString)
Dim pen As New PdfPen(PdfBrushes.Black, 1)

'draw text in header space
Dim font As New PdfTrueTypeFont(New Font("Helvetica", 8.75F, FontStyle.Bold))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Center)
Dim format2 As New PdfStringFormat(PdfTextAlignment.Left)
' Specify the desired width for alignment
Dim totalWidth As Integer = 18
'Dim format1 As New PdfStringFormat(PdfTextAlignment.Left)
Dim TDAmt, ODAmt As String
TDAmt = MCurBal.ToString("c", culture)
ODAmt = cpd1.ToString("c", culture)
Dim headerText As [String] = "Statement of account for the period from " + yrst.Date.ToLongDateString + " to " +
Today.Date.ToLongDateString + "," + DateTime.Now.ToLongTimeString()
Dim custcode1 As String = "Customer Code: " + custcode '+ "-" + StrConv(CustNm, VbStrConv.ProperCase)
Dim custname As String = StrConv(PDFCustNm, VbStrConv.ProperCase)
Dim Creperiod As String = "Credit Period: " + StrConv(CustCrP.ToString + " Days", VbStrConv.ProperCase)
Dim Totdue As String = "Total Due: " + TDAmt.PadRight(totalWidth)
Dim overdue As String = "Overdue : " + ODAmt.PadLeft(totalWidth + 2)
Dim custadd5s As String = custadd5
Dim custadd2s As String = StrConv(add1, VbStrConv.ProperCase) + "," + StrConv(add2, VbStrConv.ProperCase)
Dim custadd4s As String = StrConv(add3, VbStrConv.ProperCase) + "-" + StrConv(pin, VbStrConv.ProperCase)

Dim size As SizeF = font.MeasureString(headerText, format1)
'headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format)
headerSpace.Graphics.DrawImage(headerImage, x + (pageSize.Width / 2) - 25, y, width, height)
headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x + (pageSize.Width / 2), y + 38, format1)
headerSpace.Graphics.DrawString(custname, font, PdfBrushes.Black, x + 20, y + 51, format2)
headerSpace.Graphics.DrawString(custadd2s, font, PdfBrushes.Black, x + 20, y + 66, format2)
headerSpace.Graphics.DrawString(custadd4s, font, PdfBrushes.Black, x + 20, y + 81, format2)
headerSpace.Graphics.DrawString(custadd5s, font, PdfBrushes.Black, x + 20, y + 96, format2)

headerSpace.Graphics.DrawString(custcode1, font, PdfBrushes.Black, x + 410, y + 51, format2)
headerSpace.Graphics.DrawString(Creperiod, font, PdfBrushes.Black, x + 410, y + 66, format2)
headerSpace.Graphics.DrawString(Totdue, font, PdfBrushes.Black, x + 410, y + 81, format2)
headerSpace.Graphics.DrawString(overdue, font, PdfBrushes.Black, x + 410, y + 96, format2)
' headerSpace.Graphics.DrawString(custadd4s, font, PdfBrushes.Black, x + 350, y + 62, format)
headerSpace.Graphics.DrawLine(pen, x + 20, y + 108, x + 580, y + 108)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
'return headerSpace
'MsgBox(headerSpace.ToString)
Return headerSpace
End Function

Public Sub PDFDOC_Cov(ByVal HtmlPath As String, ByVal Outpath As String)
Try
Dim targetPageNumber As Integer = 0
'' Set license key to use Spire in Free mode.
''Specify the license file by a FileInfo object.
Dim licenseFile As New FileInfo(Application.StartupPath + "\license.elic.xml")
Spire.Pdf.License.LicenseProvider.SetLicense(Application.StartupPath + "\license.elic.xml")
Spire.Pdf.License.LicenseProvider.LoadLicense()

''Create a font for the header
'Dim font As New PdfFont(PdfFontFamily.Helvetica, 12)
'Dim brush As PdfBrush = PdfBrushes.Black

'MsgBox(Application.StartupPath.ToString)
Dim doc As PdfDocument = New PdfDocument()
Dim setting As PdfPageSettings = New PdfPageSettings()
setting.Size = PdfPageSize.A4 'New SizeF(612, 781)
setting.Margins = New Spire.Pdf.Graphics.PdfMargins(0, 50)
Dim htmlLayoutFormat As PdfHtmlLayoutFormat = New PdfHtmlLayoutFormat()

' htmlLayoutFormat.Layout = PdfLayoutType.OnePage
''htmlLayoutFormat.SplitImages = True
htmlLayoutFormat.Layout = PdfLayoutType.Paginate
htmlLayoutFormat.IsWaiting = True
'MsgBox(HtmlPath)
Dim htmlString As String = File.ReadAllText(HtmlPath)
doc.PageSettings.Size = PdfPageSize.A4

Try
Dim thrd As Thread = New Thread(Sub()
doc.LoadFromHTML(htmlString, True, setting, htmlLayoutFormat)
End Sub)
thrd.SetApartmentState(ApartmentState.STA)
thrd.Start()
thrd.Join()
Catch ome As OutOfMemoryException
MsgBox(ome.ToString)
End Try
Dim fonts As PdfUsedFont() = doc.UsedFonts

For Each font As PdfUsedFont In fonts
Dim trueTypeFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("sans-serif", 14.0F))
MsgBox("font")
font.Replace(trueTypeFont)
Next
'doc.CompressionLevel = PdfCompressionLevel.Best
doc.SaveToFile(Outpath)

Dim compressor As PdfCompressor = New PdfCompressor(Outpath)
'compressor.Options.TextCompressionOptions.UnembedFonts = True
compressor.Options.ImageCompressionOptions.CompressImage = True
compressor.Options.ImageCompressionOptions.ImageQuality = ImageQuality.High
compressor.Options.ImageCompressionOptions.ResizeImages = True
'compressor.Options.TextCompressionOptions.CompressFonts = True
'compressor.Options.RemoveMetadata = True
compressor.CompressToFile(Outpath)

'MsgBox(Outpath.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

SwethaKalva
 
Posts: 9
Joined: Thu Mar 30, 2023 11:46 am

Thu Dec 21, 2023 10:08 am

Hello,

Thank you for your inquiry.
I appreciate the detailed information you provided. However, due to the complexity of the matter, I will require more time to conduct a thorough investigation. Rest assured that once I have obtained the necessary information, I will promptly provide you with feedback.

Thank you for your patience.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 22, 2023 3:02 am

Hello,

Thank you for your patience.
After conducting a detailed investigation into your code, I have identified the reason for the incorrect display of the Rupee symbol. It appears to be due to an incorrect font being used.
To resolve this issue, I am providing you with a font file that supports the Rupee symbol. Please download and install it in your testing environment. Once installed, please use the modified font code provided below:
Code: Select all
' Create a new PdfTrueTypeFont object with the specified font, size, and style ("Courier New", 8.75F, FontStyle.Bold)
Dim font As New PdfTrueTypeFont(New Font("Courier New", 8.75F, FontStyle.Bold), True)

' Define the symbol string
Dim symbol As String = "₹ ₹ ₹ ₹ the rupee symbol ₹ ₹ ₹ ₹"

' Draw the symbol string on the graphics object (headerSpace.Graphics) using the specified font, brush, position, and format
headerSpace.Graphics.DrawString(symbol, font1, PdfBrushes.Black, x + 20, y + 51, format2)

RupeeSymbol Display.png


Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF