Esempio n. 1
0
        public virtual void HtmlToElementsTest05()
        {
            String           html = "123";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 1);
        }
Esempio n. 2
0
        public virtual void HtmlObjectMalformedUrlTest()
        {
            String           html = "<object data ='htt://as' type='image/svg+xml'></object>";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 0);
        }
Esempio n. 3
0
        public virtual void HtmlToElementsTest08()
        {
            // this test checks whether iText fails to process meta tag inside body section or not
            String html = "<html><p>Hello world!</p><meta name=\"author\" content=\"Bruno\"><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p></html>";

            HtmlConverter.ConvertToElements(html);
        }
Esempio n. 4
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void ConvertToElementsAndCompare(String name, String sourceFolder, String destinationFolder
                                                        , bool tagged, String fontsFolder)
        {
            String sourceHtml     = sourceFolder + name + ".html";
            String cmpPdf         = sourceFolder + "cmp_" + name + ".pdf";
            String destinationPdf = destinationFolder + name + ".pdf";
            ConverterProperties converterProperties = GetConverterProperties(fontsFolder);
            PdfDocument         pdfDocument         = new PdfDocument(new PdfWriter(destinationPdf));

            if (tagged)
            {
                pdfDocument.SetTagged();
            }
            Document document = new Document(pdfDocument);

            using (FileStream fileInputStream = new FileStream(sourceHtml, FileMode.Open, FileAccess.Read)) {
                foreach (IElement element in HtmlConverter.ConvertToElements(fileInputStream, converterProperties))
                {
                    document.Add((IBlockElement)element);
                }
            }
            System.Console.Out.WriteLine("html: file:///" + UrlUtil.ToNormalizedURI(sourceHtml).AbsolutePath + "\n");
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationPdf, cmpPdf, destinationFolder
                                                                             , "diff_" + name + "_"));
        }
Esempio n. 5
0
        public virtual void HtmlElementsTest07()
        {
            String           html = "<html>Lorem<span>Dolor</span><p>Ipsum</p><p>Sit</p></html>";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 3);
            for (int i = 0; i < lst.Count; i++)
            {
                NUnit.Framework.Assert.IsTrue(lst[i] is Paragraph);
            }
        }
Esempio n. 6
0
        public virtual void HtmlToElementsTest01()
        {
            String           html = "<p>Hello world!</p>";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 1);
            NUnit.Framework.Assert.IsTrue(lst[0] is Paragraph);
            Paragraph p = (Paragraph)lst[0];

            NUnit.Framework.Assert.AreEqual("Hello world!", ((Text)p.GetChildren()[0]).GetText());
            NUnit.Framework.Assert.AreEqual(12f, p.GetProperty <UnitValue>(Property.FONT_SIZE).GetValue(), 1e-10);
        }
Esempio n. 7
0
        public virtual void HtmlToElementsTest04()
        {
            // Handles malformed html
            String           html = "<p>Hello world!<table><td>123";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 2);
            NUnit.Framework.Assert.IsTrue(lst[0] is Paragraph);
            NUnit.Framework.Assert.IsTrue(lst[1] is Table);
            NUnit.Framework.Assert.AreEqual("Hello world!", ((Text)((Paragraph)lst[0]).GetChildren()[0]).GetText());
            NUnit.Framework.Assert.AreEqual("123", ((Text)(((Paragraph)((Table)lst[1]).GetCell(0, 0).GetChildren()[0])
                                                           .GetChildren()[0])).GetText());
        }
Esempio n. 8
0
        public virtual void HtmlToElementsTest03()
        {
            String           html = "<p>Hello world!</p><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p>";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 3);
            NUnit.Framework.Assert.IsTrue(lst[0] is Paragraph);
            NUnit.Framework.Assert.IsTrue(lst[1] is Table);
            NUnit.Framework.Assert.IsTrue(lst[2] is Paragraph);
            NUnit.Framework.Assert.AreEqual("Hello world!", ((Text)((Paragraph)lst[0]).GetChildren()[0]).GetText());
            NUnit.Framework.Assert.AreEqual("123", ((Text)(((Paragraph)((Table)lst[1]).GetCell(0, 0).GetChildren()[0])
                                                           .GetChildren()[0])).GetText());
        }
Esempio n. 9
0
        public virtual void HtmlToElementsTest02()
        {
            String           html = "<table style=\"font-size: 2em\"><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table>";
            IList <IElement> lst  = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.IsTrue(lst.Count == 1);
            NUnit.Framework.Assert.IsTrue(lst[0] is Table);
            Table t = (Table)lst[0];

            NUnit.Framework.Assert.AreEqual(2, t.GetNumberOfRows());
            NUnit.Framework.Assert.AreEqual("123", ((Text)(((Paragraph)t.GetCell(0, 0).GetChildren()[0]).GetChildren()
                                                           [0])).GetText());
            NUnit.Framework.Assert.AreEqual(24f, t.GetProperty <UnitValue>(Property.FONT_SIZE).GetValue(), 1e-10);
        }
        /// <exception cref="System.IO.IOException"/>
        private void WriteToDocument(Document doc, byte[] bytes)
        {
            Stream           @in       = new MemoryStream(bytes);
            IList <IElement> arrayList = HtmlConverter.ConvertToElements(@in);

            foreach (IElement element in arrayList)
            {
                if (element is IBlockElement)
                {
                    doc.Add((IBlockElement)element);
                }
            }
            doc.Close();
        }
Esempio n. 11
0
        public virtual void LeadingInDefaultRenderingModeTest()
        {
            String html = "This text is directly in body. It should have the same default LEADING property as everything else.\n"
                          + "<p>This text is in paragraph.</p>";
            IList <IElement> elements = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.AreEqual(2, elements.Count);
            IElement anonymousParagraph = elements[0];

            // TODO DEVSIX-3873 anonymous paragraph inherited styles should be applied in general way
            NUnit.Framework.Assert.IsNull(anonymousParagraph.GetProperty <Leading>(Property.LEADING));
            IElement normalParagraph = elements[1];

            NUnit.Framework.Assert.AreEqual(new Leading(Leading.MULTIPLIED, 1.2f), normalParagraph.GetProperty <Leading
                                                                                                                >(Property.LEADING));
        }
Esempio n. 12
0
        public virtual void BodyFontFamilyTest()
        {
            String html = "<!DOCTYPE html>\n" + "<html>\n" + "<body style=\"font-family: monospace\">\n" + "This text is directly in body and should be monospaced.\n"
                          + "<p>This text is in paragraph and should be monospaced.</p>\n" + "</body>\n" + "</html>";
            IList <IElement> elements = HtmlConverter.ConvertToElements(html);

            NUnit.Framework.Assert.AreEqual(2, elements.Count);
            IElement anonymousParagraph = elements[0];

            NUnit.Framework.Assert.AreEqual(new String[] { "monospace" }, anonymousParagraph.GetProperty <String[]>(Property
                                                                                                                    .FONT));
            IElement normalParagraph = elements[1];

            NUnit.Framework.Assert.AreEqual(new String[] { "monospace" }, normalParagraph.GetProperty <String[]>(Property
                                                                                                                 .FONT));
        }
Esempio n. 13
0
        public virtual void HtmlToElementsVsHtmlToPdfTest()
        {
            String src                  = sourceFolder + "basic.html";
            String outConvertToPdf      = destinationFolder + "basicCovertToPdfResult.pdf";
            String outConvertToElements = destinationFolder + "basicCovertToElementsResult.pdf";

            HtmlConverter.ConvertToPdf(new FileInfo(src), new FileInfo(outConvertToPdf));
            IList <IElement> elements = HtmlConverter.ConvertToElements(new FileStream(src, FileMode.Open, FileAccess.Read
                                                                                       ));
            Document document = new Document(new PdfDocument(new PdfWriter(outConvertToElements)));

            // In order to collapse margins between the direct children of root element
            // it's required to manually enable collapsing on root element. This is because siblings
            // margins collapsing is controlled by the parent element.
            // This leads to the difference between pure convertToPdf/Document and convertToElements methods.
            document.SetProperty(Property.COLLAPSING_MARGINS, true);
            foreach (IElement elem in elements)
            {
                if (elem is IBlockElement)
                {
                    document.Add((IBlockElement)elem);
                }
                else
                {
                    if (elem is Image)
                    {
                        document.Add((Image)elem);
                    }
                    else
                    {
                        if (elem is AreaBreak)
                        {
                            document.Add((AreaBreak)elem);
                        }
                        else
                        {
                            NUnit.Framework.Assert.Fail("The #convertToElements method gave element which is unsupported as root element, it's unexpected."
                                                        );
                        }
                    }
                }
            }
            document.Close();
            System.Console.Out.WriteLine("html: " + UrlUtil.GetNormalizedFileUriString(src) + "\n");
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outConvertToElements, outConvertToPdf, destinationFolder
                                                                             ));
        }
Esempio n. 14
0
        public virtual void HtmlToElementsTest09()
        {
            //Test OutlineHandler exception throwing

            /*
             * Outlines require a PdfDocument, and OutlineHandler is based around its availability
             * Any convert to elements workflow of course doesn't have a PdfDocument.
             * Instead of throwing an NPE when trying it, the OutlineHandler will check for the existence of a pdfDocument
             * If no PdfDocument is found, the handler will do nothing silently
             */
            String html = "<html><p>Hello world!</p><meta name=\"author\" content=\"Bruno\"><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p></html>";
            ConverterProperties props          = new ConverterProperties();
            OutlineHandler      outlineHandler = new OutlineHandler();

            outlineHandler.PutTagPriorityMapping("h1", 1);
            outlineHandler.PutTagPriorityMapping("h3", 2);
            outlineHandler.PutTagPriorityMapping("p", 3);
            props.SetOutlineHandler(outlineHandler);
            HtmlConverter.ConvertToElements(html);
        }
Esempio n. 15
0
        public virtual void ConvertToElementsAndCreateTwoDocumentsTest()
        {
            String html = "This text is directly in body. It should have the same default LEADING property as everything else.\n"
                          + "<p>This text is in paragraph.</p>";
            IList <IElement> iElementList = HtmlConverter.ConvertToElements(html);

            using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) {
                using (Document document = new Document(pdfDocument)) {
                    AddElementsToDocument(document, iElementList);
                }
            }
            PdfDocument pdfDocument_1 = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            Document    document_1    = new Document(pdfDocument_1);

            AddElementsToDocument(document_1, iElementList);
            // TODO DEVSIX-5753 error should not be thrown here
            Exception e = NUnit.Framework.Assert.Catch(typeof(PdfException), () => document_1.Close());

            NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.PDF_INDIRECT_OBJECT_BELONGS_TO_OTHER_PDF_DOCUMENT
                                            , e.Message);
        }
Esempio n. 16
0
 public virtual void EventGenerationTest()
 {
     Html2ElementsTest.StoreEventsHandler handler = new Html2ElementsTest.StoreEventsHandler();
     try {
         EventManager.GetInstance().Register(handler);
         String           html     = "<table><tr><td>123</td><td><456></td></tr><tr><td>789</td></tr></table><p>Hello world!</p>";
         IList <IElement> elements = HtmlConverter.ConvertToElements(html);
         NUnit.Framework.Assert.AreEqual(1, handler.GetEvents().Count);
         NUnit.Framework.Assert.IsTrue(handler.GetEvents()[0] is PdfHtmlProductEvent);
         SequenceId expectedSequenceId = ((PdfHtmlProductEvent)handler.GetEvents()[0]).GetSequenceId();
         int        validationsCount   = ValidateSequenceIds(expectedSequenceId, elements);
         // Table                                     1
         //      Cell -> Paragraph -> Text [123]      3
         //      Cell -> Paragraph -> Text [456]      3
         //      Cell -> Paragraph -> Text [789]      3
         // Paragraph -> Text [Hello world!]          2
         //--------------------------------------------
         //                                          12
         NUnit.Framework.Assert.AreEqual(12, validationsCount);
     }
     finally {
         EventManager.GetInstance().Unregister(handler);
     }
 }