-2

I am using ITextSharp and I wanted to include CSS to my PDF. Below is my current code:

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

But the problem is that I lack a reference for ByteArrayInputStream. Even System.IO cannot fix the error. Are there any other workarounds to this?

Thank you very much!

kuujinbo
  • 9,272
  • 3
  • 44
  • 57
JPaulPunzalan
  • 417
  • 1
  • 6
  • 20
  • 1
    That code looks like Java version of iText. Are you sure u need help with C#? If so, can you post your C# code? – Mike Hixson Mar 02 '17 at 04:51
  • If you try to port java samples to .Net, `ByteArrayInputStream` and `ByteArrayOutputStream` usually become `MemoryStream`. – mkl Mar 02 '17 at 05:17
  • @MikeHixson I need help on converting it to **C#** but I'm new to IText. My current code uses `parseXHtml` but does not have the CSS parameter but I realized that I **need** to include CSS in my PDF. The code above is the only snippet that I saw online that incorporates CSS and HTML using `parseXHtml`. – JPaulPunzalan Mar 02 '17 at 07:32
  • @mkl Hi, could you help me on converting the `ByteArrayInputStream` code above into C# including the **Memory Stream**? – JPaulPunzalan Mar 02 '17 at 07:33

1 Answers1

0

A simple, working example to get you started:

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());
        }
    }
}
kuujinbo
  • 9,272
  • 3
  • 44
  • 57