0

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,

Kate
  • 85
  • 2
  • 8
  • You are using `HTMLWorker` instead of XML Worker. You are not creating a `FontProvider`. Please read my answer to the following question and you'll find some examples: http://stackoverflow.com/questions/26738289/itextsharp-write-html-to-pdf-document/ – Bruno Lowagie Nov 06 '14 at 11:40
  • By the way: there is no such thing as an arial.afm file. That would imply that you can find Arial as a Type1 font somewhere. Also: you are registering arial.ttf, but looking at your HTML, I see that you actually want arialbd.ttf (and you didn't register that font). – Bruno Lowagie Nov 06 '14 at 11:43
  • Thanks for your response, but could you please explain how to create FontProvider? which lines of my code are incorrect? – Kate Nov 06 '14 at 15:25
  • Arial is a font family consisting of at least 4 fonts: Arial (arial.ttf), Arial bold (arialbd.ttf), Arial italic (ariali.ttf) and Arial bolditalic (arialbi.ttf). You register 1 font of the family, instead of 4. That's mistake 1. Mistake 2 is using HTMLWorker instead of XML Worker. Please take a look at the examples I refer to (there's also a FontProvider example). – Bruno Lowagie Nov 06 '14 at 17:25
  • Thanks, I just used HTML to format the content of the PDF. Can I just add text to pdf? How can I format it? Could you please let me know if there are some other examples to use XML Worker? – Kate Nov 07 '14 at 15:59
  • I tried to use XML Worker but have some problems, Could you please advice base on my code? – Kate Nov 07 '14 at 16:04

1 Answers1

0

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno, You are a star, I'll try this – Kate Nov 07 '14 at 16:16
  • By the way, Do you know why I could not use Style='font-family:Arial" on HTML? I registered all 4 Arial Font iTextSharp.text.FontFactory.Register(@"C:\Windows\Fonts\arial.ttf", "Arial"); – Kate Nov 07 '14 at 16:25
  • Yes, you didn't follow my advice in the sense that you didn't try examples such as http://itextpdf.com/sandbox/xmlworker/D06_ParseHtmlFonts which clearly use an `XMLWorkerFontProvider`. I can bring the horse to the water, but I can't make the horse drink... If you don't follow the advice that is given, I'm helpless. – Bruno Lowagie Nov 07 '14 at 16:28
  • Hi Bruno, I have tried PdfPTable, everything is fine except, reading date from a rich text. In PDF, it shows data in HTML format instead of string format. I tried to use XMLWorker but as it is a SharePoint Solution, I need to add iTextSharp.xmlworker.dll to my project which is not working, Could you please give me an example for C# -SharePoint? – Kate Nov 18 '14 at 14:58
  • I added iTextSharp.xmlworker.dll to the references, but the example you have provided itextpdf.com/sandbox/xmlworker/D06_ParseHtmlFonts is not c#, I don't know how to use it. Could you please explain? – Kate Nov 18 '14 at 15:41
  • I'm a Java developer, not a C# developer, but from what other developers tell me it should be very easy to convert the Java code to C#. When in doubt, look at the other examples on StackOverflow for inspiration: http://stackoverflow.com/questions/tagged/xmlworker+itextsharp (or ask a C# developer if you are not familiar with iTextSharp) – Bruno Lowagie Nov 18 '14 at 16:04