2

我正在使用 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();

        }
4

1 回答 1

0

尝试这个:

private const string HTML_ATTR_STYLESHEET = "stylesheet";

document.Add(new Header( HTML_ATTR_STYLESHEET, string.Empty));
于 2018-03-02T14:13:33.407 回答