c# - 如何将bootstrap.css添加到pdf文档itextsharp

以此为基础
http://dangtrung87.blogspot.com/2013/07/asp-generate-pdf-with-itextsharp.html
我有下一个代码

  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);
附加信息:输入字符串无效。

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

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

引导程序样式不起作用(

如何正确添加这个?

最佳答案

出现此错误的原因是,在管道中使用cssResolver之前必须对其进行初始化,请尝试更改这些行



        // 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);


通过



        // 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)));


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

本文翻译自 https://stackoverflow.com/questions/28209106/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 c# css twitter-bootstrap itextsharp


相关文章:

c# - 限制HttpModule仅处理某些请求

c# - Monitor.Wait确保重新读取字段吗?

c# - 当设备在线时,Xamarin.Forms iOS抛出NSURLErrorDomain“互联网连接脱机”

c# - 无法获得实际错误以响应REST请求。 Fiddler向我展示了更多错误详细信息。在.NET中,我得到的只是500-内部服务器错误

c# - 如何在Silverlight TextBlock中用省略号截断字符串?

c# - 如何在Windows中使用C#调用屏幕保护程序?

c# - 如何以编程方式获取主Dns后缀?

c# - 如何从抽象基本控制器访问HttpContext?

c# - SQL Server用户实例中有哪些使它们在非Express Edition中无法实现的?

c# - C#Windows窗体控件的图像?