/// <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)); } } }
/// <summary> /// Create an /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/> /// tied to the passed /// <c>PdfDocument</c> /// using the SVG processing result. /// </summary> /// <param name="result">Processing result containing the SVG information</param> /// <param name="pdfDocument">pdf that shall contain the image</param> /// <returns>PdfFormXObject instance</returns> public virtual PdfFormXObject CreateXObjectFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument ) { ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer(); float width; float height; float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer); width = wh[0]; height = wh[1]; PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, width, height)); PdfCanvas canvas = new PdfCanvas(pdfForm, pdfDocument); ResourceResolver tempResolver = new ResourceResolver(null, resourceResolver.GetRetriever()); // TODO DEVSIX-4107 pass the resourceResolver variable (not tempResolver variable) to the // SvgDrawContext constructor so that the SVG inside the SVG is processed. SvgDrawContext context = new SvgDrawContext(tempResolver, result.GetFontProvider(), result.GetRootRenderer ()); context.AddNamedObjects(result.GetNamedObjects()); context.PushCanvas(canvas); ISvgNodeRenderer root = new PdfRootSvgNodeRenderer(topSvgRenderer); root.Draw(context); return(pdfForm); }
public virtual void ParseAndProcessSuccessTest() { String name = "minimal"; FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); IDictionary <String, ISvgNodeRenderer> map = new Dictionary <String, ISvgNodeRenderer>(); RectangleSvgNodeRenderer rect = new RectangleSvgNodeRenderer(); rect.SetAttribute("fill", "none"); rect.SetAttribute("stroke", "black"); rect.SetAttribute("width", "500"); rect.SetAttribute("height", "400"); ISvgNodeRenderer root = new SvgTagSvgNodeRenderer(); root.SetAttribute("xmlns", "http://www.w3.org/2000/svg"); root.SetAttribute("version", "1.1"); root.SetAttribute("width", "500"); root.SetAttribute("height", "400"); ISvgProcessorResult expected = new SvgProcessorResult(map, root, new FontProvider(), new FontSet()); ISvgProcessorResult actual = SvgConverter.ParseAndProcess(fis); //TODO(RND-868): remove below checks NUnit.Framework.Assert.AreEqual(typeof(SvgTagSvgNodeRenderer), actual.GetRootRenderer().GetType()); NUnit.Framework.Assert.AreEqual(0, actual.GetNamedObjects().Count); NUnit.Framework.Assert.AreEqual("500", actual.GetRootRenderer().GetAttribute("width")); }
/// <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))); } } }
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 )); }
public virtual void ParseAndProcessIOExceptionTest() { NUnit.Framework.Assert.That(() => { Stream fis = new ExceptionInputStream(); ISvgProcessorResult result = SvgConverter.ParseAndProcess(fis); } , NUnit.Framework.Throws.InstanceOf <SvgProcessingException>()) ; }
public virtual void ProcessWithNullPropertiesTest() { DefaultSvgProcessor processor = new DefaultSvgProcessor(); iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGRoot = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("svg"), ""); INode root = new JsoupElementNode(jsoupSVGRoot); ISvgProcessorResult actual = processor.Process(root, null); ISvgProcessorResult expected = processor.Process(root); NUnit.Framework.Assert.AreEqual(expected.GetRootRenderer(), actual.GetRootRenderer()); }
/// <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 = new SvgConverterProperties().SetBaseUri(context.GetBaseUri()); 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); } }
/// <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); } }
/// <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); } }
/// <summary> /// Create an /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/> /// tied to the passed /// <c>PdfDocument</c> /// using the SVG processing result. /// </summary> /// <param name="result">Processing result containing the SVG information</param> /// <param name="pdfDocument">pdf that shall contain the image</param> /// <returns>PdfFormXObject instance</returns> public virtual PdfFormXObject CreateXObjectFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument ) { ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer(); float width; float height; float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer); width = wh[0]; height = wh[1]; PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, width, height)); PdfCanvas canvas = new PdfCanvas(pdfForm, pdfDocument); SvgDrawContext context = new SvgDrawContext(null, result.GetFontProvider()); context.AddNamedObjects(result.GetNamedObjects()); context.PushCanvas(canvas); ISvgNodeRenderer root = new PdfRootSvgNodeRenderer(topSvgRenderer); root.Draw(context); return(pdfForm); }
/// <summary> /// Create an /// <c>Image</c> /// layout object tied to the passed /// <c>PdfDocument</c> /// using the SVG processing result. /// </summary> /// <param name="result">Processing result containing the SVG information</param> /// <param name="pdfDocument">pdf that shall contain the image</param> /// <returns>image layout object</returns> public virtual Image CreateImageFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument) { PdfFormXObject xObject = CreateXObjectFromProcessingResult(result, pdfDocument); return(new Image(xObject)); }