Ejemplo n.º 1
0
 /// <summary>Creates a SvgStyleResolver.</summary>
 /// <remarks>
 /// Creates a SvgStyleResolver. This constructor will instantiate its internal style sheet and it
 /// will collect the css declarations from the provided node.
 /// </remarks>
 /// <param name="rootNode">node to collect css from</param>
 /// <param name="context">the processor context</param>
 public SvgStyleResolver(INode rootNode, SvgProcessorContext context)
 {
     // TODO DEVSIX-2060. Fetch default styles first.
     this.deviceDescription = context.GetDeviceDescription();
     CollectCssDeclarations(rootNode, context.GetResourceResolver());
     CollectFonts();
 }
Ejemplo n.º 2
0
        public void ManipulatePdf(string htmlSource, string pdfDest, string resourceLoc, PageSize pageSize, float screenWidth)
        {
            PdfWriter   writer = new PdfWriter(pdfDest);
            PdfDocument pdfDoc = new PdfDocument(writer);

            // Set the result to be tagged
            pdfDoc.SetTagged();
            pdfDoc.SetDefaultPageSize(pageSize);

            ConverterProperties converterProperties = new ConverterProperties();

            // Set media device description details
            MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDescription.SetWidth(screenWidth);
            converterProperties.SetMediaDeviceDescription(mediaDescription);

            FontProvider fp = new DefaultFontProvider();

            // Register external font directory
            fp.AddDirectory(resourceLoc);

            converterProperties.SetFontProvider(fp);
            // Base URI is required to resolve the path to source files
            converterProperties.SetBaseUri(resourceLoc);

            // Create acroforms from text and button input fields
            converterProperties.SetCreateAcroForm(true);

            HtmlConverter.ConvertToPdf(new FileStream(htmlSource, FileMode.Open, FileAccess.Read, FileShare.Read),
                                       pdfDoc, converterProperties);

            pdfDoc.Close();
        }
Ejemplo n.º 3
0
        public virtual void Test06()
        {
            String        htmlFileName = sourceFolder + "html06.html";
            String        cssFileName  = sourceFolder + "css06.css";
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode document     = htmlParser.Parse(new FileStream(htmlFileName, FileMode.Open, FileAccess.Read), "UTF-8"
                                                          );
            CssStyleSheet css     = CssStyleSheetParser.Parse(new FileStream(cssFileName, FileMode.Open, FileAccess.Read));
            IElementNode  element = new JsoupElementNode(((JsoupDocumentNode)document).GetDocument().GetElementsByTag("p"
                                                                                                                      ).First());
            MediaDeviceDescription deviceDescription1 = new MediaDeviceDescription(MediaType.PRINT).SetBitsPerComponent
                                                            (2);
            MediaDeviceDescription deviceDescription2 = new MediaDeviceDescription(MediaType.HANDHELD).SetBitsPerComponent
                                                            (2);
            MediaDeviceDescription deviceDescription3 = new MediaDeviceDescription(MediaType.SCREEN).SetBitsPerComponent
                                                            (1);
            IList <CssDeclaration> declarations1 = css.GetCssDeclarations(element, deviceDescription1);
            IList <CssDeclaration> declarations2 = css.GetCssDeclarations(element, deviceDescription2);
            IList <CssDeclaration> declarations3 = css.GetCssDeclarations(element, deviceDescription3);

            NUnit.Framework.Assert.IsTrue(Enumerable.SequenceEqual(declarations1, declarations2));
            NUnit.Framework.Assert.AreEqual(0, declarations3.Count);
            NUnit.Framework.Assert.AreEqual(1, declarations1.Count);
            NUnit.Framework.Assert.AreEqual("color: red", declarations1[0].ToString());
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new
 /// <see cref="DefaultCssResolver"/>
 /// instance.
 /// </summary>
 /// <param name="treeRoot">the root node</param>
 /// <param name="mediaDeviceDescription">the media device description</param>
 /// <param name="resourceResolver">the resource resolver</param>
 public DefaultCssResolver(INode treeRoot, MediaDeviceDescription mediaDeviceDescription, ResourceResolver
                           resourceResolver)
 {
     this.deviceDescription = mediaDeviceDescription;
     CollectCssDeclarations(treeRoot, resourceResolver, null);
     CollectFonts();
 }
 public DummySvgConverterProperties()
 {
     rendererFactory        = new DummySvgNodeFactory();
     mediaDeviceDescription = new MediaDeviceDescription("");
     baseUri          = "";
     resourceResolver = new ResourceResolver("");
 }
Ejemplo n.º 6
0
        public FileContentResult Imprimir(string registros)
        {
            string html = "";

            if (registros == "TabelaAluno")
            {
                html = RenderRazorViewToString("TabelaAluno", new AlunoDal().ObterTodos().ToList());
            }
            if (registros == "TabelaCidade")
            {
                html = RenderRazorViewToString("TabelaCidade", new CidadeDal().ObterTodos().ToList());
            }
            if (registros == "TabelaAlunoCidade")
            {
                html = RenderRazorViewToString("TabelaAlunoCidade", new CidadeDal().ObterTodos().ToList());
            }

            var properties = new ConverterProperties();
            var baseUrl    = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));

            properties.SetBaseUri(baseUrl);

            MediaDeviceDescription med = new MediaDeviceDescription(MediaType.ALL);

            med.SetOrientation("landscape");
            properties.SetMediaDeviceDescription(med);

            using (var stream = new MemoryStream())
            {
                HtmlConverter.ConvertToPdf(html, stream, properties);

                return(File(stream.ToArray(), "application/pdf"));
            }
        }
Ejemplo n.º 7
0
        public virtual void MediaAppliedToRunningElementsProperties()
        {
            MediaDeviceDescription printMediaDevice    = new MediaDeviceDescription("print");
            ConverterProperties    converterProperties = new ConverterProperties().SetMediaDeviceDescription(printMediaDevice
                                                                                                             );

            RunTest("mediaAppliedToRunningElementsProperties", converterProperties);
        }
Ejemplo n.º 8
0
        // ignore by default

        /* (non-Javadoc)
         * @see com.itextpdf.styledxmlparser.css.CssStatement#getCssRuleSets(com.itextpdf.styledxmlparser.html.node.INode, com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription)
         */
        public override IList <CssRuleSet> GetCssRuleSets(INode node, MediaDeviceDescription deviceDescription)
        {
            IList <CssRuleSet> result = new List <CssRuleSet>();

            foreach (CssStatement childStatement in body)
            {
                result.AddAll(childStatement.GetCssRuleSets(node, deviceDescription));
            }
            return(result);
        }
Ejemplo n.º 9
0
        /// <summary>Gets the CSS rule sets.</summary>
        /// <param name="node">the node</param>
        /// <param name="deviceDescription">the device description</param>
        /// <returns>the css rule sets</returns>
        public virtual IList <CssRuleSet> GetCssRuleSets(INode node, MediaDeviceDescription deviceDescription)
        {
            IList <CssRuleSet> ruleSets = new List <CssRuleSet>();

            foreach (CssStatement statement in statements)
            {
                ruleSets.AddAll(statement.GetCssRuleSets(node, deviceDescription));
            }
            JavaCollectionsUtil.Sort(ruleSets, new CssRuleSetComparator());
            return(ruleSets);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="baseUri">the base URI</param>
        /// <param name="src">the path to the source HTML file</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(String baseUri, String src, String dest)
        {
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(baseUri);
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);

            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open, FileAccess.Read),
                                       new FileStream(dest, FileMode.Create), properties);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Instantiates a new
 /// <see cref="ConverterProperties"/>
 /// instance based on another
 /// <see cref="ConverterProperties"/>
 /// instance
 /// (copy constructor).
 /// </summary>
 /// <param name="other">
 /// the other
 /// <see cref="ConverterProperties"/>
 /// instance
 /// </param>
 public ConverterProperties(iText.Html2pdf.ConverterProperties other)
 {
     this.mediaDeviceDescription = other.mediaDeviceDescription;
     this.fontProvider           = other.fontProvider;
     this.tagWorkerFactory       = other.tagWorkerFactory;
     this.cssApplierFactory      = other.cssApplierFactory;
     this.baseUri        = other.baseUri;
     this.createAcroForm = other.createAcroForm;
     this.outlineHandler = other.outlineHandler;
     this.charset        = other.charset;
     this.metaInfo       = other.metaInfo;
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="url">the URL object for the web page</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(Uri url, String dest)
        {
            ConverterProperties    properties             = new ConverterProperties();
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);

            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            var httpResponse   = (HttpWebResponse)httpWebRequest.GetResponse();

            HtmlConverter.ConvertToPdf(httpResponse.GetResponseStream(),
                                       new FileStream(dest, FileMode.Create), properties);
        }
        private void WritePdfDocument(string html, string css, Stream outputStream)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            MediaDeviceDescription deviceDescription = new MediaDeviceDescription(MediaType.ALL)
                                                       .SetWidth(PageSize.A4.GetWidth())
                                                       .SetHeight(PageSize.A4.GetHeight());

            ConverterProperties converterProperties = new ConverterProperties().SetMediaDeviceDescription(deviceDescription);
            PdfDocument         pdfDoc = new PdfDocument(new PdfWriter(outputStream));
            var doc = new Document(pdfDoc, PageSize.A4, true);

            HtmlConverter.ConvertToPdf(html, doc.GetPdfDocument(), converterProperties);
        }
Ejemplo n.º 14
0
        public void createPdf(String src, String dest)
        {
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(css);
            //FontProvider dfp = new DefaultFontProvider();
            CssStyleCollection style = new Panel().Style;



            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.PRINT);

            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open), new FileStream(dest, FileMode.Create), properties);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Instantiates a new
 /// <see cref="ProcessorContext"/>
 /// instance.
 /// </summary>
 /// <param name="converterProperties">
 /// a
 /// <see cref="iText.Html2pdf.ConverterProperties"/>
 /// instance
 /// </param>
 public ProcessorContext(ConverterProperties converterProperties)
 {
     if (converterProperties == null)
     {
         converterProperties = new ConverterProperties();
     }
     state             = new State();
     deviceDescription = converterProperties.GetMediaDeviceDescription();
     if (deviceDescription == null)
     {
         deviceDescription = MediaDeviceDescription.GetDefault();
     }
     fontProvider = converterProperties.GetFontProvider();
     if (fontProvider == null)
     {
         fontProvider = new DefaultFontProvider();
     }
     tagWorkerFactory = converterProperties.GetTagWorkerFactory();
     if (tagWorkerFactory == null)
     {
         tagWorkerFactory = DefaultTagWorkerFactory.GetInstance();
     }
     cssApplierFactory = converterProperties.GetCssApplierFactory();
     if (cssApplierFactory == null)
     {
         cssApplierFactory = DefaultCssApplierFactory.GetInstance();
     }
     baseUri = converterProperties.GetBaseUri();
     if (baseUri == null)
     {
         baseUri = "";
     }
     outlineHandler = converterProperties.GetOutlineHandler();
     if (outlineHandler == null)
     {
         outlineHandler = new OutlineHandler();
     }
     resourceResolver      = new HtmlResourceResolver(baseUri, this, converterProperties.GetResourceRetriever());
     limitOfLayouts        = converterProperties.GetLimitOfLayouts();
     cssContext            = new CssContext();
     linkContext           = new LinkContext();
     createAcroForm        = converterProperties.IsCreateAcroForm();
     formFieldNameResolver = new FormFieldNameResolver();
     radioCheckResolver    = new RadioCheckResolver();
     immediateFlush        = converterProperties.IsImmediateFlush();
     metaInfo            = converterProperties.GetEventCountingMetaInfo();
     processingInlineSvg = false;
 }
Ejemplo n.º 16
0
        /// <summary>Gets the CSS declarations.</summary>
        /// <param name="node">the node</param>
        /// <param name="deviceDescription">the media device description</param>
        /// <returns>the CSS declarations</returns>
        public virtual IList <CssDeclaration> GetCssDeclarations(INode node, MediaDeviceDescription deviceDescription
                                                                 )
        {
            IList <CssRuleSet> ruleSets = GetCssRuleSets(node, deviceDescription);
            IDictionary <String, CssDeclaration> declarations = new LinkedDictionary <String, CssDeclaration>();

            foreach (CssRuleSet ruleSet in ruleSets)
            {
                PopulateDeclarationsMap(ruleSet.GetNormalDeclarations(), declarations);
            }
            foreach (CssRuleSet ruleSet in ruleSets)
            {
                PopulateDeclarationsMap(ruleSet.GetImportantDeclarations(), declarations);
            }
            return(new List <CssDeclaration>(declarations.Values));
        }
Ejemplo n.º 17
0
        public virtual void Test01()
        {
            String        htmlFileName = sourceFolder + "html01.html";
            String        cssFileName  = sourceFolder + "css01.css";
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode document     = htmlParser.Parse(new FileStream(htmlFileName, FileMode.Open, FileAccess.Read), "UTF-8"
                                                          );
            CssStyleSheet          css = CssStyleSheetParser.Parse(new FileStream(cssFileName, FileMode.Open, FileAccess.Read));
            MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all");
            IElementNode           element           = new JsoupElementNode(((JsoupDocumentNode)document).GetDocument().GetElementsByTag("p"
                                                                                                                                         ).First());
            IList <CssDeclaration> declarations = css.GetCssDeclarations(element, deviceDescription);

            NUnit.Framework.Assert.AreEqual(1, declarations.Count);
            NUnit.Framework.Assert.AreEqual("font-weight: bold", declarations[0].ToString());
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="url">the URL object for the web page</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(Uri url, String dest)
        {
            PdfWriter   writer   = new PdfWriter(dest);
            PdfDocument pdf      = new PdfDocument(writer);
            PageSize    pageSize = new PageSize(850, 1700);

            pdf.SetDefaultPageSize(pageSize);
            ConverterProperties    properties             = new ConverterProperties();
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDeviceDescription.SetWidth(pageSize.GetWidth());
            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            var httpResponse   = (HttpWebResponse)httpWebRequest.GetResponse();

            HtmlConverter.ConvertToPdf(httpResponse.GetResponseStream(), pdf, properties);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="baseUri">the base URI</param>
        /// <param name="src">the path to the source HTML file</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(String baseUri, String src, String dest)
        {
            PdfWriter   writer = new PdfWriter(dest);
            PdfDocument pdf    = new PdfDocument(writer);

            pdf.SetTagged();
            PageSize pageSize = new PageSize(440, 2000);

            pdf.SetDefaultPageSize(pageSize);
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(baseUri);
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDeviceDescription.SetWidth(pageSize.GetWidth());
            properties.SetMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open, FileAccess.Read), pdf, properties);
        }
Ejemplo n.º 20
0
        public virtual void Test07()
        {
            String htmlFileName = sourceFolder + "html07.html";

            byte[] bytes = StreamUtil.InputStreamToArray(new FileStream(htmlFileName, FileMode.Open, FileAccess.Read));
            String html  = iText.IO.Util.JavaUtil.GetStringForBytes(bytes);
            MediaDeviceDescription printDevice   = new MediaDeviceDescription(MediaType.PRINT);
            MediaDeviceDescription screenDevice  = new MediaDeviceDescription(MediaType.SCREEN).SetWidth(1000);
            IList <IElement>       printElements = HtmlConverter.ConvertToElements(html, new ConverterProperties().SetMediaDeviceDescription
                                                                                       (printDevice).SetBaseUri(sourceFolder));
            IList <IElement> screenElements = HtmlConverter.ConvertToElements(html, new ConverterProperties().SetMediaDeviceDescription
                                                                                  (screenDevice).SetBaseUri(sourceFolder));

            NUnit.Framework.Assert.AreEqual(12f, printElements[0].GetProperty <UnitValue>(Property.FONT_SIZE).GetValue(
                                                ), 1e-10f);
            NUnit.Framework.Assert.AreEqual(20f, screenElements[0].GetProperty <UnitValue>(Property.FONT_SIZE).GetValue
                                                (), 1e-10f);
        }
Ejemplo n.º 21
0
        public byte[] Convert(string htmlContent)
        {
            byte[] pdf = null;
            using (var memoryStream = new MemoryStream())
            {
                var writterProperties = new WriterProperties()
                                        .SetFullCompressionMode(true);
                using (PdfWriter writer = new PdfWriter(memoryStream, writterProperties))
                {
                    PdfDocument pdfDoc = new PdfDocument(writer);
                    pdfDoc.SetTagged();

                    PageSize pageSize = new PageSize(900, 1000);
                    pdfDoc.SetDefaultPageSize(pageSize);

                    ConverterProperties converterProperties = new ConverterProperties();
                    converterProperties.SetCreateAcroForm(true);

                    var fp = new DefaultFontProvider(true, false, false);
                    converterProperties.SetFontProvider(fp);

                    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                    converterProperties.SetMediaDeviceDescription(mediaDescription);

                    var                elements        = HtmlConverter.ConvertToElements(htmlContent, converterProperties);
                    Document           document        = new Document(pdfDoc);
                    CJKSplitCharacters splitCharacters = new CJKSplitCharacters();
                    document.SetSplitCharacters(splitCharacters);
                    document.SetProperty(Property.SPLIT_CHARACTERS, splitCharacters);
                    foreach (IElement element in elements)
                    {
                        document.Add((IBlockElement)element);
                    }
                    document.Close();

                    pdf = memoryStream.ToArray();

                    memoryStream.Close();
                    pdfDoc.Close();
                }
            }

            return(pdf);
        }
Ejemplo n.º 22
0
        public virtual void ResponsiveIText()
        {
            PageSize[] pageSizes = new PageSize[] { null, new PageSize(PageSize.A3.GetHeight(), PageSize.A4.GetHeight(
                                                                           )), new PageSize(760, PageSize.A4.GetHeight()), new PageSize(PageSize.A5.GetWidth(), PageSize.A4.GetHeight
                                                                                                                                            ()) };
            String htmlSource = sourceFolder + "responsiveIText.html";

            foreach (PageSize pageSize in pageSizes)
            {
                float?pxWidth = null;
                if (pageSize != null)
                {
                    pxWidth = CssUtils.ParseAbsoluteLength(pageSize.GetWidth().ToString());
                }
                String              outName             = "responsiveIText" + (pxWidth != null ? "_" + (int)(float)pxWidth : "") + ".pdf";
                PdfWriter           writer              = new PdfWriter(destinationFolder + outName);
                PdfDocument         pdfDoc              = new PdfDocument(writer);
                ConverterProperties converterProperties = new ConverterProperties();
                if (pageSize != null)
                {
                    pdfDoc.SetDefaultPageSize(pageSize);
                    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                    mediaDescription.SetWidth((float)pxWidth);
                    converterProperties.SetMediaDeviceDescription(mediaDescription);
                }
                using (FileStream fileInputStream = new FileStream(htmlSource, FileMode.Open, FileAccess.Read)) {
                    HtmlConverter.ConvertToPdf(fileInputStream, pdfDoc, converterProperties);
                }
                pdfDoc.Close();
            }
            foreach (PageSize pageSize in pageSizes)
            {
                float?pxWidth = null;
                if (pageSize != null)
                {
                    pxWidth = CssUtils.ParseAbsoluteLength(pageSize.GetWidth().ToString());
                }
                String outName = "responsiveIText" + (pxWidth != null ? "_" + (int)(float)pxWidth : "") + ".pdf";
                String cmpName = "cmp_" + outName;
                NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + outName, sourceFolder
                                                                                 + cmpName, destinationFolder, "diffResponsive_"));
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Instantiates a new
        /// <see cref="SvgProcessorContext"/>
        /// instance.
        /// </summary>
        /// <param name="converterProperties">
        /// a
        /// <see cref="iText.Svg.Processors.ISvgConverterProperties"/>
        /// instance
        /// </param>
        public SvgProcessorContext(ISvgConverterProperties converterProperties)
        {
            deviceDescription = converterProperties.GetMediaDeviceDescription();
            if (deviceDescription == null)
            {
                deviceDescription = MediaDeviceDescription.GetDefault();
            }
            fontProvider = converterProperties.GetFontProvider();
            if (fontProvider == null)
            {
                fontProvider = new BasicFontProvider();
            }
            String baseUri = converterProperties.GetBaseUri();

            if (baseUri == null)
            {
                baseUri = "";
            }
            resourceResolver = new ResourceResolver(baseUri);
        }
Ejemplo n.º 24
0
        public virtual void Test05()
        {
            String        htmlFileName = sourceFolder + "html05.html";
            String        cssFileName  = sourceFolder + "css05.css";
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode document     = htmlParser.Parse(new FileStream(htmlFileName, FileMode.Open, FileAccess.Read), "UTF-8"
                                                          );
            CssStyleSheet css     = CssStyleSheetParser.Parse(new FileStream(cssFileName, FileMode.Open, FileAccess.Read));
            IElementNode  element = new JsoupElementNode(((JsoupDocumentNode)document).GetDocument().GetElementsByTag("p"
                                                                                                                      ).First());
            MediaDeviceDescription deviceDescription1 = new MediaDeviceDescription(MediaType.PRINT).SetWidth(300).SetHeight
                                                            (301);
            MediaDeviceDescription deviceDescription2 = new MediaDeviceDescription(MediaType.SCREEN).SetWidth(400).SetHeight
                                                            (400);
            IList <CssDeclaration> declarations1 = css.GetCssDeclarations(element, deviceDescription1);
            IList <CssDeclaration> declarations2 = css.GetCssDeclarations(element, deviceDescription2);

            NUnit.Framework.Assert.AreEqual(0, declarations1.Count);
            NUnit.Framework.Assert.AreEqual(1, declarations2.Count);
            NUnit.Framework.Assert.AreEqual("color: red", declarations2[0].ToString());
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Instantiates a new
        /// <see cref="SvgProcessorContext"/>
        /// instance.
        /// </summary>
        /// <param name="converterProperties">
        /// a
        /// <see cref="iText.Svg.Processors.ISvgConverterProperties"/>
        /// instance
        /// </param>
        public SvgProcessorContext(ISvgConverterProperties converterProperties)
        {
            deviceDescription = converterProperties.GetMediaDeviceDescription();
            if (deviceDescription == null)
            {
                deviceDescription = MediaDeviceDescription.GetDefault();
            }
            fontProvider = converterProperties.GetFontProvider();
            if (fontProvider == null)
            {
                fontProvider = new BasicFontProvider();
            }
            IResourceRetriever retriever = null;

            // TODO DEVSIX-3814 change the clause if block to retriever = new ResourceResolver(props.getBaseUri(),
            //  converterProperties.getResourceRetriever()) when the ISvgConverterProperties#getResourceRetriever() is added
            if (converterProperties is SvgConverterProperties)
            {
                retriever = ((SvgConverterProperties)converterProperties).GetResourceRetriever();
            }
            resourceResolver = new ResourceResolver(converterProperties.GetBaseUri(), retriever);
        }
Ejemplo n.º 26
0
        public virtual void Test02()
        {
            String        htmlFileName = sourceFolder + "html02.html";
            String        cssFileName  = sourceFolder + "css02.css";
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode document     = htmlParser.Parse(new FileStream(htmlFileName, FileMode.Open, FileAccess.Read), "UTF-8"
                                                          );
            CssStyleSheet css     = CssStyleSheetParser.Parse(new FileStream(cssFileName, FileMode.Open, FileAccess.Read));
            IElementNode  element = new JsoupElementNode(((JsoupDocumentNode)document).GetDocument().GetElementsByTag("p"
                                                                                                                      ).First());
            MediaDeviceDescription deviceDescription1 = new MediaDeviceDescription(MediaType.PRINT);

            deviceDescription1.SetWidth(525);
            MediaDeviceDescription deviceDescription2 = new MediaDeviceDescription(MediaType.HANDHELD);

            deviceDescription2.SetOrientation("landscape");
            IList <CssDeclaration> declarations1 = css.GetCssDeclarations(element, deviceDescription1);
            IList <CssDeclaration> declarations2 = css.GetCssDeclarations(element, deviceDescription2);

            NUnit.Framework.Assert.IsTrue(Enumerable.SequenceEqual(declarations1, declarations2));
            NUnit.Framework.Assert.AreEqual(1, declarations1.Count);
            NUnit.Framework.Assert.AreEqual("font-weight: bold", declarations1[0].ToString());
        }
Ejemplo n.º 27
0
        public byte[] GenerateHTML(string html)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                PdfWriter   writer = new PdfWriter(ms);//@"D:\Docs\demp.pdf"
                PdfDocument pdfDoc = new PdfDocument(writer);

                // Set the result to be tagged
                pdfDoc.SetTagged();
                pdfDoc.SetDefaultPageSize(PageSize.A4);

                ConverterProperties converterProperties = new ConverterProperties();

                // Set media device description details
                MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                //mediaDescription.SetWidth(screenWidth);
                converterProperties.SetMediaDeviceDescription(mediaDescription);

                FontProvider fp = new DefaultFontProvider();

                // Register external font directory
                ////fp.AddDirectory(resourceLoc);

                ////converterProperties.SetFontProvider(fp);
                // Base URI is required to resolve the path to source files
                ////converterProperties.SetBaseUri(resourceLoc);

                // Create acroforms from text and button input fields
                converterProperties.SetCreateAcroForm(true);

                // HtmlConverter.ConvertToPdf(html, pdfDoc, converterProperties);
                HtmlConverter.ConvertToPdf(html, pdfDoc, converterProperties);

                pdfDoc.Close();
                return(ms.ToArray());
            }
        }
Ejemplo n.º 28
0
        private void Test(String fileName, String elementPath, params String[] expectedStyles)
        {
            String        filePath    = sourceFolder + fileName;
            IXmlParser    parser      = new JsoupHtmlParser();
            IDocumentNode document    = parser.Parse(new FileStream(filePath, FileMode.Open, FileAccess.Read), "UTF-8");
            ICssResolver  cssResolver = new DefaultCssResolver(document, MediaDeviceDescription.CreateDefault(), new ResourceResolver
                                                                   (""));
            CssContext context = new CssContext();

            ResolveStylesForTree(document, cssResolver, context);
            IElementNode element = FindElement(document, elementPath);

            if (element == null)
            {
                NUnit.Framework.Assert.Fail(MessageFormatUtil.Format("Element at path \"{0}\" was not found.", elementPath
                                                                     ));
            }
            IDictionary <String, String> elementStyles     = element.GetStyles();
            ICollection <String>         expectedStylesSet = new HashSet <String>(JavaUtil.ArraysAsList(expectedStyles));
            ICollection <String>         actualStylesSet   = StylesMapToHashSet(elementStyles);

            NUnit.Framework.Assert.IsTrue(SetsAreEqual(expectedStylesSet, actualStylesSet), GetDifferencesMessage(expectedStylesSet
                                                                                                                  , actualStylesSet));
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Creates a new
 /// <see cref="DefaultCssResolver"/>
 /// instance.
 /// </summary>
 /// <param name="treeRoot">the root node</param>
 /// <param name="context">the processor context</param>
 public DefaultCssResolver(INode treeRoot, ProcessorContext context)
 {
     this.deviceDescription = context.GetDeviceDescription();
     CollectCssDeclarations(treeRoot, context.GetResourceResolver(), context.GetCssContext());
     CollectFonts();
 }
Ejemplo n.º 30
0
 /// <summary>Sets the media device description.</summary>
 /// <param name="mediaDeviceDescription">the media device description</param>
 /// <returns>the ConverterProperties instance</returns>
 public virtual iText.Svg.Processors.Impl.SvgConverterProperties SetMediaDeviceDescription(MediaDeviceDescription
                                                                                           mediaDeviceDescription)
 {
     this.mediaDeviceDescription = mediaDeviceDescription;
     return(this);
 }