0

I am trying to set styling in PDF generated from iTextSharp.

It seems the SetListSymbol is not working for me whenever I am trying to set Symbol to list items.

Below is the code that I used:

var elements = HTMLWorker.ParseToList(overviewReader,null);

foreach (var element in elements)
{
    //element
    var list = element as List;
    if (list != null)
    {
        //list.Symbol.GetImage();
        list.SetListSymbol("\u25A0");
        list.IndentationLeft = 20f;
        doc.Add(list);
    }
    else
    {
        doc.Add(element);
    }
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
D.J
  • 2,534
  • 4
  • 28
  • 43
  • As a side-note, HTMLWorker has been discontinued in favor of [XMLWorker](http://demo.itextsupport.com/xmlworker/) – Alexis Pigeon May 17 '12 at 13:47
  • Can you post the HTML that it doesn't work with. I just tried it with `
    • Test
    ` and it worked as expected. Also, please remember that whatever font you are using must also support that symbol.
    – Chris Haas May 17 '12 at 16:08
  • @ChrisHaas I think your suggestion is possible, however I am struggling to find a nice way of setting font to list items, could you point me a direction please? – D.J May 18 '12 at 01:00

2 Answers2

1

The HTMLWorker within iText and iTextSharp supports some very limited "stylesheets" via iTextSharp.text.html.simpleparser.StyleSheet. These stylesheets are loosely based on HTML/CSS properties but only the most basic (think HTML 3.2).

The three main things that you want to do are (1) load a font, (2) create a StyleSheet pointing to that font and (3) bind the StyleSheet to the HTMLWorker. I'm going to partially lift some code from my answer here.

iTextSharp doesn't automatically spider the entire system looking for fonts so you need to manually register them. (Actually, there is a method that you can call and tell iTextSharp to guess at loading fonts but this is much faster.)

Step #1, load the font, in this case Curlz

//Path to our font
string OurFont = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "CURLZ___.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(OurFont);

Step #2, create a StyleSheet and point it to our font. I'll also set some other properties just to show them off.

//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.FACE, "Curlz MT");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.COLOR, "FF0000");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.SIZE, "50");

Step #3, bind the StyleSheet to our HTMLWorker

//Use our StyleSheet from above when parsing
var elements = HTMLWorker.ParseToList(overviewReader, ST);

Below is a full-working C# WinForms app targeting iTextSharp 5.2.0 that shows off all of the above.

using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication2 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            //Path to our font
            string FontArial = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "CURLZ___.TTF");
            //Register the font with iTextSharp
            iTextSharp.text.FontFactory.Register(FontArial);

            //Create a new stylesheet
            iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
            //Set the default body font to our registered font's internal name
            ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.FACE, "Curlz MT");
            ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.COLOR, "FF0000");
            ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.SIZE, "50");

            //Sample HTML
            var html = @"<ul><li>Test</li></ul>";
            //File to output
            var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
            //Basic PDF creation, nothing special here
            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();
                        //Bind a reader to our HTML
                        using (StringReader overviewReader = new StringReader(html)) {
                            //Use our StyleSheet from above when parsing
                            var elements = HTMLWorker.ParseToList(overviewReader, ST);
                            //Loop through each element
                            foreach (var element in elements) {
                                //See if the element is a list item
                                var list = element as List;
                                if (list != null) {
                                    //Set some properties
                                    list.SetListSymbol("\u25A0");
                                    list.IndentationLeft = 20f;
                                }
                                //Add the element to the document
                                doc.Add(element);
                            }
                        }
                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}
Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
0

List objects = HTMLWorker.ParseToList(new StringReader(format), null);

                foreach (IElement element in objects)
                {

                    List list = new List();
                    list.SetListSymbol("\u2022");
                    list.IndentationLeft = 20f;                        
                    list.Add(element);
                    if (list.Chunks.Count == 0)
                    {
                        doc1.Add(element);
                    }
                    else
                    {
                        doc1.Add(list);
                    }
                }