I'm converting HTML to PDF as below:
public const string PdfDocumentHeaderHtml = @"<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta charset='utf-16' />
<title></title>
</head>
<body>
<table>
<tr>
<td colspan='3'>
<span Style='font-family:Arial;font-size:10pt;font-weight:bold;'>{0}</span>
<br/>
<br/>
<span class='pageHeaderText'>{1}</span>
</td>
<td colspan='1'>
<span><img src='' width='150' height='90' alt='NOS'/></span>
</td>
</tr>
</table>
</body>
</html>";
And save to PDF using the below code:
public override void OnCreatePDF(PdfWriter writer, Document document)
{
iTextSharp.text.FontFactory.Register(@"C:\Windows\Fonts\arial.ttf", "Arial");
base.OnCreatePDF(writer, document);
if (writer == null)
throw new ArgumentNullException("writer");
if (document == null)
throw new ArgumentNullException("document");
var headerHtml = string.Format(Constants.NosPdfDocumentHeaderHtml, Urn, Title);
var providers = new Dictionary<string, Object> { { HTMLWorker.IMG_BASEURL, string.Format(Constants.HeaderImageLocation, SiteUrlForHeaderImage) } };
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(headerHtml), null, providers);
foreach (IElement htmlElement in htmlarraylist)
{
document.Add(htmlElement);
document.Add(new LineSeparator((float)0.90, 100, new BaseColor(0, 112, 192, 0), 0, 0));
}
}
I want to set Font-Family:Arial for the PDF but the problem is, when I see the PDF-File properties, it says Helvetica is used.
I think I need to download Adobe Font Metric file (arial.afm file) and set this font family (instead of arial.ttf) for use with pdf. But I don't know how to do it.
Could you please advice?
Thanks,
In the comment section, you are asking for an alternative to add a table structure to a document.
That's easy with PdfPTable
. For instance, if I want to create a table with 3 columns, I do:
PdfPTable table = new PdfPTable(3);
I want to span 100% of the available width between the margins of the page, so I do:
table.WidthPercentage = 100;
I want the first column to be twice as wide as column two and three, so I do:
table.SetWidths(new int[]{2, 1, 1});
Now I add cells:
PdfPCell cell;
cell = new PdfPCell(new Phrase("Table 1"));
cell.Colspan = 3;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
Finally, I add the table to the Document
:
document.Add(table);
And that's it.