iText 和 iTextSharp 内部通过以下HTMLWorker
方式支持一些非常有限的“样式表”iTextSharp.text.html.simpleparser.StyleSheet
. 这些样式表松散地基于 HTML/CSS 属性,但只是最基本的(想想 HTML 3.2)。
您要做的三件事主要是 (1) 加载字体,(2) 创建StyleSheet
指向该字体的指针,以及 (3)StyleSheet
将HTMLWorker
. 我将从这里的回答中部分删除一些代码。
iTextSharp 不会自动爬取整个系统来寻找字体,因此您需要手动注册它们。(实际上,您可以调用一种方法并告诉 iTextSharp 猜测加载字体,但这要快得多。)
步骤#1,加载字体,在本例中为 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);
步骤#2,创建一个StyleSheet
并将其指向我们的字体。我还将设置一些其他属性来展示它们。
//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");
步骤#3,绑定StyleSheet
到我们的HTMLWorker
//Use our StyleSheet from above when parsing
var elements = HTMLWorker.ParseToList(overviewReader, ST);
下面是一个面向 iTextSharp 5.2.0 的功能齐全的 C# WinForms 应用程序,展示了上述所有内容。
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();
}
}
}