css添加到pdf文档itextsharp

css添加到pdf文档itextsharp

本文介绍了如何将bootstrap.css添加到pdf文档itextsharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以此
为基础
i有下一个代码

took as a basis for thishttp://dangtrung87.blogspot.com/2013/07/asp-generate-pdf-with-itextsharp.htmli have next code

  string htmlText = RenderViewToString(this.ControllerContext, "report", null, true);
       htmlText = System.Text.RegularExpressions.Regex.Replace(htmlText, @"\s+", " ");
       htmlText = htmlText.Replace("\n", "").Replace("\r","").Trim();

        //Generate PDF
        using (var document = new Document(PageSize.A4, 40, 40, 40, 40))
        {

            htmlText = FormatImageLinks(htmlText);

            //define output control HTML
            var memStream = new MemoryStream();
            TextReader xmlString = new StringReader(htmlText);

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

            //open doc
            document.Open();
            string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");

          // Set factories
            var htmlContext = new HtmlPipelineContext(null);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

            // Set css
            ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

            IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
            cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/bootstrap.css"), true);

            cssResolver.AddCss(".shadow {background-color: #ebdddd; }", true);

            var worker = new XMLWorker(pipeline, true);
            var xmlParse = new XMLParser(true, worker);



            xmlParse.Parse(xmlString);
            xmlParse.Flush();

            document.Close();
            document.Dispose();



            return File(memStream.ToArray(), "application/pdf", "test.pdf");

        }

我这里有错误xmlParse.Parse(xmlString);
附加信息:输入字符串无效。

I have error here xmlParse.Parse(xmlString);Additional information: Input string was invalid.

如果我更改
cssResolver.AddCssFile(HttpContext.Server.MapPath(〜/ Content / bootstrap.css), false );我没有错误。

if i changecssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/bootstrap.css"), false); i have no error.

我有pdf文件,但只能使用cssResolver.AddCss(。shadow {background-color:#ebdddd;},true);

And i have pdf file but work only cssResolver.AddCss(".shadow {background-color: #ebdddd; }", true);

bootstrap样式不起作用(

bootstrap style is not work (

如何正确添加这个??

how to correct add this ??

推荐答案

您遇到此错误的原因是因为cssResolver必须在管道中使用之前进行初始化,请尝试更改这些行

The reason you have this error is because cssResolver must be initialized before being used in pipeline, try changing these lines



        // Set css
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
        cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/bootstrap.css"), true);
        cssResolver.AddCss(".shadow {background-color: #ebdddd; }", true);

by



        // Set css
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/bootstrap.css"), true);
        cssResolver.AddCss(".shadow {background-color: #ebdddd; }", true);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

试试吧,对我来说很好。
问候

Try it, works fine for me.Regards

这篇关于如何将bootstrap.css添加到pdf文档itextsharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:26