Esempio n. 1
0
        public virtual void SvgCssResolverStylesheetTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupLink = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                .ValueOf(SvgConstants.Tags.LINK), "");
            Attributes linkAttributes = jsoupLink.Attributes();

            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.XMLNS, "http://www.w3.org/1999/xhtml"
                                                                               ));
            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.REL, SvgConstants.Attributes
                                                                               .STYLESHEET));
            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.HREF, "styleSheetWithLinkStyle.css"
                                                                               ));
            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("type", "text/css"));
            JsoupElementNode       node = new JsoupElementNode(jsoupLink);
            SvgConverterProperties scp  = new SvgConverterProperties();

            scp.SetBaseUri(baseUri);
            SvgProcessorContext          processorContext = new SvgProcessorContext(scp);
            SvgStyleResolver             sr           = new SvgStyleResolver(node, processorContext);
            IDictionary <String, String> attr         = sr.ResolveStyles(node, new SvgCssContext());
            IDictionary <String, String> expectedAttr = new Dictionary <String, String>();

            expectedAttr.Put(SvgConstants.Attributes.XMLNS, "http://www.w3.org/1999/xhtml");
            expectedAttr.Put(SvgConstants.Attributes.REL, SvgConstants.Attributes.STYLESHEET);
            expectedAttr.Put(SvgConstants.Attributes.HREF, "styleSheetWithLinkStyle.css");
            expectedAttr.Put("type", "text/css");
            // Attribute from external stylesheet
            expectedAttr.Put(SvgConstants.Attributes.FILL, "black");
            NUnit.Framework.Assert.AreEqual(expectedAttr, attr);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new
        /// <see cref="ImgTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public ObjectTagWorker(IElementNode element, ProcessorContext context)
        {
            this.processUtil = new SvgProcessingUtil();
            //Retrieve object type
            String type = element.GetAttribute(AttributeConstants.TYPE);

            if (IsSvgImage(type))
            {
                String dataValue = element.GetAttribute(AttributeConstants.DATA);
                try {
                    using (Stream svgStream = context.GetResourceResolver().RetrieveResourceAsInputStream(dataValue)) {
                        if (svgStream != null)
                        {
                            SvgConverterProperties props = ContextMappingHelper.MapToSvgConverterProperties(context);
                            if (!context.GetResourceResolver().IsDataSrc(dataValue))
                            {
                                Uri    fullURL = context.GetResourceResolver().ResolveAgainstBaseUri(dataValue);
                                String dir     = FileUtil.ParentDirectory(fullURL);
                                props.SetBaseUri(dir);
                            }
                            res = SvgConverter.ParseAndProcess(svgStream, props);
                        }
                    }
                }
                catch (SvgProcessingException spe) {
                    LOGGER.Error(spe.Message);
                }
                catch (Exception ie) {
                    LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
                                                          , context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA), ie));
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new
        /// <see cref="ImgTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public ObjectTagWorker(IElementNode element, ProcessorContext context)
        {
            image       = null;
            res         = null;
            processUtil = new SvgProcessingUtil();
            //Retrieve object type
            String type = element.GetAttribute(AttributeConstants.TYPE);

            if (IsSvgImage(type))
            {
                //Use resource resolver to retrieve the URL
                try {
                    using (Stream svgStream = context.GetResourceResolver().RetrieveResourceAsInputStream(element.GetAttribute
                                                                                                              (AttributeConstants.DATA))) {
                        if (svgStream != null)
                        {
                            try {
                                SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
                                svgConverterProperties.SetBaseUri(context.GetBaseUri()).SetFontProvider(context.GetFontProvider()).SetMediaDeviceDescription
                                    (context.GetDeviceDescription());
                                res = SvgConverter.ParseAndProcess(svgStream, svgConverterProperties);
                            }
                            catch (SvgProcessingException spe) {
                                LOGGER.Error(spe.Message);
                            }
                        }
                    }
                }
                catch (System.IO.IOException) {
                    LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
                                                          , context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA)));
                }
            }
        }
Esempio n. 4
0
        public virtual void ResolveFontsDefaultUri()
        {
            SvgConverterProperties properties = new SvgConverterProperties();

            properties.SetBaseUri(sourceFolder);
            String fileName = "fontSelectorTest02";

            ConvertToSinglePage(new FileInfo(sourceFolder + fileName + ".svg"), new FileInfo(destinationFolder + fileName
                                                                                             + ".pdf"), properties);
            Compare(fileName, sourceFolder, destinationFolder);
        }
Esempio n. 5
0
        public virtual void SvgCssResolveHashXlinkTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                 .ValueOf("image"), "");
            iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
            imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "#testid"));
            JsoupElementNode       node = new JsoupElementNode(jsoupImage);
            SvgConverterProperties scp  = new SvgConverterProperties();

            scp.SetBaseUri(baseUri);
            SvgProcessorContext          processorContext = new SvgProcessorContext(scp);
            SvgStyleResolver             sr   = new SvgStyleResolver(node, processorContext);
            IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext());

            NUnit.Framework.Assert.AreEqual("#testid", attr.Get("xlink:href"));
        }
Esempio n. 6
0
        public virtual void SvgCssResolverXlinkTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                 .ValueOf("image"), "");
            iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
            imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "itis.jpg"));
            JsoupElementNode       node = new JsoupElementNode(jsoupImage);
            SvgConverterProperties scp  = new SvgConverterProperties();

            scp.SetBaseUri(baseUri);
            SvgProcessorContext          processorContext = new SvgProcessorContext(scp);
            SvgStyleResolver             sr   = new SvgStyleResolver(node, processorContext);
            IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext());
            String fileName    = baseUri + "itis.jpg";
            String expectedURL = UrlUtil.ToNormalizedURI(fileName).ToString();

            NUnit.Framework.Assert.AreEqual(expectedURL, attr.Get("xlink:href"));
        }
        /// <exception cref="System.IO.IOException"/>
        private PdfFormXObject ProcessAsSvg(Stream stream, ProcessorContext context)
        {
            SvgProcessingUtil      processingUtil         = new SvgProcessingUtil();
            SvgConverterProperties svgConverterProperties = new SvgConverterProperties();

            svgConverterProperties.SetBaseUri(context.GetBaseUri()).SetFontProvider(context.GetFontProvider()).SetMediaDeviceDescription
                (context.GetDeviceDescription());
            ISvgProcessorResult res = SvgConverter.ParseAndProcess(stream, svgConverterProperties);

            if (context.GetPdfDocument() != null)
            {
                return(processingUtil.CreateXObjectFromProcessingResult(res, context.GetPdfDocument()));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 8
0
        /// <exception cref="System.IO.IOException"/>
        private PdfFormXObject ProcessAsSvg(Stream stream, ProcessorContext context, String parentDir)
        {
            SvgProcessingUtil      processingUtil         = new SvgProcessingUtil();
            SvgConverterProperties svgConverterProperties = ContextMappingHelper.MapToSvgConverterProperties(context);

            if (parentDir != null)
            {
                svgConverterProperties.SetBaseUri(parentDir);
            }
            ISvgProcessorResult res = SvgConverter.ParseAndProcess(stream, svgConverterProperties);

            if (context.GetPdfDocument() != null)
            {
                return(processingUtil.CreateXObjectFromProcessingResult(res, context.GetPdfDocument()));
            }
            else
            {
                return(null);
            }
        }