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.

Fri Oct 12, 2018 3:28 pm

Hi,

Can I change font of specific row in the PdfTable to bold?
only in a specific rows in the table, I wouldn't like to bold all the rows.
(it will be better if I can assigned them bold in advance)


This is my code:

Dim PdfTable As New PdfTable()
PdfTable.Style.CellPadding = 2
PdfTable.Style.HeaderSource = PdfHeaderSource.Rows
PdfTable.Style.HeaderRowCount = 1
PdfTable.Style.ShowHeader = True
PdfTable.DataSource = dataSource


Dim fontCell As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Alef-Regular.ttf", 12.0F), True)
PdfTable.Style.DefaultStyle.Font = fontCell
Dim result As PdfLayoutResult = PdfTable.Draw(page, New PointF(0, high))

bergernir
 
Posts: 3
Joined: Fri Oct 12, 2018 3:18 pm

Mon Oct 15, 2018 7:30 am

Hello,

Thank you for conatcting us.
Please kindly refer to below sample code to achieve your demond, the version I used is Spire.PDF Pack(Hot Fix) Version:4.9.7. If there is any question, just feel free to write back.
Code: Select all
Shared Sub Main(ByVal args() As String)
    Dim doc As New PdfDocument()
    Dim page As PdfPageBase = doc.Pages.Add()
    Dim table As New PdfTable()
    table.Style.DefaultStyle.Font = New PdfTrueTypeFont("Alef-Regular.ttf", 12.0F)

    'Create a DataTable
    Dim dataTable As New DataTable()
    dataTable.Columns.Add("1")
    dataTable.Columns.Add("2")
    dataTable.Columns.Add("3")
    dataTable.Rows.Add(New String() {"1A", "1B", "1C"})
    dataTable.Rows.Add(New String() {"2A", "2B", "2C"})

    'Assign data table as data source to the table.
    table.DataSource = dataTable

    'Subscribe to event.
    AddHandler table.BeginRowLayout, AddressOf Table_BeginRowLayout

    'Draw the table on the page and save the document.
    table.Draw(page, New RectangleF(0, 20, 200, 90))
    doc.SaveToFile("result.pdf")
    Process.Start("result.pdf")
End Sub
'Set Bold font for special row in the BeginRowLayout event.
Private Shared Sub Table_BeginRowLayout(ByVal sender As Object, ByVal args As BeginRowLayoutEventArgs)
    If args.RowIndex = 1 Then
        args.CellStyle.Font = New PdfTrueTypeFont("Alef-Regular.ttf", 16.0F, PdfFontStyle.Bold)
    End If
End Sub

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Wed Oct 17, 2018 8:07 am

Hello,

Greetings from E-iceblue.
Did my code help you? Thanks in advance for your valuable feedback and time.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Thu Oct 18, 2018 5:58 pm

Hi Lisa,
Unfortunately it doesn't.

Actually, it seems like the rest of the table become smaller (attached a screenshot)

I modify the code according to your advise:

Dim PdfTable As New PdfTable()
PdfTable.Style.CellPadding = 2
PdfTable.Style.HeaderSource = PdfHeaderSource.Rows
PdfTable.Style.HeaderRowCount = 1
PdfTable.Style.ShowHeader = True
PdfTable.DataSource = dataSource


'Subscribe to event.
AddHandler PdfTable.BeginRowLayout, AddressOf Table_BeginRowLayout


Dim fontCell As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Alef-Regular.ttf", 16.0F), True)
PdfTable.Style.DefaultStyle.Font = fontCell
Dim result As PdfLayoutResult = PdfTable.Draw(page, New PointF(0, high))
.....

'Set Bold font for special row in the BeginRowLayout event.
Private Shared Sub Table_BeginRowLayout(ByVal sender As Object, ByVal args As BeginRowLayoutEventArgs)
If args.RowIndex = 10 Then
args.CellStyle.Font = New PdfTrueTypeFont(New Font("Alef-Regular.ttf", 1.0F), PdfFontStyle.Bold)

End If
End Sub

bergernir
 
Posts: 3
Joined: Fri Oct 12, 2018 3:18 pm

Thu Oct 18, 2018 6:09 pm

Im sorry this is not the correct code , the function that you provided throw an exaction on this line:
args.CellStyle.Font = New PdfTrueTypeFont("Alef-Regular.ttf", 16.0F, PdfFontStyle.Bold)

System.Exception: 'Cannot open file: Alef-Regular.ttf for reading.'


so I change it a bit to:
'Set Bold font for special row in the BeginRowLayout event.
Private Shared Sub Table_BeginRowLayout(ByVal sender As Object, ByVal args As BeginRowLayoutEventArgs)
If args.RowIndex = 10 Then
args.CellStyle.Font = New PdfTrueTypeFont(New Font("Alef-Regular.ttf", 16.0F), PdfFontStyle.Bold)

End If
End Sub

bergernir
 
Posts: 3
Joined: Fri Oct 12, 2018 3:18 pm

Fri Oct 19, 2018 9:49 am

Dear Nir Berger,

Thanks for your feedback.
Below are my answers to your issues.
1. As for the exception (Cannot open file: Alef-Regular.ttf for reading), please make sure that the font path is correct on your side. For instance, the Alef-Regular.ttf is placed on C disk. The correct code should be like this.
Code: Select all
New PdfTrueTypeFont("C:\\Alef-Regular.ttf", 16.0F)

2. The New PdfTrueTypeFont(New Font("Alef-Regular.ttf", 16.0F), PdfFontStyle.Bold) also has an error, the parameter to specify the font represents the FontName, you should use below code to get the correct font. Note: the premise is that you have to install the font on your computer.
Code: Select all
New PdfTrueTypeFont(New Font("Alef", 16.0F, FontStyle.Bold))

If there is any question, welcome to write back.
Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Thu Oct 25, 2018 2:43 am

Dear Nir Berger,

Greetings from E-iceblue.
Has your issue been fixed? Thanks in advance for your valuable feedback and time.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF