我正在使用iTextSharp版本。 5.4.1。我正在尝试从HTML生成PDF。下面粘贴的代码行会出现错误当前上下文中不存在名称“标记”。这适用于itextSharp ver 5.2.1。某些正文可以告诉标记属于版本5.4.1中的哪个类。
document.Add(new Header(**Markup**.HTML_ATTR_STYLESHEET, string.Empty));
代码:
/// <summary>
/// Renders the PDF to an array of bytes
/// </summary>
public byte[] RenderPdf()
{
//Document is inbuilt class, available in iTextSharp
MemoryStream file = new MemoryStream();
Document document = new Document(this.PageSize);
PdfWriter writer = PdfWriter.GetInstance(document, file);
//allow modifications of the document
if (this.BeforeRender is RenderEvent)
{
this.BeforeRender(writer, document);
}
//header
document.Add(new Header(Markup.HTML_ATTR_STYLESHEET, string.Empty));
document.Open();
//render each page that has been added
foreach (HtmlPdfPage page in this._Pages)
{
document.NewPage();
//generate this page of text
MemoryStream output = new MemoryStream();
StreamWriter html = new StreamWriter(output, Encoding.UTF8);
//get the page output
html.Write(string.Concat(HtmlToPdfBuilder.DOCUMENT_HTML_START, page._Html.ToString(), HtmlToPdfBuilder.DOCUMENT_HTML_END));
html.Close();
html.Dispose();
//read the created stream
MemoryStream generate = new MemoryStream(output.ToArray());
StreamReader reader = new StreamReader(generate);
foreach (var item in (IEnumerable)HTMLWorker.ParseToList(reader, this._Styles))
{
document.Add((IElement)item);
}
//cleanup these streams
html.Dispose();
reader.Dispose();
output.Dispose();
generate.Dispose();
}
//after rendering
if (this.AfterRender is RenderEvent)
{
this.AfterRender(writer, document);
}
//return the rendered PDF
document.Close();
return file.ToArray();
}
答案 0 :(得分:0)
试试这个:
private const string HTML_ATTR_STYLESHEET = "stylesheet";
document.Add(new Header( HTML_ATTR_STYLESHEET, string.Empty));