How to set PDF paragraph or font line-height with iTextSharp?
Line spacing in terms of typography is called leading. If you can use line spacing, you can use Paragraph.Leading or Paragraph.LeadingMultiplier. See http://itextsharp.sourceforge.net/tutorial/ch02.html#phrase
iTextSharp - Paragraph Line-Height
You are adding para
in text mode instead of adding it in composite mode. Text mode means that the leading of the PdfPCell
will get preference over the leading defined for the Paragraph
. With composite mode, it's the other way around.
You can fix this with a small change:
cell = new PdfPCell();
cell.addElement(para);
tempTable.AddCell(cell);
Using the addElement()
method makes cell
switch from text mode to composite mode.
Changing text line spacing
According to the PDF specification, the distance between the baseline of two lines is called the leading. In iText, the default leading is 1.5 times the size of the font. For instance: the default font size is 12 pt, hence the default leading is 18.
You can change the leading of a Paragraph
by using one of the other constructors. See for instance: public Paragraph(float leading, String string, Font font)
You can also change the leading using one of the methods that sets the leading:
paragraph.SetLeading(fixed, multiplied);
The first parameter is the fixed leading: if you want a leading of 15 no matter which font size is used, you can choose fixed = 15 and multiplied = 0.
The second parameter is a factor: for instance if you want the leading to be twice the font size, you can choose fixed = 0 and multiplied = 2. In this case, the leading for a paragraph with font size 12 will be 24, for a font size 10, it will be 20, and son on.
You can also combine fixed and multiplied leading.
Set line spacing when using XMLWorker to parse HTML to PDF - ITextSharp C#
If you want to have a different line-height for different paragraphs, you have to define a different value for the line-height
attribute in your CSS. I have made a very simple example with some very simple inline CSS:
As you can see, the line-height of the paragraph starting with Non eram nescius is 16pt. As I use the default font which is 12 pt Helvetica. The paragraph looks fine.
For the paragraph that starts with Contra quos omnis, I use a line-height of 25pt and you see that there are big gaps between the lines.
For the paragraph that starts with Sive enim ad, I use a line-height of 13pt which is only 1 pt more than the font height. The lines are very close together for this paragraph.
It doesn't matter where you define the line-height
. Your options are to define it inline in the tag, in the <head>
section of your HTML or in an external CSS file that is either referenced from the header of your HTML or loaded into XML Worker separately. Whatever you like most is OK.
Reduce paragraph line break height on iTextSharp
When using a table, you need to set the leading on the cell itself. However, you'll see that the Leading
property is read-only so instead you'll need to use the SetLeading()
method which takes two values, the first is the fixed leading and the second is the multiplied leading. According to this post here:
Multiplied basically means, the larger the font, the larger the leading. Fixed means the same leading for any font size.
To shrink the leading to 80% you'd use:
Dim P1 As New Paragraph("It was the best of times, it was the worst of times")
Dim C1 As New PdfPCell(P1)
C1.SetLeading(0, 0.8)
EDIT
Sorry, I saw "Column" and my coffee-lacking brain went to tables.
For a ColumnText
you should be able to use the Paragraph's leading values just fine.
Dim cb = writer.DirectContent
Dim ct As New ColumnText(cb)
ct.SetSimpleColumn(0, 0, 200, 200)
Dim P1 As New Paragraph("It was the best of times, it was the worst of times")
''//Disable fixed leading
P1.Leading = 0
''//Set a font-relative leading
P1.MultipliedLeading = 0.8
ct.AddElement(P1)
ct.Go()
On my machine running iTextSharp 5.1.2.0 this produces two lines of text that are slightly squished together.
Related Topics
When Do You Use Extension Methods, Ext. Methods VS. Inheritance
Bind 5 Items in Each Row of Repeater
Asp.Net Jquery Ajax Calling Code-Behind Method
How to Open PDF File in a New Tab or Window Instead of Downloading It (Using ASP.NET)
More Elegant Exception Handling Than Multiple Catch Blocks
Application Can't Scaffold Items
How to Return Dynamic Object from SQL Query
Error Handling When Downloading File from ASP.NET Web Handler (.Ashx)
Secure Way of Inserting Dynamic Values in External JavaScript Files
Get List of Certificates from the Certificate Store in C#
SQL Injections with Replace Single-Quotation and Validate Integers
How to Explain C++ Pointers to a C#/Java Developer
ASP.NET 5 Add Wcf Service Reference
C# Datetime.Ticks Equivalent in Java
Scope_Identity() Always Returning 0
C#/.Net Analysis Tool to Find Race Conditions/Deadlocks
Hmc Sha1 Hash - C# Producing Different Hash Output Than Ruby