Exemple #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)
        {
            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));
                }
            }
        }
Exemple #2
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
                                                                                                       ));
        }
Exemple #3
0
 /// <summary>
 /// Creates a new
 /// <see cref="SvgTagWorker"/>
 /// instance.
 /// </summary>
 /// <param name="element">the element</param>
 /// <param name="context">the context</param>
 public SvgTagWorker(IElementNode element, ProcessorContext context)
 {
     svgImage = null;
     try {
         SvgConverterProperties props = ContextMappingHelper.MapToSvgConverterProperties(context);
         processingResult = new DefaultSvgProcessor().Process((INode)element, props);
         context.StartProcessingInlineSvg();
     }
     catch (SvgProcessingException pe) {
         LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Impl.Tags.SvgTagWorker)).Error(iText.Html2pdf.LogMessageConstant
                                                                                          .UNABLE_TO_PROCESS_IMAGE_AS_SVG, pe);
     }
 }
Exemple #4
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);
            }
        }