简体   繁体   中英

iTextSharp - html to pdf with background color for font

I'm using iTextSharp to convert HTML to a PDF and email it. It's working fine, except it doesn't seem to support the "background" style. This is the code I'm using to parse the HTML

        private MemoryStream createPDF(string html){
        MemoryStream msOutput = new MemoryStream();
        TextReader reader = new StringReader(html);

        Document document = new Document(PageSize.A4, 30, 30, 30, 30);

        PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

        HTMLWorker worker = new HTMLWorker(document);

        document.Open();

        writer.CloseStream = false;
        worker.StartDocument();

        worker.Parse(reader);

        worker.EndDocument();
        worker.Close();
        document.Close();
        msOutput.Position = 0;
        return msOutput;
}

And the HTML looks something like this:

<p>
Have you <span style="background:red;padding:0.1em 0;" title="This has been brought to your attention.">ever switched your electronic medical records vendor? If so...</span></p>

Which doesn't highlight the text, which I would like it to. However, using "color:red", works, changing the color of the text, but I need it to highlight, like 'background' would do.

Anyway, I've been searching for the last day and can't find a solution. Is this possible? If not, is there a library that supports this? I've also tried the Pechkin library, but the same thing happens.

Zero work is being done in the HTMLWorker , all work is being done in the separate but related project XMLWorker so please move to that. You can then pretty much then just plug this in instead of the HTMLWorker :

//Bind a reader to our text
using (TextReader reader = new StringReader(html)) {
    //Parse the HTML and write it to the document
    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
}

Side note, I'd really recommend not passing around a MemoryStream unless you really need to and instead just pass around the underlying byte array. When passing around the raw stream you have to worrying about the current position, checking if something closed it for you, etc. I'd recommend changing your method to something like this (also note the using pattern):

private byte[] createPDF(string html) {
    //Basic PDF setup
    using (var msOutput = new MemoryStream()) {
        using (var document = new Document(PageSize.A4, 30, 30, 30, 30)) {
            using (var writer = PdfWriter.GetInstance(document, msOutput)) {

                //Open our document for writing
                document.Open();

                //Bind a reader to our text
                using (TextReader reader = new StringReader(html)) {
                    //Parse the HTML and write it to the document
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
                }

                //Close the main document
                document.Close();
            }

            //Return our raw bytes
            return msOutput.ToArray();
        }
    }
}

I think here you have the answer. http://blog.rubypdf.com/itextsharp/tutorial01/index.html A basic tutorial for that. look for the background tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM