Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PdfTable"/> class with a native pdf table reference.
        /// </summary>
        /// <param name="reference">A reference to native pdf table.</param>
        /// <param name="configuration">Table configuration reference.</param>
        private PdfTable(PdfPTable reference, PdfTableConfig configuration = null)
        {
            var safeConfiguration = configuration;

            if (configuration == null)
            {
                safeConfiguration = PdfTableConfig.Default;
            }

            Table         = reference;
            Configuration = safeConfiguration.Clone();
        }
Exemple #2
0
        /// <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));
        }