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> /// 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 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 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> /// 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); }