示例#1
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)));
                }
            }
        }
示例#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));
                }
            }
        }
示例#3
0
        public virtual void AttemptToProcessBySvgProcessingUtilSvgWithSvgTest()
        {
            // TODO review this test in the scope of DEVSIX-4107
            String                  fileName               = "svgWithSvg.svg";
            ProcessorContext        context                = new ProcessorContext(new ConverterProperties());
            HtmlResourceResolver    resourceResolver       = new HtmlResourceResolver(sourceFolder, context);
            ISvgConverterProperties svgConverterProperties = ContextMappingHelper.MapToSvgConverterProperties(context);
            ISvgProcessorResult     res = SvgConverter.ParseAndProcess(resourceResolver.RetrieveResourceAsInputStream(fileName
                                                                                                                      ), svgConverterProperties);
            ISvgNodeRenderer imageRenderer = ((SvgTagSvgNodeRenderer)res.GetRootRenderer()).GetChildren()[1];

            // Remove the previous result of the resource resolving in order to demonstrate that the resource will not be
            // resolved due to not setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method.
            // But even if set baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method, the SVG will not
            // be processed, because in the createXObjectFromProcessingResult method we create ResourceResolver, not HtmlResourceResolver.
            imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "res\\itextpdf.com\\lines.svg");
            SvgProcessingUtil processingUtil = new SvgProcessingUtil(resourceResolver);
            PdfDocument       pdfDocument    = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfFormXObject    pdfFormXObject = processingUtil.CreateXObjectFromProcessingResult(res, pdfDocument);
            PdfDictionary     resources      = (PdfDictionary)pdfFormXObject.GetResources().GetPdfObject().Get(PdfName.XObject);
            PdfDictionary     fm1Dict        = (PdfDictionary)resources.Get(new PdfName("Fm1"));

            NUnit.Framework.Assert.IsFalse(((PdfDictionary)fm1Dict.Get(PdfName.Resources)).ContainsKey(PdfName.XObject
                                                                                                       ));
        }
示例#4
0
 public virtual void ProcessEnd(IElementNode element, ProcessorContext context)
 {
     if (context.GetPdfDocument() != null && processingResult != null)
     {
         SvgProcessingUtil util = new SvgProcessingUtil();
         svgImage = util.CreateImageFromProcessingResult(processingResult, context.GetPdfDocument());
         context.EndProcessingInlineSvg();
     }
 }
示例#5
0
 public virtual void ProcessEnd(IElementNode element, ProcessorContext context)
 {
     if (context.GetPdfDocument() != null && processingResult != null)
     {
         SvgProcessingUtil util = new SvgProcessingUtil();
         svgImage = util.CreateImageFromProcessingResult(processingResult, context.GetPdfDocument());
         AccessiblePropHelper.TrySetLangAttribute(svgImage, element);
         context.EndProcessingInlineSvg();
     }
 }
        /// <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);
            }
        }
示例#7
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);
            }
        }