AddCss() public method

public AddCss ( ICssFile file ) : void
file ICssFile
return void
 virtual public void SetUp() {
     StyleAttrCSSResolver css = new StyleAttrCSSResolver();
     css.AddCss("dummy { key1: value1; key2: value2 } .aklass { key3: value3;} #dumid { key4: value4}", true);
     CssResolverPipeline p = new CssResolverPipeline(css, null);
     Tag t = new Tag("dummy");
     t.Attributes["id"] = "dumid";
     t.Attributes["class"] = "aklass";
     WorkerContextImpl context = new WorkerContextImpl();
     p.Init(context);
     IPipeline open = p.Open(context, t, null);
     css2 = t.CSS;
 }
Ejemplo n.º 2
0
        private StyleAttrCSSResolver InitializeCssFiles(string[] cssPaths)
        {
            var cssFiles = new CssFilesImpl();
            cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());

            var cssResolver = new StyleAttrCSSResolver(cssFiles);
            cssResolver.AddCss(@"body { padding: 2px 4px; }", "utf-8", true);

            foreach (var cssPath in cssPaths)
            {
                cssResolver.AddCssFile(cssPath, false);
            }

            return cssResolver;
        }
Ejemplo n.º 3
0
 /**
  * Get a CSSResolver implementation.
  *
  * @param addDefaultCss true if the defaultCss should already be added.
  * @return the default CSSResolver
  *
  */
 public ICSSResolver GetDefaultCssResolver(bool addDefaultCss) {
     ICSSResolver resolver = new StyleAttrCSSResolver();
     if (addDefaultCss) {
         resolver.AddCss(GetDefaultCSS());
     }
     return resolver;
 }
Ejemplo n.º 4
0
        /**
         * Parses an HTML string and a string containing CSS into a list of Element objects.
         * The FontProvider will be obtained from iText's FontFactory object.
         * 
         * @param   html    a String containing an XHTML snippet
         * @param   css     a String containing CSS
         * @return  an ElementList instance
         */
        public static ElementList ParseToElementList(String html, String css) {
            // CSS
            ICSSResolver cssResolver = new StyleAttrCSSResolver();
            if (css != null) {
                ICssFile cssFile = XMLWorkerHelper.GetCSS(new MemoryStream(Encoding.Default.GetBytes(css)));
                cssResolver.AddCss(cssFile);
            }

            // HTML
            CssAppliers cssAppliers = new CssAppliersImpl(FontFactory.FontImp);
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            htmlContext.AutoBookmark(false);

            // Pipelines
            ElementList elements = new ElementList();
            ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
            HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
            CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

            // XML Worker
            XMLWorker worker = new XMLWorker(cssPipeline, true);
            XMLParser p = new XMLParser(worker);
            p.Parse(new MemoryStream(Encoding.Default.GetBytes(html)));

            return elements;
        }
Ejemplo n.º 5
0
        private void processHtml(IElementHandler elementsHandler)
        {
            var cssResolver = new StyleAttrCSSResolver();

            if (CssFilesPath != null && CssFilesPath.Any())
            {
                foreach (var cssFile in CssFilesPath)
                {
                    cssResolver.AddCss(XmlWorkerUtils.GetCssFile(cssFile));
                }
            }

            if (!string.IsNullOrEmpty(InlineCss))
            {
                cssResolver.AddCss(InlineCss, "utf-8", true);
            }

            var htmlContext = new HtmlPipelineContext(new CssAppliersImpl(new UnicodeFontProvider(DefaultFont)));
            if (!string.IsNullOrEmpty(ImagesPath))
            {
                htmlContext.SetImageProvider(new ImageProvider { ImagesPath = ImagesPath });
            }
            htmlContext.CharSet(Encoding.UTF8);

            var tagsProcessorFactory = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
            if (PdfElement != null)
            {
                tagsProcessorFactory.AddProcessor("totalpagesnumber", new TotalPagesNumberXmlWorkerProcessor(PdfElement));
            }

            htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagsProcessorFactory);
            var pipeline = new CssResolverPipeline(cssResolver,
                                                   new HtmlPipeline(htmlContext, new ElementHandlerPipeline(elementsHandler, null)));
            var worker = new XMLWorker(pipeline, parseHtml: true);
            var parser = new XMLParser();
            parser.AddListener(worker);
            parser.Parse(new StringReader(Html));
        }