コード例 #1
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);
        }
コード例 #2
0
        /// <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();
        }
コード例 #3
0
        public void ResourceResolverIncorrectSyntaxTest()
        {
            //this test is inconsistent with java
            String baseUri = sourceFolder;
            String outPdf  = destinationFolder + "resourceResolverIncorrectSyntaxObject.pdf";
            String cmpPdf  = sourceFolder + "cmp_resourceResolverIncorrectSyntaxObject.pdf";
            String inHtml  = sourceFolder + "resourceResolverIncorrectSyntaxObject.html";

            using (FileStream fileInputStream = new FileStream(inHtml, FileMode.Open, FileAccess.Read),
                   fileOutputStream = new FileStream(outPdf, FileMode.Create)) {
                HtmlConverter.ConvertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().SetBaseUri(baseUri));
            }
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diffIncorrectSyntax_"));
        }
コード例 #4
0
        public void ResourceResolverSvgWithImageBackgroundTest()
        {
            //Browsers do not render this
            String baseUri = sourceFolder;
            String outPdf  = destinationFolder + "resourceResolverSvgWithImageBackground.pdf";
            String cmpPdf  = sourceFolder + "cmp_resourceResolverSvgWithImageBackground.pdf";
            String inHtml  = sourceFolder + "resourceResolverSvgWithImageBackground.html";

            using (FileStream fileInputStream = new FileStream(inHtml, FileMode.Open, FileAccess.Read),
                   fileOutputStream = new FileStream(outPdf, FileMode.Create)) {
                HtmlConverter.ConvertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().SetBaseUri(baseUri));
            }
            NUnit.Framework.Assert.IsNull(
                new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diffSvgWithImg_"));
        }
コード例 #5
0
        public virtual void ResourceResolverTest11()
        {
            String outPdf = destinationFolder + "resourceResolverTest11.pdf";
            String cmpPdf = sourceFolder + "cmp_resourceResolverTest11.pdf";

            using (
                FileStream fileInputStream = new FileStream(sourceFolder + "resourceResolverTest11.html", FileMode.Open,
                                                            FileAccess.Read),
                fileOutputStream = new FileStream(outPdf, FileMode.Create))
            {
                HtmlConverter.ConvertToPdf(fileInputStream, fileOutputStream,
                                           new ConverterProperties().SetBaseUri("https://en.wikipedia.org/wiki/Welsh_Corgi"));
                NUnit.Framework.Assert.IsNull(
                    new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diff11_"));
            }
        }
コード例 #6
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));
        }
コード例 #7
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));
        }
コード例 #8
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
                                                                             ));
        }
コード例 #9
0
        public void ResourceResolverTest18()
        {
            String baseUri = sourceFolder;
            String outPdf  = destinationFolder + "resourceResolverTest18.pdf";
            String cmpPdf  = sourceFolder + "cmp_resourceResolverTest18.pdf";

            using (
                FileStream fileInputStream = new FileStream(sourceFolder + "resourceResolverTest18.html", FileMode.Open,
                                                            FileAccess.Read),
                fileOutputStream = new FileStream(outPdf, FileMode.Create))
            {
                HtmlConverter.ConvertToPdf(fileInputStream, fileOutputStream,
                                           new ConverterProperties().SetBaseUri(baseUri));
                NUnit.Framework.Assert.IsNull(
                    new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diff18_"));
            }
        }
コード例 #10
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);
        }
コード例 #11
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void ConvertToPdfAndCompare(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();
            }
            using (FileStream fileInputStream = new FileStream(sourceHtml, FileMode.Open, FileAccess.Read)) {
                HtmlConverter.ConvertToPdf(fileInputStream, pdfDocument, converterProperties);
            }
            System.Console.Out.WriteLine("html: file:///" + UrlUtil.ToNormalizedURI(sourceHtml).AbsolutePath + "\n");
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationPdf, cmpPdf, destinationFolder
                                                                             , "diff_" + name + "_"));
        }
コード例 #12
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);
        }
コード例 #13
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);
     }
 }
コード例 #14
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        /// <exception cref="Javax.Xml.Parsers.ParserConfigurationException"/>
        /// <exception cref="Org.Xml.Sax.SAXException"/>
        public virtual void ConvertToPdfAcroformFlattenAndCompare(String name, String sourceFolder, String destinationFolder
                                                                  , bool tagged)
        {
            String sourceHtml = sourceFolder + name + ".html";

            if (tagged)
            {
                name = name + "Tagged";
            }
            String outPdfPath            = destinationFolder + name + ".pdf";
            String outPdfPathAcro        = destinationFolder + name + "_acro.pdf";
            String outPdfPathFlatted     = destinationFolder + name + "_acro_flatten.pdf";
            String cmpPdfPath            = sourceFolder + "cmp_" + name + ".pdf";
            String cmpPdfPathAcro        = sourceFolder + "cmp_" + name + "_acro.pdf";
            String cmpPdfPathAcroFlatten = sourceFolder + "cmp_" + name + "_acro_flatten.pdf";
            String diff1 = "diff1_" + name;
            String diff2 = "diff2_" + name;
            String diff3 = "diff3_" + name;
            //convert tagged PDF without acroform (from html with form elements)
            PdfWriter   taggedWriter = new PdfWriter(outPdfPath);
            PdfDocument pdfTagged    = new PdfDocument(taggedWriter);

            if (tagged)
            {
                pdfTagged.SetTagged();
            }
            HtmlConverter.ConvertToPdf(new FileStream(sourceHtml, FileMode.Open, FileAccess.Read), pdfTagged, new ConverterProperties
                                           ().SetBaseUri(sourceFolder));
            //convert PDF with acroform
            PdfWriter   writerAcro    = new PdfWriter(outPdfPathAcro);
            PdfDocument pdfTaggedAcro = new PdfDocument(writerAcro);

            if (tagged)
            {
                pdfTaggedAcro.SetTagged();
            }
            ConverterProperties converterPropertiesAcro = new ConverterProperties();

            converterPropertiesAcro.SetBaseUri(sourceFolder);
            converterPropertiesAcro.SetCreateAcroForm(true);
            HtmlConverter.ConvertToPdf(new FileStream(sourceHtml, FileMode.Open, FileAccess.Read), pdfTaggedAcro, converterPropertiesAcro
                                       );
            System.Console.Out.WriteLine("html: file:///" + UrlUtil.ToNormalizedURI(sourceHtml).AbsolutePath + "\n");
            //flatted created tagged PDF with acroform
            PdfDocument document = new PdfDocument(new PdfReader(outPdfPathAcro), new PdfWriter(outPdfPathFlatted));
            PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);

            acroForm.FlattenFields();
            document.Close();
            //compare with cmp
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, cmpPdfPath, destinationFolder
                                                                             , diff1));
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPathAcro, cmpPdfPathAcro, destinationFolder
                                                                             , diff2));
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPathFlatted, cmpPdfPathAcroFlatten,
                                                                             destinationFolder, diff3));
            //compare tags structure if tagged
            if (tagged)
            {
                CompareTagStructure(outPdfPath, cmpPdfPath);
                CompareTagStructure(outPdfPathAcro, cmpPdfPathAcro);
                CompareTagStructure(outPdfPathFlatted, cmpPdfPathAcroFlatten);
            }
        }