/** * 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); }
public static ICssFile GetCSS(Stream inp) { ICssFile cssFile = null; if (null != inp) { CssFileProcessor cssFileProcessor = new CssFileProcessor(); try { int i = -1; while (-1 != (i = inp.ReadByte())) { cssFileProcessor.Process((char)i); } cssFile = new CSSFileWrapper(cssFileProcessor.GetCss(), true); } catch (IOException e) { throw new RuntimeWorkerException(e); } finally { try { inp.Close(); } catch (IOException e) { throw new RuntimeWorkerException(e); } } } return(cssFile); }
public static ElementList ParseToElementList(string html, string css) { ICSSResolver iCSSResolver = new StyleAttrCSSResolver(); if (css != null) { ICssFile cSS = XMLWorkerHelper.GetCSS(new MemoryStream(Encoding.Default.GetBytes(css))); iCSSResolver.AddCss(cSS); } MyFontsProvider fontProvider = new MyFontsProvider(); CssAppliers cssAppliers = new CssAppliersImpl(fontProvider); //CssAppliers cssAppliers = new CssAppliersImpl(FontFactory.FontImp); HtmlPipelineContext htmlPipelineContext = new HtmlPipelineContext(cssAppliers); htmlPipelineContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); htmlPipelineContext.AutoBookmark(false); ElementList elementList = new ElementList(); ElementHandlerPipeline next = new ElementHandlerPipeline(elementList, null); HtmlPipeline next2 = new HtmlPipeline(htmlPipelineContext, next); CssResolverPipeline pipeline = new CssResolverPipeline(iCSSResolver, next2); XMLWorker listener = new XMLWorker(pipeline, true); XMLParser xMLParser = new XMLParser(listener); //因为XMLWork不支持html的单标签,所以要对单标签进行过滤。不然就会报错:Invalid nested tag div found, expected closing tag br html = html.Replace("<br>", "").Replace("<hr>", "").Replace("<img>", "").Replace("<param>", "") .Replace("<link>", ""); //xMLParser.Parse(new MemoryStream(Encoding.Default.GetBytes(html))); xMLParser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(html))); return(elementList); }
virtual public void Add(ICssFile css) { if (css != null) { this.files.Add(css); } }
/** * @return the default css file. */ public ICssFile GetDefaultCSS() { if (null == defaultCssFile) { Stream inp = Assembly.GetExecutingAssembly().GetManifestResourceStream("iTextSharp.tool.xml.css.default.css"); if (null != inp) { CssFileProcessor cssFileProcessor = new CssFileProcessor(); int i = -1; try { while (-1 != (i = inp.ReadByte())) { cssFileProcessor.Process((char)i); } defaultCssFile = new CSSFileWrapper(cssFileProcessor.GetCss(), true); } catch (IOException e) { throw new RuntimeWorkerException(e); } finally { try { inp.Close(); } catch (IOException e) { throw new RuntimeWorkerException(e); } } } } return(defaultCssFile); }
virtual public ICssFile GetDefaultCSS() { if (defaultCssFile == null) { defaultCssFile = GetCSS(Assembly.GetExecutingAssembly().GetManifestResourceStream("iTextSharp.tool.xml.css.default.css")); } return(defaultCssFile); }
virtual public void ParseCSS() { retriever.ProcessFromStream(File.OpenRead(RESOURCES + "/css/test.css"), proc); ICssFile file = proc.GetCss(); IDictionary <String, String> map = file.Get("body"); Assert.IsTrue(map.ContainsKey("margin"), "margin not found."); Assert.AreEqual("20px", map["margin"], "Value for margin not correct."); }
virtual public void ParseCSS() { retriever.ProcessFromStream(File.OpenRead(RESOURCES + "/css/test.css"), proc); ICssFile file = proc.GetCss(); IList <CssRule> rules = file.Get(new Tag("body")); Assert.IsTrue(rules.Count == 1); Assert.IsTrue(rules[0].NormalDeclarations.ContainsKey("margin"), "margin not found."); Assert.AreEqual("20px", rules[0].NormalDeclarations["margin"], "Value for margin not correct."); }
/** * @param file the CssFile * */ public CssStateController(ICssFile file) { this.css = file; utils = CssUtils.GetInstance(); buffer = new StringBuilder(); commentStart = new CommentStart(this); commentEnd = new CommentEnd(this); commentInside = new CommentInside(this); unknown = new Unknown(this); properties = new Properties(this); rule = new Rule(this); current = unknown; }
/* * (non-Javadoc) * @see com.itextpdf.tool.xml.pipeline.css.CSSResolver#addCss(java.lang.String, java.lang.String) */ public void AddCss(String content, Encoding charSet, bool isPersistent) { CssFileProcessor proc = new CssFileProcessor(); try { retrieve.ProcessFromStream(new MemoryStream(charSet.GetBytes(content)), proc); ICssFile css = proc.GetCss(); css.IsPersistent(isPersistent); this.cssFiles.Add(css); } catch (IOException e) { throw new CssResolverException(e); } }
/** * Add a file to the CssFiles Collection. * * @param href the path, if it starts with http we try to retrieve the file * from the net, if not we try a normal file operation. */ public void AddCssFile(String href, bool isPersistent) { CssFileProcessor cssFileProcessor = new CssFileProcessor(); try { retrieve.ProcessFromHref(href, cssFileProcessor); } catch (IOException e) { throw new CssResolverException(e); } ICssFile css = cssFileProcessor.GetCss(); css.IsPersistent(isPersistent); this.cssFiles.Add(css); }
public void PopulateOneCss(ICssFile cssFile, IDictionary <String, String> aggregatedProps, String selector) { IDictionary <String, String> t = cssFile.Get(selector); IDictionary <String, String> css = new Dictionary <String, String>(); foreach (KeyValuePair <String, String> e in t) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(e.Key); String value = utils.StripDoubleSpacesAndTrim(e.Value); if (Util.EqualsIgnoreCase(CSS.Property.BORDER, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "margin-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_WIDTH, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-width")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_STYLE, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-style")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_COLOR, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-color")); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "padding-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.FONT, key)) { CssUtils.MapPutAll(css, utils.ProcessFont(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.LIST_STYLE, key)) { CssUtils.MapPutAll(css, utils.ProcessListStyle(value)); } else { css[key] = value; } } CssUtils.MapPutAll(aggregatedProps, css); }
/// <summary> /// Creates a <see cref="PdfTable"/> object from specified <b>HTML</b> code. /// </summary> /// <param name="html">A reference to input html code to convert.</param> /// <param name="css">A reference to css styles to apply.</param> /// <param name="config">Table configuration reference.</param> /// <returns> /// A new <see cref="PdfPTable"/> that contains a /// </returns> public static PdfTable CreateFromHtml(string html, string css = default, PdfTableConfig config = null) { var hasCss = !string.IsNullOrEmpty(css); if (!hasCss) { css = " "; } // css StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(); ICssFile cssFile = XMLWorkerHelper.GetCSS(css.AsStream()); cssResolver.AddCss(cssFile); // html XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts))); CssAppliers cssAppliers = new CssAppliersImpl(fontProvider); HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); // pipelines ElementList elements = new ElementList(); ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null); HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline); // XML Worker XMLWorker worker = new XMLWorker(cssPipeline, true); XMLParser parser = new XMLParser(worker, Encoding.UTF8); parser.Parse(html.AsStream(Encoding.UTF8)); PdfPTable nativeTable = (PdfPTable)elements[0]; return(new PdfTable(nativeTable, config)); }
/** * */ public CssFileProcessor() { this.css = new CssFileImpl(); this.controller = new CssStateController(css); }
/** * Construct a new CssFilesImpl with the given css file. * @param css the css file */ public CssFilesImpl(ICssFile css) : this() { this.Add(css); }
public ICssFile GetDefaultCSS() { if (defaultCssFile == null) { Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("iTextSharp.tool.xml.css.default.css"); if (input != null) { CssFileProcessor cssFileProcessor = new CssFileProcessor(); int i = -1; try { while (-1 != (i = input.ReadByte())) { cssFileProcessor.Process((char)i); } defaultCssFile = new CSSFileWrapper(cssFileProcessor.GetCss(), true); } catch (IOException e) { throw new RuntimeWorkerException(e); } finally { try { input.Close(); } catch (IOException e) { throw new RuntimeWorkerException(e); } } } } return defaultCssFile; }
public ICssFile GetDefaultCSS() { if (defaultCssFile == null) { defaultCssFile = GetCSS(Assembly.GetExecutingAssembly().GetManifestResourceStream("iTextSharp.tool.xml.css.default.css")); } return defaultCssFile; }
/** * Add a file to the CssFiles Collection. * @param file the CssFile to add. */ virtual public void AddCss(ICssFile file) { this.cssFiles.Add(file); }
public void PopulateOneCss(ICssFile cssFile, IDictionary<String, String> aggregatedProps, String selector) { IDictionary<String, String> t = cssFile.Get(selector); IDictionary<String, String> css = new Dictionary<String, String>(); foreach (KeyValuePair<String, String> e in t) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(e.Key); String value = utils.StripDoubleSpacesAndTrim(e.Value); if (Util.EqualsIgnoreCase(CSS.Property.BORDER, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN, key)) { IDictionary<String, String> margins = utils.ParseBoxValues(value, "margin-", ""); foreach (String marginKey in margins.Keys) { if (!css.ContainsKey(marginKey)) { css.Add(marginKey, margins[marginKey]); } } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_WIDTH, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-width")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_STYLE, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-style")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_COLOR, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-color")); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING, key)) { IDictionary<String, String> paddings = utils.ParseBoxValues(value, "padding-", ""); foreach (String paddingKey in paddings.Keys) { if (!css.ContainsKey(paddingKey)) { css.Add(paddingKey, paddings[paddingKey]); } } //CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "padding-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.FONT, key)) { CssUtils.MapPutAll(css, utils.ProcessFont(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.LIST_STYLE, key)) { CssUtils.MapPutAll(css, utils.ProcessListStyle(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.BACKGROUND, key)) { IDictionary<String, String> backgroundStyles = utils.ProcessBackground(value); foreach (String backgroundKey in backgroundStyles.Keys) { if (!css.ContainsKey(backgroundKey)) { css.Add(backgroundKey, backgroundStyles[backgroundKey]); } } } else { css[key] = value; } } CssUtils.MapPutAll(aggregatedProps, css); }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssFiles#addFile(com.itextpdf.tool.xml.css.CssFile) */ public void Add(ICssFile css) { this.files.Add(css); }
/** * Add a file to the CssFiles Collection. * @param file the CssFile to add. */ public void AddCss(ICssFile file) { this.cssFiles.Add(file); }
public void PopulateOneCss(ICssFile cssFile, IDictionary<String, String> aggregatedProps, String selector) { IDictionary<String, String> t = cssFile.Get(selector); IDictionary<String, String> css = new Dictionary<String, String>(); foreach (KeyValuePair<String, String> e in t) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(e.Key); String value = utils.StripDoubleSpacesAndTrim(e.Value); if (Util.EqualsIgnoreCase(CSS.Property.BORDER, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "margin-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_WIDTH, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-width")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_STYLE, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-style")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_COLOR, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-color")); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "padding-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.FONT, key)) { CssUtils.MapPutAll(css, utils.ProcessFont(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.LIST_STYLE, key)) { CssUtils.MapPutAll(css, utils.ProcessListStyle(value)); } else { css[key] = value; } } CssUtils.MapPutAll(aggregatedProps, css); }
public void Add(ICssFile css) { if (css != null) { this.files.Add(css); } }
/** * @param css * @param b */ public CSSFileWrapper(ICssFile css, bool b) { this.css = css; this.persistent = b; }
virtual public void PopulateOneCss(ICssFile cssFile, IDictionary <String, String> aggregatedProps, String selector) { IDictionary <String, String> t = cssFile.Get(selector); IDictionary <String, String> css = new Dictionary <String, String>(); foreach (KeyValuePair <String, String> e in t) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(e.Key); String value = utils.StripDoubleSpacesAndTrim(e.Value); if (Util.EqualsIgnoreCase(CSS.Property.BORDER, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN, key)) { IDictionary <String, String> margins = utils.ParseBoxValues(value, "margin-", ""); foreach (String marginKey in margins.Keys) { if (!css.ContainsKey(marginKey)) { css.Add(marginKey, margins[marginKey]); } } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_WIDTH, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-width")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_STYLE, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-style")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_COLOR, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-color")); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING, key)) { IDictionary <String, String> paddings = utils.ParseBoxValues(value, "padding-", ""); foreach (String paddingKey in paddings.Keys) { if (!css.ContainsKey(paddingKey)) { css.Add(paddingKey, paddings[paddingKey]); } } //CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "padding-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.FONT, key)) { CssUtils.MapPutAll(css, utils.ProcessFont(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.LIST_STYLE, key)) { CssUtils.MapPutAll(css, utils.ProcessListStyle(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.BACKGROUND, key)) { IDictionary <String, String> backgroundStyles = utils.ProcessBackground(value); foreach (String backgroundKey in backgroundStyles.Keys) { if (!css.ContainsKey(backgroundKey)) { css.Add(backgroundKey, backgroundStyles[backgroundKey]); } } } else { css[key] = value; } } CssUtils.MapPutAll(aggregatedProps, css); }
public ICssFile GetCSS(Stream inp) { if (null != inp) { CssFileProcessor cssFileProcessor = new CssFileProcessor(); int i = -1; try { while (-1 != (i = inp.ReadByte())) { cssFileProcessor.Process((char) i); } defaultCssFile = new CSSFileWrapper(cssFileProcessor.GetCss(), true); } catch (IOException e) { throw new RuntimeWorkerException(e); } finally { try { inp.Close(); } catch (IOException e) { throw new RuntimeWorkerException(e); } } } return defaultCssFile; }