出于某种原因,我似乎无法在我的 htmlElements 上应用样式。我为测试目的创建了一个样式包,但该样式并未应用于每个 div 元素。谁能发现我做错了什么?
try
{
// set the file name
string file = "C:/MyPdf.pdf";
// create a pdf document
Document document = new Document();
// set the page size, set the orientation
document.SetPageSize(PageSize.A4);
// create a writer instance
PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(file, FileMode.Create));
// open the document
document.Open();
// THIS STYLE IS SET FOR TESTING PURPOSES
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.DIV, HtmlTags.BGCOLOR, "#ff0000");
// html pagina inlezen
string htmlText = File.ReadAllText("C:\\afl.html");
// html pagina parsen in een arraylist van IElements
List<IElement> htmlElements = HTMLWorker.ParseToList(new StringReader(htmlText), styles);
// add the IElements to the document
for (int i = 0; i < htmlElements.Count; i++)
{
// cast the element
IElement htmlElement = ((IElement)htmlElements[i]);
document.Add(htmlElement);
}
// close the document
document.Close();
// open the pdf document
//Process.Start(file);
}
catch (Exception ex)
{
var derp = ex.Message;
}