示例#1
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());
        }
示例#2
0
        /// <summary>
        /// Converts a
        /// <see cref="System.String"/>
        /// containing HTML to a
        /// <see cref="System.Collections.IList{E}"/>
        /// of
        /// iText objects (
        /// <see cref="iText.Layout.Element.IElement"/>
        /// instances), using specific
        /// <see cref="ConverterProperties"/>.
        /// </summary>
        /// <param name="html">
        /// the html in the form of a
        /// <see cref="System.String"/>
        /// </param>
        /// <param name="converterProperties">
        /// a
        /// <see cref="ConverterProperties"/>
        /// instance
        /// </param>
        /// <returns>a list of iText building blocks</returns>
        public static IList <IElement> ConvertToElements(String html, ConverterProperties converterProperties)
        {
            ReflectionUtils.ScheduledLicenseCheck();
            IXmlParser    parser = new JsoupHtmlParser();
            IDocumentNode doc    = parser.Parse(html);

            return(Attacher.Attach(doc, converterProperties));
        }
        public virtual void MatchesRootSelectorItemTest()
        {
            CssPseudoClassRootSelectorItem item = CssPseudoClassRootSelectorItem.GetInstance();
            IXmlParser    htmlParser            = new JsoupHtmlParser();
            IDocumentNode documentNode          = htmlParser.Parse("<div><p>Alexander</p><p>Alexander</p></div>");
            INode         headNode = documentNode.ChildNodes()[0];

            NUnit.Framework.Assert.IsTrue(item.Matches(headNode));
        }
        public virtual void MatchesFirstOfTypeSelectorItemTestNotTaggedText()
        {
            CssPseudoClassFirstOfTypeSelectorItem item = CssPseudoClassFirstOfTypeSelectorItem.GetInstance();
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode documentNode = htmlParser.Parse("Some text!");
            INode         bodyNode     = documentNode.ChildNodes()[0].ChildNodes()[1];
            INode         divNode      = bodyNode.ChildNodes()[0];

            NUnit.Framework.Assert.IsFalse(item.Matches(divNode));
        }
        public virtual void MatchesEmptySelectorItemSpaceTest()
        {
            CssPseudoClassEmptySelectorItem item = CssPseudoClassEmptySelectorItem.GetInstance();
            IXmlParser    htmlParser             = new JsoupHtmlParser();
            IDocumentNode documentNode           = htmlParser.Parse("<div> </div>");
            INode         bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
            INode         divNode  = bodyNode.ChildNodes()[0];

            NUnit.Framework.Assert.IsFalse(item.Matches(divNode));
        }
        public virtual void MatchesNthOfTypeSelectorItemTestBadNodeArgument()
        {
            CssPseudoClassNthOfTypeSelectorItem item = new CssPseudoClassNthOfTypeSelectorItem("text");
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode documentNode = htmlParser.Parse("<div><p>Alexander</p><p>Alexander</p></div>");
            INode         bodyNode     = documentNode.ChildNodes()[0].ChildNodes()[1];
            INode         divNode      = bodyNode.ChildNodes()[0].ChildNodes()[0];

            NUnit.Framework.Assert.IsFalse(item.Matches(divNode));
        }
示例#7
0
        public virtual void GetCssRuleSetsTest()
        {
            MediaDeviceDescription deviceDescription = new MediaDeviceDescription("all");
            String             html     = "<a id=\"123\" class=\"baz = 'bar'\" style = media= all and (min-width: 600px) />";
            IDocumentNode      node     = new JsoupHtmlParser().Parse(html);
            IList <CssRuleSet> ruleSets = new CssMediaRule("only all and (min-width: 600px) and (min-height: 600px)").GetCssRuleSets
                                              (node, deviceDescription);

            NUnit.Framework.Assert.IsNotNull(ruleSets);
        }
示例#8
0
        /// <summary>
        /// Converts HTML obtained from an
        /// <see cref="System.IO.Stream"/>
        /// to objects that
        /// will be added to a
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// , using specific
        /// <see cref="ConverterProperties"/>
        /// ,
        /// returning a
        /// <see cref="iText.Layout.Document"/>
        /// instance.
        /// </summary>
        /// <param name="html">
        /// the html in the form of a
        /// <see cref="System.String"/>
        /// </param>
        /// <param name="pdfDocument">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// instance
        /// </param>
        /// <param name="converterProperties">
        /// a
        /// <see cref="ConverterProperties"/>
        /// instance
        /// </param>
        /// <returns>
        /// a
        /// <see cref="iText.Layout.Document"/>
        /// instance
        /// </returns>
        public static Document ConvertToDocument(String html, PdfDocument pdfDocument, ConverterProperties converterProperties
                                                 )
        {
            ReflectionUtils.ScheduledLicenseCheck();
            if (pdfDocument.GetReader() != null)
            {
                throw new Html2PdfException(Html2PdfException.PdfDocumentShouldBeInWritingMode);
            }
            IXmlParser    parser = new JsoupHtmlParser();
            IDocumentNode doc    = parser.Parse(html);

            return(Attacher.Attach(doc, pdfDocument, converterProperties));
        }
        private void ConvertToPdfWithCustomRendererAndCompare(String name)
        {
            ConverterProperties properties = new ConverterProperties().SetTagWorkerFactory(new _DefaultTagWorkerFactory_166
                                                                                               ());
            DefaultHtmlProcessor processor = new DefaultHtmlProcessor(properties);
            IXmlParser           parser    = new JsoupHtmlParser();
            String        outPdfPath       = destinationFolder + name + ".pdf";
            PdfDocument   pdfDocument      = new PdfDocument(new PdfWriter(outPdfPath));
            IDocumentNode doc = parser.Parse(new FileStream(sourceFolder + name + ".html", FileMode.Open, FileAccess.Read
                                                            ), properties.GetCharset());
            Document document = processor.ProcessDocument(doc, pdfDocument);

            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, sourceFolder + "cmp_" + name
                                                                             + ".pdf", destinationFolder));
        }
示例#10
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());
        }
        public virtual void TestDisabledSelector()
        {
            String filename = sourceFolder + "disabled.html";
            CssPseudoClassDisabledSelectorItem item = CssPseudoClassDisabledSelectorItem.GetInstance();
            IXmlParser    htmlParser   = new JsoupHtmlParser();
            IDocumentNode documentNode = htmlParser.Parse(new FileStream(filename, FileMode.Open, FileAccess.Read), "UTF-8"
                                                          );
            IElementNode disabledInput = new JsoupElementNode(((JsoupDocumentNode)documentNode).GetDocument().GetElementsByTag
                                                                  ("input").First());
            IElementNode enabledInput = new JsoupElementNode(((JsoupDocumentNode)documentNode).GetDocument().GetElementsByTag
                                                                 ("input")[1]);

            NUnit.Framework.Assert.IsFalse(item.Matches(documentNode));
            NUnit.Framework.Assert.IsTrue(item.Matches(disabledInput));
            NUnit.Framework.Assert.IsFalse(item.Matches(enabledInput));
            NUnit.Framework.Assert.IsFalse(item.Matches(null));
        }
示例#12
0
        /// <summary>
        /// Converts HTML obtained from an
        /// <see cref="System.IO.Stream"/>
        /// to objects that
        /// will be added to a
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// , using specific
        /// <see cref="ConverterProperties"/>
        /// ,
        /// returning a
        /// <see cref="iText.Layout.Document"/>
        /// instance.
        /// </summary>
        /// <param name="htmlStream">
        /// the
        /// <see cref="System.IO.Stream"/>
        /// with the source HTML
        /// </param>
        /// <param name="pdfDocument">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// instance
        /// </param>
        /// <param name="converterProperties">
        /// a
        /// <see cref="ConverterProperties"/>
        /// instance
        /// </param>
        /// <returns>
        /// a
        /// <see cref="iText.Layout.Document"/>
        /// instance
        /// </returns>
        /// <exception cref="System.IO.IOException">Signals that an I/O exception has occurred.</exception>
        public static Document ConvertToDocument(Stream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties
                                                 )
        {
            try
            {
                String licenseKeyClassName        = "iText.License.LicenseKey, itext.licensekey";
                String licenseKeyProductClassName = "iText.License.LicenseKeyProduct, itext.licensekey";
                String licenseKeyFeatureClassName = "iText.License.LicenseKeyProductFeature, itext.licensekey";
                String checkLicenseKeyMethodName  = "ScheduledCheck";
                Type   licenseKeyClass            = GetClass(licenseKeyClassName);
                if (licenseKeyClass != null)
                {
                    Type     licenseKeyProductClass        = GetClass(licenseKeyProductClassName);
                    Type     licenseKeyProductFeatureClass = GetClass(licenseKeyFeatureClassName);
                    Array    array   = Array.CreateInstance(licenseKeyProductFeatureClass, 0);
                    object[] objects = new object[]
                    {
                        Html2PdfProductInfo.PRODUCT_NAME,
                        Html2PdfProductInfo.MAJOR_VERSION,
                        Html2PdfProductInfo.MINOR_VERSION,
                        array
                    };
                    Object     productObject = System.Activator.CreateInstance(licenseKeyProductClass, objects);
                    MethodInfo m             = licenseKeyClass.GetMethod(checkLicenseKeyMethodName);
                    m.Invoke(System.Activator.CreateInstance(licenseKeyClass), new object[] { productObject });
                }
            }
            catch (Exception e)
            {
                if (!Kernel.Version.IsAGPLVersion())
                {
                    throw;
                }
            }
            if (pdfDocument.GetReader() != null)
            {
                throw new Html2PdfException(Html2PdfException.PdfDocumentShouldBeInWritingMode);
            }
            IXmlParser    parser = new JsoupHtmlParser();
            IDocumentNode doc    = parser.Parse(htmlStream, converterProperties != null ? converterProperties.GetCharset(
                                                    ) : null);

            return(Attacher.Attach(doc, pdfDocument, converterProperties));
        }
示例#13
0
        public virtual void BatchConversionTest()
        {
            ConverterProperties properties = new ConverterProperties().SetBaseUri(sourceFolder).SetMediaDeviceDescription
                                                 (new MediaDeviceDescription(MediaType.PRINT));
            FontProvider fontProvider = new DefaultFontProvider(true, false, false);

            fontProvider.AddDirectory(sourceFolder + "fonts/");
            properties.SetFontProvider(fontProvider);
            IHtmlProcessor processor   = new DefaultHtmlProcessor(properties);
            IXmlParser     parser      = new JsoupHtmlParser();
            String         outPdfPath  = destinationFolder + "smashing1.pdf";
            PdfDocument    pdfDocument = new PdfDocument(new PdfWriter(outPdfPath));
            IDocumentNode  doc         = parser.Parse(new FileStream(sourceFolder + "smashing01.html", FileMode.Open, FileAccess.Read
                                                                     ), properties.GetCharset());
            Document document = processor.ProcessDocument(doc, pdfDocument);

            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, sourceFolder + "cmp_smashing1.pdf"
                                                                             , destinationFolder, "diff01_"));
        }
示例#14
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());
        }
 public virtual void DoNotResetFontProviderTest()
 {
     NUnit.Framework.Assert.That(() => {
         FileStream fileInputStream = new FileStream(sourceFolder + "justHelloWorld.html", FileMode.Open, FileAccess.Read
                                                     );
         IXmlParser parser          = new JsoupHtmlParser();
         IDocumentNode documentNode = parser.Parse(fileInputStream, null);
         ConverterProperties converterProperties = new ConverterProperties();
         converterProperties.SetFontProvider(new _DefaultFontProvider_91(false, true, false));
         // Do nothing here. That should result in an exception.
         IHtmlProcessor processor = new DefaultHtmlProcessor(converterProperties);
         Document doc1            = processor.ProcessDocument(documentNode, new PdfDocument(new PdfWriter(new MemoryStream()))
                                                              );
         doc1.Close();
         Document doc2 = processor.ProcessDocument(documentNode, new PdfDocument(new PdfWriter(new MemoryStream()))
                                                   );
         doc2.Close();
         NUnit.Framework.Assert.IsTrue(false, "The test should have failed before that assert, since it's strictly forbidden not to reset the FontProvider instance after each html to pdf conversion."
                                       );
     }
                                 , NUnit.Framework.Throws.InstanceOf <PdfException>())
     ;
 }
示例#16
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());
        }
示例#17
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));
        }