简体   繁体   中英

itextsharp html to pdf

I want to change some HTML in a pdf. All my html is in HTML string but I don't know how to pass it in correctly within iTextSharp .

public void PDF()
        {

            // Create a doc object
            var doc = new doc(PageSize.A4, 50, 50, 25, 25);

            // Create a new PdfWrite object, writing the output to the file ~/PDFTemplate/SimpleFormFieldDemo.pdf
            var output = new FileStream(Server.MapPath("t.pdf"), FileMode.Create);
            var writer = PdfWriter.GetInstance(doc, output);

            // Open the doc for writing
            doc.Open();

            //Add Wallpaper image to the pdf
            var Wallpaper = iTextSharp.text.Image.GetInstance(Server.MapPath("hfc.png"));
            Wallpaper.SetAbsolutePosition(0, 0);
            Wallpaper.ScaleAbsolute(600, 840);
            doc.Add(Wallpaper);


            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(doc);
            StyleSheet css = new StyleSheet();
            css.LoadTagStyle("body", "face", "Garamond");
            css.LoadTagStyle("body", "encoding", "Identity-H");
            css.LoadTagStyle("body", "size", "12pt");

            hw.Parse(new StringReader(HTML));

            doc.Close();
            Response.Redirect("t.pdf");
        }

If anyone knows how to make this work.. it be good. Thanks Dom

Please download The Best iText Questions on StackOverflow . It's a free ebook, you'll benefit from it.

Once you have downloaded is, go to the section entitled "Parsing XML and XHTML".

Allow me to quote from the answer to this question: RowSpan does not work in iTextSharp?

You are using HTMLWorker instead of XML Worker, and you are right: HTMLWorker has no support for CSS. Saying CSS doesn't work in iTextSharp is wrong. It doesn't work when you use HTMLWorker , but that's documented: the CSS you need works in XML Worker.

Please throw away your code, and start anew using XML Worker.

There are many examples (simple ones as well as complex ones) in the book. Let me give you only one:

using (var fsOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
    using (var stringReader = new StringReader(result))
    {
        var document = new Document();
        var pdfWriter = PdfWriter.GetInstance(document, fsOut);
        pdfWriter.InitialLeading = 12.5f;
        document.Open();
        var xmlWorkerHelper = XMLWorkerHelper.GetInstance();
        var cssResolver = new StyleAttrCSSResolver();
        var xmlWorkerFontProvider = new XMLWorkerFontProvider();
        foreach (string font in fonts)
        {
            xmlWorkerFontProvider.Register(font);
        }
        var cssAppliers = new CssAppliersImpl(xmlWorkerFontProvider);
        var htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        PdfWriterPipeline pdfWriterPipeline = new PdfWriterPipeline(document, pdfWriter);
        HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, pdfWriterPipeline);
        CssResolverPipeline cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
        XMLWorker xmlWorker = new XMLWorker(cssResolverPipeline, true);
        XMLParser xmlParser = new XMLParser(xmlWorker);
        xmlParser.Parse(stringReader);
        document.Close();
    }
}

(Source: iTextSharp XmlWorker: right-to-left )

If you want an easier example, take a look at the answers of these questions:

The code that parses an HTML string and a CSS string to a list of iText(Sharp) elements is as simple as this:

ElementList list = XMLWorkerHelper.parseToElementList(html, css);

You can find more examples on the official iText web site .

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