-2

我正在使用ITextSharp,我想将CSS 包含到我的 PDF中。以下是我当前的代码:

ByteArrayInputStream bis = new ByteArrayInputStream(htmlSource.toString().getBytes());  
ByteArrayInputStream cis = new ByteArrayInputStream(cssSource.toString().getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis, cis);

但问题是我缺乏. ByteArrayInputStream甚至System.IO无法修复错误。还有其他解决方法吗?

非常感谢!

4

1 回答 1

0

一个简单的工作示例可以帮助您入门:

var html = "<h1>H1</h1>";
var css = "h1 {font-size: 2em;}";

using (var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
{
    using (var cssStream = new MemoryStream(Encoding.UTF8.GetBytes(css)))
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var document = new Document())
            {
                PdfWriter writer = PdfWriter.GetInstance(
                    document, memoryStream
                );
                document.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(
                    writer, document, htmlStream, cssStream
                );
            }
            File.WriteAllBytes(OUTPUT_FILE, memoryStream.ToArray());
        }
    }
}
于 2017-03-02T21:31:00.543 回答